-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Refactor date picker to be compatible with form #7627
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
c3e298b
6cf0b80
f4ef1eb
37c8733
6020b72
0bcf38e
e20c7da
82f8c87
906d7f4
345f711
78d1897
9b6f0f9
19f4a6b
8d3d32c
0021a62
77c9d1e
d9b4a82
bd46454
9a7e686
beaf471
5e16990
faefd21
6e4f5b7
5653c91
cef12cc
895b204
e048d09
b40bc70
b5c8e8c
75df8d5
74ccc9e
c33917c
304c08d
d833d3d
e06dba3
12af0d6
f9e0147
6115c67
0f711eb
d50f8ab
a01b40e
e95bd28
6b53090
0b886e8
4959d11
83dfe08
3a2153d
fd0f219
b623066
1fa8dcc
87ee30a
a386321
f266233
47d7542
f38e10d
92a58da
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 |
|---|---|---|
|
|
@@ -4,60 +4,80 @@ import moment from 'moment'; | |
| import TextInput from '../TextInput'; | ||
| import CONST from '../../CONST'; | ||
| import {propTypes, defaultProps} from './datepickerPropTypes'; | ||
| import DateUtils from '../../libs/DateUtils'; | ||
|
|
||
| class DatePicker extends React.Component { | ||
| constructor(props) { | ||
| super(props); | ||
|
|
||
| this.state = { | ||
| isPickerVisible: false, | ||
| selectedDate: props.value || props.defaultValue, | ||
| }; | ||
|
|
||
| this.showPicker = this.showPicker.bind(this); | ||
| this.raiseDateChange = this.raiseDateChange.bind(this); | ||
| this.setDate = this.setDate.bind(this); | ||
| } | ||
|
|
||
| /** | ||
| * @param {Event} event | ||
| */ | ||
| showPicker(event) { | ||
| this.setState({isPickerVisible: true}); | ||
| event.preventDefault(); | ||
| componentDidUpdate() { | ||
| const dateValue = DateUtils.getDateAsText(this.props.value); | ||
| if (this.props.value === undefined || DateUtils.getDateAsText(this.state.selectedDate) === dateValue) { | ||
| return; | ||
| } | ||
| // eslint-disable-next-line react/no-did-update-set-state | ||
| this.setState({selectedDate: this.props.value}); | ||
| this.textInput.setNativeProps({text: dateValue}); | ||
|
luacmartins marked this conversation as resolved.
|
||
| } | ||
|
|
||
| /** | ||
| * @param {Event} event | ||
| * @param {Date} selectedDate | ||
| */ | ||
| raiseDateChange(event, selectedDate) { | ||
| setDate(event, selectedDate) { | ||
| this.setState({isPickerVisible: false}); | ||
|
luacmartins marked this conversation as resolved.
|
||
| if (event.type === 'set') { | ||
| this.props.onChange(selectedDate); | ||
|
Contributor
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. we renamed
Contributor
Author
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. Sure thing! I will update it. |
||
| } | ||
| this.setState({selectedDate}); | ||
|
|
||
| this.setState({isPickerVisible: false}); | ||
| // Updates the value of TextInput on Date Change | ||
| this.textInput.setNativeProps({text: DateUtils.getDateAsText(selectedDate)}); | ||
| } | ||
|
|
||
| render() { | ||
| const dateAsText = this.props.value ? moment(this.props.value).format(CONST.DATE.MOMENT_FORMAT_STRING) : ''; | ||
| /** | ||
| * @param {Event} event | ||
| */ | ||
| showPicker(event) { | ||
| this.setState({isPickerVisible: true}); | ||
| event.preventDefault(); | ||
| } | ||
|
|
||
| render() { | ||
| return ( | ||
| <> | ||
| <TextInput | ||
| label={this.props.label} | ||
| value={dateAsText} | ||
| ref={(el) => { | ||
| this.textInput = el; | ||
| if (typeof this.props.forwardRef === 'function') { | ||
| this.props.forwardedRef(el); | ||
| } | ||
| }} | ||
| defaultValue={DateUtils.getDateAsText(this.state.selectedDate) || CONST.DATE.MOMENT_FORMAT_STRING} | ||
| placeholder={this.props.placeholder} | ||
| hasError={this.props.hasError} | ||
| errorText={this.props.errorText} | ||
| containerStyles={this.props.containerStyles} | ||
| onPress={this.showPicker} | ||
| editable={false} | ||
| disabled={this.props.disabled} | ||
| onBlur={this.props.onBlur} | ||
| shouldSaveDraft={this.props.shouldSaveDraft} | ||
| isFormInput={this.props.isFormInput} | ||
| inputID={this.props.inputID} | ||
| /> | ||
| {this.state.isPickerVisible && ( | ||
| <RNDatePicker | ||
| value={this.props.value ? moment(this.props.value).toDate() : new Date()} | ||
| value={this.state.selectedDate ? moment(this.state.selectedDate).toDate() : new Date()} | ||
| mode="date" | ||
| onChange={this.raiseDateChange} | ||
| onChange={this.setDate} | ||
| maximumDate={this.props.maximumDate} | ||
| /> | ||
| )} | ||
|
|
@@ -69,4 +89,7 @@ class DatePicker extends React.Component { | |
| DatePicker.propTypes = propTypes; | ||
| DatePicker.defaultProps = defaultProps; | ||
|
|
||
| export default DatePicker; | ||
| export default (React.forwardRef((props, ref) => ( | ||
| /* eslint-disable-next-line react/jsx-props-no-spreading */ | ||
| <DatePicker {...props} forwardedRef={ref} /> | ||
| ))); | ||
Uh oh!
There was an error while loading. Please reload this page.