|
| 1 | +import React, {useEffect, useMemo, useState} from 'react'; |
| 2 | +import {Assets, View, Text, Incubator, Icon, Colors} from 'react-native-ui-lib'; |
| 3 | +import {renderRadioGroup, renderSliderOption} from '../ExampleScreenPresenter'; |
| 4 | + |
| 5 | +const {Gradient} = Incubator; |
| 6 | + |
| 7 | +const COLORS = [Colors.$backgroundPrimaryHeavy, Colors.$backgroundPrimaryHeavy, Colors.$backgroundPrimaryMedium]; |
| 8 | + |
| 9 | +const GradientScreen = () => { |
| 10 | + const [type, setType] = useState('rectangle'); |
| 11 | + const [children, setChildren] = useState('none'); |
| 12 | + const [alignment, setAlignment] = useState('none'); |
| 13 | + const [size, setSize] = useState('fixed'); |
| 14 | + const [error, setError] = useState(''); |
| 15 | + const [angle, setAngle] = useState(0); |
| 16 | + |
| 17 | + const gradientProps = useMemo(() => { |
| 18 | + switch (type) { |
| 19 | + case 'rectangle': |
| 20 | + return size === 'fixed' ? {type: 'rectangle', width: 100, height: 100} : {type: 'rectangle'}; |
| 21 | + case 'circle': |
| 22 | + return size === 'fixed' ? {type: 'circle', radius: 50} : {type: 'circle'}; |
| 23 | + case 'border': |
| 24 | + return size === 'fixed' ? {type: 'border', width: 100, height: 100} : {type: 'border'}; |
| 25 | + } |
| 26 | + }, [type, size]); |
| 27 | + |
| 28 | + const childrenProps = useMemo(() => { |
| 29 | + switch (children) { |
| 30 | + case 'shortText': |
| 31 | + return <Text>Lorem ipsum dolor sit amet.</Text>; |
| 32 | + case 'text': |
| 33 | + return ( |
| 34 | + <Text> |
| 35 | + Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et |
| 36 | + dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex |
| 37 | + ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat |
| 38 | + nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit |
| 39 | + anim id est laborum. |
| 40 | + </Text> |
| 41 | + ); |
| 42 | + case 'icon': |
| 43 | + return <Icon source={Assets.icons.demo.search}/>; |
| 44 | + } |
| 45 | + }, [children]); |
| 46 | + |
| 47 | + const alignmentProp = useMemo(() => { |
| 48 | + switch (alignment) { |
| 49 | + case 'none': |
| 50 | + return undefined; |
| 51 | + case 'center': |
| 52 | + return {center: true}; |
| 53 | + case 'centerH': |
| 54 | + return {centerH: true}; |
| 55 | + case 'centerV': |
| 56 | + return {centerV: true}; |
| 57 | + } |
| 58 | + }, [alignment]); |
| 59 | + |
| 60 | + useEffect(() => { |
| 61 | + if (children === 'none' && size === 'flex' && type !== 'border') { |
| 62 | + setError('No children + flex gives no gradient'); |
| 63 | + } else if (size === 'flex' && type === 'circle') { |
| 64 | + setError('flex size will result with an ellipse instead of a circle'); |
| 65 | + } else { |
| 66 | + setError(''); |
| 67 | + } |
| 68 | + }, [children, size, type]); |
| 69 | + |
| 70 | + return ( |
| 71 | + <View padding-page> |
| 72 | + {renderRadioGroup('Select type', |
| 73 | + 'type', |
| 74 | + {Rectangle: 'rectangle', Circle: 'circle', Border: 'border'}, |
| 75 | + {isRow: true, state: type, setState: setType})} |
| 76 | + {renderRadioGroup('Select children', |
| 77 | + 'children', |
| 78 | + {No: 'none', 'Short text': 'shortText', Text: 'text', Icon: 'icon'}, |
| 79 | + {isRow: true, state: children, setState: setChildren})} |
| 80 | + {renderRadioGroup('Select children`s alignment', |
| 81 | + 'alignment', |
| 82 | + {None: 'none', Center: 'center', CenterH: 'centerH', CenterV: 'centerV'}, |
| 83 | + {isRow: true, state: alignment, setState: setAlignment})} |
| 84 | + {renderRadioGroup('Select size', |
| 85 | + 'size', |
| 86 | + {Fixed: 'fixed', Flex: 'flex'}, |
| 87 | + {isRow: true, state: size, setState: setSize})} |
| 88 | + <View marginH-s10> |
| 89 | + {renderSliderOption('Angle', 'angle', { |
| 90 | + min: 0, |
| 91 | + max: 360, |
| 92 | + step: 1, |
| 93 | + state: angle, |
| 94 | + setState: setAngle |
| 95 | + })} |
| 96 | + </View> |
| 97 | + <Gradient |
| 98 | + colors={COLORS} |
| 99 | + // @ts-expect-error |
| 100 | + type={type} |
| 101 | + {...gradientProps} |
| 102 | + {...alignmentProp} |
| 103 | + angle={angle} |
| 104 | + > |
| 105 | + {childrenProps} |
| 106 | + </Gradient> |
| 107 | + <Text marginT-s10 center $textDangerLight> |
| 108 | + {error} |
| 109 | + </Text> |
| 110 | + </View> |
| 111 | + ); |
| 112 | +}; |
| 113 | + |
| 114 | +export default GradientScreen; |
0 commit comments