|
| 1 | +import React from 'react' |
| 2 | +import PropTypes from 'prop-types' |
| 3 | +import { View, Animated, ViewPropTypes, Easing } from 'react-native' |
| 4 | + |
| 5 | +import styles from './styles' |
| 6 | +import Segment from './Segment' |
| 7 | + |
| 8 | +/** |
| 9 | + * A custom `SegmentControl` component, pretty much similar to native's SegmentControl but with animation. |
| 10 | + * Animates when changing the segment value. |
| 11 | + */ |
| 12 | +class SegmentControl extends React.Component { |
| 13 | + constructor(props) { |
| 14 | + super(props) |
| 15 | + |
| 16 | + this.state = { |
| 17 | + selectedIndex: props.selectedIndex, |
| 18 | + segmentDimension: { width: 0, height: 0 }, |
| 19 | + activeSegmentPosition: { x: props.offsetHeight, y: props.offsetHeight }, |
| 20 | + positionAnimationValue: new Animated.Value(0) |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * On segment change event. |
| 26 | + * |
| 27 | + * @param {Number} index |
| 28 | + */ |
| 29 | + onSegmentSelection = index => { |
| 30 | + const animate = () => { |
| 31 | + Animated.timing(this.state.positionAnimationValue, { |
| 32 | + toValue: this.state.activeSegmentPosition.x, |
| 33 | + duration: 150, |
| 34 | + easing: Easing.ease |
| 35 | + }).start(() => this.props.onChange(index)) |
| 36 | + } |
| 37 | + |
| 38 | + this.setState( |
| 39 | + prevState => ({ |
| 40 | + selectedIndex: index, |
| 41 | + activeSegmentPosition: { x: prevState.segmentDimension.width * index + this.props.offsetHeight, y: prevState.activeSegmentPosition.y } |
| 42 | + }), |
| 43 | + animate |
| 44 | + ) |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Invoked on mount and layout change of `segmentContainer` view. |
| 49 | + * |
| 50 | + * @param {Object} event |
| 51 | + */ |
| 52 | + segmentOnLayout = event => { |
| 53 | + const { width, height } = event.nativeEvent.layout |
| 54 | + const segmentWidth = (width - this.props.offsetHeight * 2) / this.props.values.length |
| 55 | + |
| 56 | + const animate = () => { |
| 57 | + Animated.timing(this.state.positionAnimationValue, { |
| 58 | + toValue: segmentWidth * this.state.selectedIndex + this.props.offsetHeight, |
| 59 | + duration: 100 |
| 60 | + }).start() |
| 61 | + } |
| 62 | + |
| 63 | + this.setState(() => ({ |
| 64 | + segmentDimension: { width: segmentWidth, height } |
| 65 | + }), animate) |
| 66 | + } |
| 67 | + |
| 68 | + render() { |
| 69 | + const { style, disable, activeSegmentStyle, segmentControlStyle, selectedTextStyle, unSelectedTextStyle } = this.props |
| 70 | + const { width, height } = this.state.segmentDimension |
| 71 | + const segmentHeight = height - this.props.offsetHeight * 2 |
| 72 | + |
| 73 | + const isDisabled = disable ? 'none' : 'auto' |
| 74 | + const extraStyles = disable ? styles.vivid : {} |
| 75 | + |
| 76 | + return ( |
| 77 | + <View style={[styles.mainContainer, style]} pointerEvents={isDisabled}> |
| 78 | + <View |
| 79 | + style={[styles.segmentContainer, extraStyles, { height, borderRadius: height }, segmentControlStyle]} |
| 80 | + onLayout={this.segmentOnLayout} |
| 81 | + > |
| 82 | + {this.props.values.map((segment, index) => ( |
| 83 | + <Segment |
| 84 | + style={{ height: segmentHeight }} |
| 85 | + title={segment} |
| 86 | + textStyle={index !== this.state.selectedIndex ? unSelectedTextStyle : {...styles.activeText, ...selectedTextStyle}} |
| 87 | + onPress={() => this.onSegmentSelection(index)} |
| 88 | + /> |
| 89 | + ))} |
| 90 | + <Animated.View |
| 91 | + style={[ |
| 92 | + { |
| 93 | + width, |
| 94 | + height: segmentHeight, |
| 95 | + left: this.state.positionAnimationValue, |
| 96 | + top: this.state.activeSegmentPosition.y |
| 97 | + }, |
| 98 | + styles.segment, |
| 99 | + styles.activeSegment, |
| 100 | + activeSegmentStyle, |
| 101 | + ]} |
| 102 | + /> |
| 103 | + </View> |
| 104 | + </View> |
| 105 | + ) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +SegmentControl.defaultProps = { |
| 110 | + offsetHeight: 3, |
| 111 | + selectedIndex: 0, |
| 112 | + style: {}, |
| 113 | + segmentControlStyle: {}, |
| 114 | + activeSegmentStyle: {}, |
| 115 | + selectedTextStyle: {}, |
| 116 | + unSelectedTextStyle: {} |
| 117 | +} |
| 118 | + |
| 119 | +SegmentControl.propTypes = { |
| 120 | + /** |
| 121 | + * Segment values that are rendered on the view itself. |
| 122 | + */ |
| 123 | + values: PropTypes.arrayOf(PropTypes.string).isRequired, |
| 124 | + |
| 125 | + /** |
| 126 | + * To enable diable the segment control. Default value is `false`. |
| 127 | + */ |
| 128 | + disable: PropTypes.bool.isRequired, |
| 129 | + |
| 130 | + /** |
| 131 | + * A callback function of segment index on change. Changed index is send on the callback as a param. |
| 132 | + */ |
| 133 | + onChange: PropTypes.func.isRequired, |
| 134 | + |
| 135 | + /** |
| 136 | + * Index of the selected segment |
| 137 | + */ |
| 138 | + selectedIndex: PropTypes.number, |
| 139 | + |
| 140 | + /** |
| 141 | + * Active Segment's offset height. Basically a padding. |
| 142 | + */ |
| 143 | + offsetHeight: PropTypes.number, |
| 144 | + |
| 145 | + /** |
| 146 | + * Styles props of main wrapper |
| 147 | + */ |
| 148 | + style: ViewPropTypes.style, |
| 149 | + |
| 150 | + /** |
| 151 | + * Styles props of segment control |
| 152 | + */ |
| 153 | + segmentControlStyle: ViewPropTypes.style, |
| 154 | + |
| 155 | + /** |
| 156 | + * Styles props of active segment |
| 157 | + */ |
| 158 | + activeSegmentStyle: ViewPropTypes.style, |
| 159 | + |
| 160 | + /** |
| 161 | + * Selected Segment text style. |
| 162 | + */ |
| 163 | + selectedTextStyle: ViewPropTypes.style, |
| 164 | + |
| 165 | + /** |
| 166 | + * Unselected Segment text style. |
| 167 | + */ |
| 168 | + unSelectedTextStyle: ViewPropTypes.style, |
| 169 | +} |
| 170 | + |
| 171 | +export default SegmentControl |
0 commit comments