|
| 1 | +import React, {useState, useRef, useCallback} from 'react'; |
| 2 | +import {StyleSheet, ScrollView} from 'react-native'; |
| 3 | +import {Constants, Colors, View, Text, Button, Incubator} from 'react-native-ui-lib'; //eslint-disable-line |
| 4 | +import {renderBooleanOption} from '../ExampleScreenPresenter'; |
| 5 | + |
| 6 | +const VALUE = 20; |
| 7 | +const NEGATIVE_VALUE = -30; |
| 8 | +const MIN = 0; |
| 9 | +const MAX = 100; |
| 10 | +const INITIAL_MIN = 10; |
| 11 | +const INITIAL_MAX = 70; |
| 12 | + |
| 13 | +const IncubatorSliderScreen = () => { |
| 14 | + const [disableRTL, setDisableRTL] = useState<boolean>(false); |
| 15 | + |
| 16 | + const [sliderValue, setSliderValue] = useState(0); |
| 17 | + const [customSliderValue, setCustomSliderValue] = useState(VALUE); |
| 18 | + const [negativeSliderValue, setNegativeSliderValue] = useState(NEGATIVE_VALUE); |
| 19 | + const [sliderMinValue, setSliderMinValue] = useState(INITIAL_MIN); |
| 20 | + const [sliderMaxValue, setSliderMaxValue] = useState(INITIAL_MAX); |
| 21 | + |
| 22 | + const slider = useRef<typeof Incubator.Slider>(); |
| 23 | + const customSlider = useRef<typeof Incubator.Slider>(); |
| 24 | + const negativeSlider = useRef<typeof Incubator.Slider>(); |
| 25 | + const rangeSlider = useRef<typeof Incubator.Slider>(); |
| 26 | + |
| 27 | + const resetSliders = useCallback(() => { |
| 28 | + slider.current?.reset(); |
| 29 | + customSlider.current?.reset(); |
| 30 | + rangeSlider.current?.reset(); |
| 31 | + negativeSlider.current?.reset(); |
| 32 | + }, []); |
| 33 | + |
| 34 | + const onValueChange = useCallback((value: number) => { |
| 35 | + setSliderValue(value); |
| 36 | + }, []); |
| 37 | + |
| 38 | + const onCustomValueChange = useCallback((value: number) => { |
| 39 | + setCustomSliderValue(value); |
| 40 | + }, []); |
| 41 | + |
| 42 | + const onNegativeValueChange = useCallback((value: number) => { |
| 43 | + setNegativeSliderValue(value); |
| 44 | + }, []); |
| 45 | + |
| 46 | + const onRangeChange = useCallback((value: {min: number; max: number}) => { |
| 47 | + setSliderMaxValue(value.max); |
| 48 | + setSliderMinValue(value.min); |
| 49 | + }, []); |
| 50 | + |
| 51 | + const renderValuesBox = (min: number, max?: number) => { |
| 52 | + if (max !== undefined) { |
| 53 | + return ( |
| 54 | + <View row spread marginB-20> |
| 55 | + <Text bodySmall $textNeutral> |
| 56 | + min. {min} |
| 57 | + </Text> |
| 58 | + <Text bodySmall $textNeutral> |
| 59 | + max. {max} |
| 60 | + </Text> |
| 61 | + </View> |
| 62 | + ); |
| 63 | + } else { |
| 64 | + return ( |
| 65 | + <View center marginB-20> |
| 66 | + <Text bodySmall $textNeutral> |
| 67 | + value: {min} |
| 68 | + </Text> |
| 69 | + </View> |
| 70 | + ); |
| 71 | + } |
| 72 | + }; |
| 73 | + |
| 74 | + const renderDefaultSliderExample = () => { |
| 75 | + return ( |
| 76 | + <View> |
| 77 | + <Text margin-10 text70BL $textDefault> |
| 78 | + Default Slider values 0 to 1 |
| 79 | + </Text> |
| 80 | + {renderValuesBox(sliderValue)} |
| 81 | + <Incubator.Slider |
| 82 | + ref={slider} |
| 83 | + onValueChange={onValueChange} |
| 84 | + containerStyle={styles.container} |
| 85 | + disableRTL={disableRTL} |
| 86 | + // maximumValue={0} |
| 87 | + // maximumValue={50} |
| 88 | + step={0.1} |
| 89 | + /> |
| 90 | + </View> |
| 91 | + ); |
| 92 | + }; |
| 93 | + |
| 94 | + const renderDisabledSliderExample = () => { |
| 95 | + return ( |
| 96 | + <View marginT-20> |
| 97 | + <Text margin-10 text70BL $textDefault> |
| 98 | + Disabled Slider |
| 99 | + </Text> |
| 100 | + <Incubator.Slider value={0.4} containerStyle={styles.container} disableRTL={disableRTL} disabled/> |
| 101 | + </View> |
| 102 | + ); |
| 103 | + }; |
| 104 | + |
| 105 | + const renderCustomSliderExample = () => { |
| 106 | + return ( |
| 107 | + <View marginT-20 marginH-40> |
| 108 | + <Text margin-10 text70BL $textDefault> |
| 109 | + Custom Slider |
| 110 | + </Text> |
| 111 | + {renderValuesBox(customSliderValue)} |
| 112 | + <Incubator.Slider |
| 113 | + ref={customSlider} |
| 114 | + onValueChange={onCustomValueChange} |
| 115 | + value={20} |
| 116 | + minimumValue={0} |
| 117 | + maximumValue={100} |
| 118 | + containerStyle={styles.container} |
| 119 | + trackStyle={styles.customTrack} |
| 120 | + minimumTrackTintColor={Colors.grey30} |
| 121 | + maximumTrackTintColor={Colors.grey70} |
| 122 | + // thumbTintColor={Colors.orange30} |
| 123 | + thumbStyle={styles.customThumb} |
| 124 | + activeThumbStyle={styles.customActiveThumb} |
| 125 | + disableRTL={disableRTL} |
| 126 | + // disableActiveStyling |
| 127 | + /> |
| 128 | + </View> |
| 129 | + ); |
| 130 | + }; |
| 131 | + |
| 132 | + const renderNegativeSliderExample = () => { |
| 133 | + return ( |
| 134 | + <View marginT-20> |
| 135 | + <Text margin-10 text70BL $textDefault> |
| 136 | + Negative values -20 to -100 initial -30 |
| 137 | + </Text> |
| 138 | + {renderValuesBox(negativeSliderValue)} |
| 139 | + <Incubator.Slider |
| 140 | + ref={negativeSlider} |
| 141 | + onValueChange={onNegativeValueChange} |
| 142 | + value={-30} |
| 143 | + minimumValue={-100} |
| 144 | + maximumValue={-20} |
| 145 | + // step={10} |
| 146 | + containerStyle={styles.container} |
| 147 | + disableRTL={disableRTL} |
| 148 | + /> |
| 149 | + </View> |
| 150 | + ); |
| 151 | + }; |
| 152 | + |
| 153 | + const renderRangeSliderExample = () => { |
| 154 | + return ( |
| 155 | + <View marginT-20> |
| 156 | + <Text margin-10 text70BL $textDefault> |
| 157 | + Range Slider values 0 to 100 |
| 158 | + </Text> |
| 159 | + <View marginH-20> |
| 160 | + {renderValuesBox(sliderMinValue, sliderMaxValue)} |
| 161 | + <Incubator.Slider |
| 162 | + ref={rangeSlider} |
| 163 | + useRange |
| 164 | + useGap |
| 165 | + onRangeChange={onRangeChange} |
| 166 | + minimumValue={MIN} |
| 167 | + maximumValue={MAX} |
| 168 | + initialMinimumValue={INITIAL_MIN} |
| 169 | + initialMaximumValue={INITIAL_MAX} |
| 170 | + step={1} |
| 171 | + disableRTL={disableRTL} |
| 172 | + /> |
| 173 | + </View> |
| 174 | + </View> |
| 175 | + ); |
| 176 | + }; |
| 177 | + |
| 178 | + return ( |
| 179 | + <ScrollView showsVerticalScrollIndicator={false} style={{backgroundColor: Colors.$backgroundDefault}}> |
| 180 | + <View row spread margin-20> |
| 181 | + <Text h1 $textDefault> |
| 182 | + Slider |
| 183 | + </Text> |
| 184 | + <Button link label="Reset Sliders" onPress={resetSliders}/> |
| 185 | + </View> |
| 186 | + <View marginL-20> |
| 187 | + {Constants.isRTL && |
| 188 | + renderBooleanOption('Disable RTL', 'disableRTL', {spread: false, state: disableRTL, setState: setDisableRTL})} |
| 189 | + </View> |
| 190 | + {renderDefaultSliderExample()} |
| 191 | + {renderDisabledSliderExample()} |
| 192 | + {renderCustomSliderExample()} |
| 193 | + {renderNegativeSliderExample()} |
| 194 | + {renderRangeSliderExample()} |
| 195 | + </ScrollView> |
| 196 | + ); |
| 197 | +}; |
| 198 | + |
| 199 | +export default IncubatorSliderScreen; |
| 200 | + |
| 201 | +const styles = StyleSheet.create({ |
| 202 | + container: { |
| 203 | + marginHorizontal: 20 |
| 204 | + }, |
| 205 | + customTrack: { |
| 206 | + borderRadius: 1.5, |
| 207 | + height: 3 |
| 208 | + }, |
| 209 | + customThumb: { |
| 210 | + width: 14, |
| 211 | + height: 14, |
| 212 | + borderRadius: 7, |
| 213 | + backgroundColor: Colors.black, |
| 214 | + borderColor: Colors.black |
| 215 | + }, |
| 216 | + customActiveThumb: { |
| 217 | + backgroundColor: Colors.red30, |
| 218 | + borderWidth: 2, |
| 219 | + borderColor: Colors.red70 |
| 220 | + } |
| 221 | +}); |
0 commit comments