Skip to content

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

Merged
merged 3 commits into from
Nov 16, 2020

Conversation

Inbal-Tish
Copy link
Collaborator

Description

DateTimePicker - turn to a controlled component. Fix issue #729

Changelog

DateTimePicker - turn to a controlled component.

@Inbal-Tish Inbal-Tish requested a review from ethanshar October 19, 2020 07:11
@ethanshar ethanshar self-assigned this Oct 21, 2020
Copy link
Collaborator

@ethanshar ethanshar left a 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.

Comment on lines +104 to 112
static getDerivedStateFromProps(nextProps, prevState) {
if (nextProps.value !== prevState.prevValue) {
return {
prevValue: prevState.value,
value: nextProps.value
};
}
return null;
}
Copy link
Collaborator

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.

Copy link
Collaborator Author

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;
Copy link
Collaborator

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.

Copy link
Collaborator Author

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.

@Inbal-Tish
Copy link
Collaborator Author

@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.

@Inbal-Tish Inbal-Tish requested a review from ethanshar October 27, 2020 07:41
…hub.com:wix/react-native-ui-lib into fix/DateTimePicker_to_controlled

# Conflicts:
#	src/components/dateTimePicker/index.js
@ethanshar ethanshar merged commit 800483c into master Nov 16, 2020
@Inbal-Tish Inbal-Tish deleted the fix/DateTimePicker_to_controlled branch November 17, 2020 08:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants