|
| 1 | +/** |
| 2 | + * @providesModule %BigSlider |
| 3 | + * @flow |
| 4 | + */ |
| 5 | + |
| 6 | +import React, { Component } from 'react'; |
| 7 | +import { |
| 8 | + StyleSheet, |
| 9 | + PanResponder, |
| 10 | + Text, |
| 11 | + View, |
| 12 | +} from 'react-native'; |
| 13 | + |
| 14 | +export default class BigSlider extends Component { |
| 15 | + static defaultProps = { |
| 16 | + value: 40, |
| 17 | + maximumValue: 100, |
| 18 | + minimumValue: 0, |
| 19 | + } |
| 20 | + |
| 21 | + constructor(props) { |
| 22 | + super() |
| 23 | + this.state = { |
| 24 | + value: props.value |
| 25 | + } |
| 26 | + |
| 27 | + this.range = props.maximumValue - props.minimumValue |
| 28 | + } |
| 29 | + |
| 30 | + componentWillMount () { |
| 31 | + this.panResponder = PanResponder.create({ |
| 32 | + onStartShouldSetPanResponder: (e, gestureState) => true, |
| 33 | + onPanResponderGrant: (evt, gestureState) => { |
| 34 | + if (typeof this.props.onSlidingStart === 'function') this.props.onSlidingStart() |
| 35 | + this.setState({ anchorValue: this.state.value }) |
| 36 | + }, |
| 37 | + onPanResponderMove: (evt, gestureState) => { |
| 38 | + const { maximumValue, minimumValue } = this.props |
| 39 | + let valueIncrement = (-gestureState.dy * this.range) / this.state.height |
| 40 | + let nextValue = this.state.anchorValue + valueIncrement |
| 41 | + nextValue = nextValue >= maximumValue ? maximumValue : nextValue |
| 42 | + nextValue = nextValue <= minimumValue ? minimumValue : nextValue |
| 43 | + |
| 44 | + if (typeof this.props.onValueChange === 'function') this.props.onValueChange(nextValue) |
| 45 | + this.setState({ value: nextValue }) |
| 46 | + }, |
| 47 | + onPanResponderRelease: (evt, gestureState) => { |
| 48 | + if (typeof this.props.onSlidingComplete === 'function') this.props.onSlidingComplete() |
| 49 | + }, |
| 50 | + }) |
| 51 | + } |
| 52 | + |
| 53 | + onLayout = ({ nativeEvent }) => { |
| 54 | + this.setState({ |
| 55 | + width: nativeEvent.layout.width, |
| 56 | + height: nativeEvent.layout.height, |
| 57 | + }) |
| 58 | + } |
| 59 | + |
| 60 | + render () { |
| 61 | + const value = this.state.value |
| 62 | + const unitValue = (value - this.props.minimumValue) / this.range |
| 63 | + |
| 64 | + return ( |
| 65 | + <View |
| 66 | + onLayout={this.onLayout} |
| 67 | + style={[styles.container, this.props.style]} |
| 68 | + {...this.panResponder.panHandlers}> |
| 69 | + <View style={[styles.pendingTrack, { flex: 1 - unitValue }]} /> |
| 70 | + <View style={[styles.track, { flex: unitValue }, this.props.trackStyle]}> |
| 71 | + <View style={styles.thumb} /> |
| 72 | + {this.props.renderLabel |
| 73 | + ? this.props.renderLabel() |
| 74 | + : <View style={styles.trackLabel}> |
| 75 | + <Text style={styles.trackLabelText}> |
| 76 | + {this.props.label || `${formatNumber(this.props.value)}%`} |
| 77 | + </Text> |
| 78 | + </View> |
| 79 | + } |
| 80 | + </View> |
| 81 | + </View> |
| 82 | + ); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +const styles = StyleSheet.create({ |
| 87 | + container: { |
| 88 | + flex: 1, |
| 89 | + justifyContent: 'center', |
| 90 | + alignItems: 'center', |
| 91 | + backgroundColor: 'rgb(241, 242, 247)', |
| 92 | + borderRadius: 12, |
| 93 | + overflow: 'hidden', |
| 94 | + width: 120, |
| 95 | + }, |
| 96 | + pendingTrack: { |
| 97 | + }, |
| 98 | + track: { |
| 99 | + flex: 1, |
| 100 | + backgroundColor: 'rgb(1, 160, 188)', |
| 101 | + borderRadius: 12, |
| 102 | + alignSelf: 'stretch', |
| 103 | + }, |
| 104 | + trackLabel: { |
| 105 | + flex: 1, |
| 106 | + justifyContent: 'center', |
| 107 | + alignItems: 'center', |
| 108 | + }, |
| 109 | + trackLabelText: { |
| 110 | + color: 'white', |
| 111 | + fontWeight: '600', |
| 112 | + }, |
| 113 | + thumb: { |
| 114 | + backgroundColor: 'rgba(0,0,0,.5)', |
| 115 | + borderRadius: 12, |
| 116 | + height: 3, |
| 117 | + width: 24, |
| 118 | + marginTop: 6, |
| 119 | + alignSelf: 'center', |
| 120 | + }, |
| 121 | +}) |
| 122 | + |
| 123 | +function formatNumber (x) { |
| 124 | + return x.toFixed(1).replace(/\.?0*$/, '') |
| 125 | +} |
0 commit comments