-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,19 @@ | ||
import _ from 'lodash'; | ||
import PropTypes from 'prop-types'; | ||
import moment from 'moment'; | ||
import React from 'react'; | ||
import React, {Component} from 'react'; | ||
import {StyleSheet} from 'react-native'; | ||
import RNDateTimePicker from '@react-native-community/datetimepicker'; | ||
import {Constants} from '../../helpers'; | ||
import {Colors} from '../../style'; | ||
import Assets from '../../assets'; | ||
import {BaseComponent} from '../../commons'; | ||
import {asBaseComponent} from '../../commons'; | ||
import {TextField} from '../inputs'; | ||
import Dialog from '../dialog'; | ||
import View from '../view'; | ||
import Button from '../button'; | ||
|
||
|
||
const MODES = { | ||
DATE: 'date', | ||
TIME: 'time' | ||
|
@@ -28,8 +29,9 @@ const MODES = { | |
*/ | ||
/*eslint-enable*/ | ||
|
||
class DateTimePicker extends BaseComponent { | ||
class DateTimePicker extends Component { | ||
static displayName = 'DateTimePicker'; | ||
|
||
static propTypes = { | ||
...TextField.propTypes, | ||
/** | ||
|
@@ -98,17 +100,23 @@ class DateTimePicker extends BaseComponent { | |
constructor(props) { | ||
super(props); | ||
|
||
const initialValue = props.value; | ||
this.chosenDate = initialValue; | ||
this.chosenDate = props.value; | ||
|
||
this.state = { | ||
showExpandableOverlay: false, | ||
value: initialValue | ||
prevValue: props.value, | ||
value: props.value | ||
}; | ||
} | ||
|
||
generateStyles() { | ||
this.styles = createStyles(this.props); | ||
static getDerivedStateFromProps(nextProps, prevState) { | ||
if (nextProps.value !== prevState.prevValue) { | ||
return { | ||
prevValue: prevState.value, | ||
value: nextProps.value | ||
}; | ||
} | ||
return null; | ||
} | ||
|
||
handleChange = (event = {}, date) => { | ||
|
@@ -141,14 +149,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 commentThe 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 commentThe 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. |
||
const {mode, dateFormat, timeFormat, dateFormatter, timeFormatter} = this.getThemeProps(); | ||
const {mode, dateFormat, timeFormat, dateFormatter, timeFormatter} = this.props; | ||
if (value) { | ||
switch (mode) { | ||
case MODES.DATE: | ||
|
@@ -168,7 +176,7 @@ class DateTimePicker extends BaseComponent { | |
}; | ||
|
||
renderExpandableOverlay = () => { | ||
const {testID, dialogProps} = this.getThemeProps(); | ||
const {testID, dialogProps} = this.props; | ||
const {showExpandableOverlay} = this.state; | ||
|
||
return ( | ||
|
@@ -180,7 +188,7 @@ class DateTimePicker extends BaseComponent { | |
bottom | ||
centerH | ||
onDismiss={this.toggleExpandableOverlay} | ||
containerStyle={this.styles.dialog} | ||
containerStyle={styles.dialog} | ||
testID={`${testID}.dialog`} | ||
supportedOrientations={['portrait', 'landscape', 'landscape-left', 'landscape-right']} // iOS only | ||
{...dialogProps} | ||
|
@@ -197,7 +205,7 @@ class DateTimePicker extends BaseComponent { | |
const {useCustomTheme} = this.props; | ||
|
||
return ( | ||
<View row spread bg-white paddingH-20 style={this.styles.header}> | ||
<View row spread bg-white paddingH-20 style={styles.header}> | ||
<Button | ||
link | ||
iconSource={Assets.icons.x} | ||
|
@@ -235,7 +243,7 @@ class DateTimePicker extends BaseComponent { | |
}; | ||
|
||
render() { | ||
const textInputProps = TextField.extractOwnProps(this.getThemeProps()); | ||
const textInputProps = TextField.extractOwnProps(this.props); | ||
|
||
return ( | ||
<TextField | ||
|
@@ -249,23 +257,19 @@ class DateTimePicker extends BaseComponent { | |
} | ||
} | ||
|
||
export default DateTimePicker; | ||
export {DateTimePicker}; // For tests | ||
export default asBaseComponent(DateTimePicker); | ||
|
||
function createStyles(props) { | ||
const borderRadius = 12; | ||
|
||
const styles = StyleSheet.create({ | ||
header: { | ||
height: 56, | ||
borderBottomWidth: 1, | ||
borderBottomColor: Colors.dark80 | ||
}, | ||
dialog: { | ||
backgroundColor: Colors.white, | ||
borderTopLeftRadius: borderRadius, | ||
borderTopRightRadius: borderRadius | ||
} | ||
}); | ||
|
||
return styles; | ||
} | ||
const styles = StyleSheet.create({ | ||
header: { | ||
height: 56, | ||
borderBottomWidth: 1, | ||
borderBottomColor: Colors.dark80 | ||
}, | ||
dialog: { | ||
backgroundColor: Colors.white, | ||
borderTopLeftRadius: 12, | ||
borderTopRightRadius: 12 | ||
} | ||
}); |
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