-
Notifications
You must be signed in to change notification settings - Fork 734
Fix/date time picker to controlled #989
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Few comments on this PR
- The PR doesn't change the component to be a controlled component cause it still keep the value in an internal state instead of getting it from the user props.
- This is a breaking change to the component and we can't push it directly to master.
It means the user must pass the value prop, otherwise it won't react to changes.
It should be part of v6. - We need to make sure One App users migrate accordingly before we merge this.
static getDerivedStateFromProps(nextProps, prevState) { | ||
if (nextProps.value !== prevState.prevValue) { | ||
return { | ||
prevValue: prevState.value, | ||
value: nextProps.value | ||
}; | ||
} | ||
return null; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no need to keep the value in state if the component is controlled.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to keep an internal state for the onChange
@@ -133,14 +141,14 @@ class DateTimePicker extends BaseComponent { | |||
// since handleChange() is not called on iOS when there is no actual change | |||
this.chosenDate = new Date(); | |||
} | |||
|
|||
|
|||
_.invoke(this.props, 'onChange', this.chosenDate); | |||
this.setState({value: this.chosenDate}); | |||
}); | |||
|
|||
getStringValue = () => { | |||
const {value} = this.state; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the component is controlled, the value should derived directly from props and not the state.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, generally speaking, but when we want a controlled component and still need to keep an internal state that's the way to do it. See Carousel, DialogDismissibleView, PageControl, Picker, GradientSlider for example.
…DateTimePicker_to_controlled
@ethanshar I fixed the branching, should be only 1 file change. I don't see a breaking change here, the user doesn't have to pass 'value' prop. The change is made for when the user does pass 'value' prop and change it with screen state change. |
…hub.com:wix/react-native-ui-lib into fix/DateTimePicker_to_controlled # Conflicts: # src/components/dateTimePicker/index.js
Description
DateTimePicker - turn to a controlled component. Fix issue #729
Changelog
DateTimePicker - turn to a controlled component.