Skip to content

Supress generated warnings #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ render() {

## Properties

| Prop | Type | Description |
|---|---|---|
|**`disable`**|`Boolean`|Enable / Disable the entire component. Default is false.|
|**`values`**|`Array(String)`|The labels for the control's segment buttons, in order.|
|**`onChange`**|`function`|Callback that is called when the user taps a segment.|
|**`selectedIndex`**|`Number`|Index of the selected segment.|
|**`offsetHeight`**|`Number`|Active Segment's offset height.|
|**`style`**|`Styles`|Styles props of segment control.|
|**`segmentControlStyle`**|`Styles`|Styles props of segment control.|
|**`activeSegmentStyle`**|`Styles`|Styles props of active segment view.|
|**`selectedTextStyle`**|`Styles`|Selected Segment's text style.|
|**`unSelectedTextStyle`**|`Styles`|Unselected Segment's text style.|
| Prop | Type | Default | Description |
|---|---|---|---|
|**`disable`**|`Boolean`|`false`|Enable / Disable the entire component. Default is false.|
|**`values`**|`Array(String)`|`-`|The labels for the control's segment buttons, in order.|
|**`onChange`**|`function`|`-`|Callback that is called when the user taps a segment.|
|**`selectedIndex`**|`Number`|`0`|Index of the selected segment.|
|**`offsetHeight`**|`Number`|`3`|Active Segment's offset height.|
|**`style`**|`Styles`|`{ }`|Styles props of segment control.|
|**`segmentControlStyle`**|`Styles`|`{ }`|Styles props of segment control.|
|**`activeSegmentStyle`**|`Styles`|`{ }`|Styles props of active segment view.|
|**`selectedTextStyle`**|`Styles`|`{ }`|Selected Segment's text style.|
|**`unSelectedTextStyle`**|`Styles`|`{ }`|Unselected Segment's text style.|

## License

Expand Down
5 changes: 3 additions & 2 deletions src/SegmentControl/Segment.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ const Segment = ({ title, style, textStyle, onPress }) => (
)

Segment.defaultProps = {
style: {}
style: {},
textStyle: {}
}

Segment.propTypes = {
title: PropTypes.string.isRequired,
textStyle: ViewPropTypes.style.isRequired,
textStyle: Text.propTypes.style,
onPress: PropTypes.func.isRequired,
style: ViewPropTypes.style
}
Expand Down
61 changes: 56 additions & 5 deletions src/SegmentControl/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import PropTypes from 'prop-types'
import { View, Animated, ViewPropTypes, Easing } from 'react-native'
import { View, Animated, ViewPropTypes, Easing, Text} from 'react-native'

import styles from './styles'
import Segment from './Segment'
Expand All @@ -19,6 +19,47 @@ class SegmentControl extends React.Component {
activeSegmentPosition: { x: props.offsetHeight, y: props.offsetHeight },
positionAnimationValue: new Animated.Value(0)
}

this.componentShouldReRender = false
}

UNSAFE_componentWillUpdate(nextProps, nextState) {
if(this.componentShouldReRender) {
return;
}

if (!this.componentShouldReRender && nextProps.selectedIndex != this.state.selectedIndex) {
this.onParentStateChange(nextProps.selectedIndex)
}
}

/**
* Trigger change when state is changed from parent component
*
* @param {Number} index
*/
onParentStateChange = index => {

this.componentShouldReRender = true

setTimeout(() => this.componentShouldReRender = false, 1000)

const animate = () => {
Animated.timing(this.state.positionAnimationValue, {
toValue: this.state.activeSegmentPosition.x,
duration: 150,
useNativeDriver: this.props.useNativeDriver,
easing: Easing.ease
}).start()
}

this.setState(
prevState => ({
selectedIndex: index,
activeSegmentPosition: { x: prevState.segmentDimension.width * index + this.props.offsetHeight, y: prevState.activeSegmentPosition.y }
}),
animate
)
}

/**
Expand All @@ -31,6 +72,7 @@ class SegmentControl extends React.Component {
Animated.timing(this.state.positionAnimationValue, {
toValue: this.state.activeSegmentPosition.x,
duration: 150,
useNativeDriver: this.props.useNativeDriver,
easing: Easing.ease
}).start(() => this.props.onChange(index))
}
Expand All @@ -56,7 +98,8 @@ class SegmentControl extends React.Component {
const animate = () => {
Animated.timing(this.state.positionAnimationValue, {
toValue: segmentWidth * this.state.selectedIndex + this.props.offsetHeight,
duration: 100
duration: 100,
useNativeDriver: this.props.useNativeDriver,
}).start()
}

Expand All @@ -81,6 +124,7 @@ class SegmentControl extends React.Component {
>
{this.props.values.map((segment, index) => (
<Segment
key={`key-${segment}`}
style={{ height: segmentHeight }}
title={segment}
textStyle={index !== this.state.selectedIndex ? unSelectedTextStyle : {...styles.activeText, ...selectedTextStyle}}
Expand Down Expand Up @@ -113,7 +157,9 @@ SegmentControl.defaultProps = {
segmentControlStyle: {},
activeSegmentStyle: {},
selectedTextStyle: {},
unSelectedTextStyle: {}
unSelectedTextStyle: {},
disable: false,
useNativeDriver: false
}

SegmentControl.propTypes = {
Expand Down Expand Up @@ -160,12 +206,17 @@ SegmentControl.propTypes = {
/**
* Selected Segment text style.
*/
selectedTextStyle: ViewPropTypes.style,
selectedTextStyle: Text.propTypes.style,

/**
* Unselected Segment text style.
*/
unSelectedTextStyle: ViewPropTypes.style,
unSelectedTextStyle: Text.propTypes.style,

/**
* To enable useNativeDriver for smooth animations, current style throws error when set to true. Default value is `false`.
*/
useNativeDriver: PropTypes.bool,
}

export default SegmentControl