|
| 1 | +import React from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import {StyleSheet, View, ScrollView} from 'react-native'; |
| 4 | +import LinearGradient from 'react-native-linear-gradient'; |
| 5 | +import PickerListItem from './PickerListItem'; |
| 6 | + |
| 7 | +export default class DynamicallySelectedPicker extends React.Component { |
| 8 | + state = { |
| 9 | + itemHeight: this.props.height / (this.props.transparentItemRows * 2 + 1), |
| 10 | + itemIndex: this.props.initialSelectedIndex, |
| 11 | + }; |
| 12 | + |
| 13 | + fakeItems(n = 3) { |
| 14 | + const itemsArr = []; |
| 15 | + for (let i = 0; i < n; i++) { |
| 16 | + itemsArr[i] = { |
| 17 | + value: -1, |
| 18 | + label: '', |
| 19 | + }; |
| 20 | + } |
| 21 | + return itemsArr; |
| 22 | + } |
| 23 | + |
| 24 | + allItemsLength() { |
| 25 | + return this.extendedItems().length - this.props.transparentItemRows * 2; |
| 26 | + } |
| 27 | + |
| 28 | + onScroll(event) { |
| 29 | + const {items, onScrollDynamicallyChange} = this.props; |
| 30 | + const tempIndex = this.getItemTemporaryIndex(event); |
| 31 | + |
| 32 | + if ( |
| 33 | + this.state.itemIndex !== tempIndex && |
| 34 | + tempIndex >= 0 && |
| 35 | + tempIndex < this.allItemsLength() |
| 36 | + ) { |
| 37 | + this.setItemIndex(tempIndex); |
| 38 | + onScrollDynamicallyChange({index: tempIndex, item: items[tempIndex]}); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + onScrollBegin(event) { |
| 43 | + const {items, onScrollBegin} = this.props; |
| 44 | + const tempIndex = this.getItemTemporaryIndex(event); |
| 45 | + |
| 46 | + if (tempIndex >= 0 && tempIndex < this.allItemsLength()) { |
| 47 | + this.setItemIndex(tempIndex); |
| 48 | + onScrollBegin({index: tempIndex, item: items[tempIndex]}); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + onScrollEnd(event) { |
| 53 | + const {items, onScrollEnd} = this.props; |
| 54 | + const tempIndex = this.getItemTemporaryIndex(event); |
| 55 | + |
| 56 | + if (tempIndex >= 0 && tempIndex < this.allItemsLength()) { |
| 57 | + this.setItemIndex(tempIndex); |
| 58 | + onScrollEnd({index: tempIndex, item: items[tempIndex]}); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + getItemTemporaryIndex(event) { |
| 63 | + return Math.round( |
| 64 | + event.nativeEvent.contentOffset.y / this.state.itemHeight, |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + setItemIndex(index) { |
| 69 | + this.setState({ |
| 70 | + itemIndex: index, |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + extendedItems() { |
| 75 | + const {transparentItemRows} = this.props; |
| 76 | + return [ |
| 77 | + ...this.fakeItems(transparentItemRows), |
| 78 | + ...this.props.items, |
| 79 | + ...this.fakeItems(transparentItemRows), |
| 80 | + ]; |
| 81 | + } |
| 82 | + |
| 83 | + render() { |
| 84 | + const {itemIndex, itemHeight} = this.state; |
| 85 | + const { |
| 86 | + width, |
| 87 | + height, |
| 88 | + topGradientColors, |
| 89 | + bottomGradientColors, |
| 90 | + transparentItemRows, |
| 91 | + itemsColor, |
| 92 | + fontSize, |
| 93 | + fontFamily, |
| 94 | + selectedItemBorderColor, |
| 95 | + } = this.props; |
| 96 | + return ( |
| 97 | + <View style={{height: height, width: width}}> |
| 98 | + <ScrollView |
| 99 | + showsVerticalScrollIndicator={false} |
| 100 | + showsHorizontalScrollIndicator={false} |
| 101 | + onMomentumScrollBegin={(event) => { |
| 102 | + this.onScrollBegin(event); |
| 103 | + }} |
| 104 | + onMomentumScrollEnd={(event) => { |
| 105 | + this.onScrollEnd(event); |
| 106 | + }} |
| 107 | + onScroll={(event) => { |
| 108 | + this.onScroll(event); |
| 109 | + }} |
| 110 | + scrollEventThrottle |
| 111 | + initialScrollIndex={itemIndex} |
| 112 | + snapToInterval={itemHeight}> |
| 113 | + {this.extendedItems().map((item, index) => { |
| 114 | + return ( |
| 115 | + <PickerListItem |
| 116 | + key={index} |
| 117 | + label={item.label} |
| 118 | + itemColor={item.itemColor} |
| 119 | + itemsColor={itemsColor} |
| 120 | + fontSize={fontSize ? fontSize : itemHeight / 2} |
| 121 | + fontFamily={fontFamily} |
| 122 | + style={[ |
| 123 | + styles.listItem, |
| 124 | + { |
| 125 | + height: itemHeight, |
| 126 | + }, |
| 127 | + ]} |
| 128 | + /> |
| 129 | + ); |
| 130 | + })} |
| 131 | + </ScrollView> |
| 132 | + <View |
| 133 | + style={[ |
| 134 | + styles.gradientWrapper, |
| 135 | + { |
| 136 | + top: 0, |
| 137 | + borderBottomWidth: 1, |
| 138 | + borderBottomColor: selectedItemBorderColor, |
| 139 | + }, |
| 140 | + ]} |
| 141 | + pointerEvents="none"> |
| 142 | + <LinearGradient |
| 143 | + colors={topGradientColors} |
| 144 | + style={[ |
| 145 | + styles.pickerGradient, |
| 146 | + { |
| 147 | + height: transparentItemRows * itemHeight, |
| 148 | + }, |
| 149 | + ]} |
| 150 | + /> |
| 151 | + </View> |
| 152 | + <View |
| 153 | + style={[ |
| 154 | + styles.gradientWrapper, |
| 155 | + { |
| 156 | + bottom: 0, |
| 157 | + borderTopWidth: 1, |
| 158 | + borderTopColor: selectedItemBorderColor, |
| 159 | + }, |
| 160 | + ]} |
| 161 | + pointerEvents="none"> |
| 162 | + <LinearGradient |
| 163 | + colors={bottomGradientColors} |
| 164 | + style={[ |
| 165 | + styles.pickerGradient, |
| 166 | + {height: transparentItemRows * itemHeight}, |
| 167 | + ]} |
| 168 | + /> |
| 169 | + </View> |
| 170 | + </View> |
| 171 | + ); |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +DynamicallySelectedPicker.defaultProps = { |
| 176 | + items: [{value: 0, label: 'No items'}], |
| 177 | + onScrollDynamicallyChange: () => {}, |
| 178 | + onScrollBegin: () => {}, |
| 179 | + onScrollEnd: () => {}, |
| 180 | + width: 200, |
| 181 | + height: 200, |
| 182 | + initialSelectedIndex: 0, |
| 183 | + transparentItemRows: 3, |
| 184 | + itemsColor: '#000', |
| 185 | + fontFamily: 'Arial', |
| 186 | + selectedItemBorderColor: '#cecece', |
| 187 | + topGradientColors: [ |
| 188 | + 'rgba( 255, 255, 255, 1 )', |
| 189 | + 'rgba( 255, 255, 255, 0.9 )', |
| 190 | + 'rgba( 255, 255, 255, 0.7 )', |
| 191 | + 'rgba( 255, 255, 255, 0.5 )', |
| 192 | + ], |
| 193 | + bottomGradientColors: [ |
| 194 | + 'rgba( 255, 255, 255, 0.5 )', |
| 195 | + 'rgba( 255, 255, 255, 0.7 )', |
| 196 | + 'rgba( 255, 255, 255, 0.9 )', |
| 197 | + 'rgba( 255, 255, 255, 1 )', |
| 198 | + ], |
| 199 | +}; |
| 200 | + |
| 201 | +DynamicallySelectedPicker.propTypes = { |
| 202 | + items: PropTypes.arrayOf( |
| 203 | + PropTypes.shape({ |
| 204 | + value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), |
| 205 | + label: PropTypes.string, |
| 206 | + itemColor: PropTypes.string, |
| 207 | + }), |
| 208 | + ), |
| 209 | + onScrollDynamicallyChange: PropTypes.func, |
| 210 | + onScrollBegin: PropTypes.func, |
| 211 | + onScrollEnd: PropTypes.func, |
| 212 | + initialSelectedIndex: PropTypes.number, |
| 213 | + height: PropTypes.number, |
| 214 | + width: PropTypes.number, |
| 215 | + itemsColor: PropTypes.string, |
| 216 | + selectedItemBorderColor: PropTypes.string, |
| 217 | + fontSize: PropTypes.number, |
| 218 | + fontFamily: PropTypes.string, |
| 219 | + topGradientColors: PropTypes.array, |
| 220 | + bottomGradientColors: PropTypes.array, |
| 221 | +}; |
| 222 | + |
| 223 | +const styles = StyleSheet.create({ |
| 224 | + listItem: { |
| 225 | + alignItems: 'center', |
| 226 | + justifyContent: 'center', |
| 227 | + }, |
| 228 | + gradientWrapper: { |
| 229 | + position: 'absolute', |
| 230 | + width: '100%', |
| 231 | + }, |
| 232 | + pickerGradient: { |
| 233 | + width: '100%', |
| 234 | + }, |
| 235 | +}); |
0 commit comments