time-picker
time-picker copied to clipboard
defaultValue props bug
There is a weird bug in the time picker. When I give some formatted moment object to default prop, the on change handler returns the default value not the changed value. For example if the default value is moment(this.props.defaultValue).format('YYYY-MM-DD')
and we changed the date in timepicker, it changes the display date in time picker but returns the default time provided .
defaultValue={moment(this.props.defaultValue).format('YYYY-MM-DD')} onChange = value => { console.log(value) }
You should use value
to control the component.
https://facebook.github.io/react/docs/forms.html#controlled-components
That also doesn't work
class CustomTimePickerModal extends React.Component {
constructor(props){
super(props);
this.state = { value: this.props.defaultValue };
}
onChange = value => {
this.setState({ value: value });
this.props.onChange(value)
}
render() {
return (
<div className={styles.timepicker}>
<TimePicker showSecond={false} onChange={this.onChange} clearText={'Close the Picker'} value={this.state.value} placeholder={`${moment().format('hh:mm')}` || `${moment().format('hh:mm')}`} />
</div>
);
}
}
The value should be a moment instance but moment().format('xx')
is not one.
That's how I am passing the value to the custom component defaultValue={moment(task.realizedStartTime)}
That's the moment object which is created
That formated value was passed to placeholder prop not to value
placeholder={
${moment().format('hh:mm')}||
${moment().format('hh:mm')}}
Could you provide a reproducible demo by forking this: https://codesandbox.io/s/mrq288569 ?
Here is it https://codesandbox.io/s/zqww68629p
As you can check it doesn't return the correct value in the console.
I think the issue was probably related to call back as I am getting the correct value in the component call back who is using the date picker.
@Mujaddadi I am facing same problem. did you fix this one?
@Rainpia I started using some other date picker. As you can see this issue is almost 2 years old and still open, I don't have hope that the developer has time to fix it.
For me using moment(offStartTime, 'HH:mm')
instead of moment(offStartTime).format('HH:mm')
did the trick