Skip to content

Commit

Permalink
Remove DatePicker temporary values [fix ant-design#5189]
Browse files Browse the repository at this point in the history
  • Loading branch information
megawac committed Mar 6, 2017
1 parent 1c38cad commit 540292f
Showing 1 changed file with 11 additions and 22 deletions.
33 changes: 11 additions & 22 deletions components/date-picker/createPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,13 @@ export default function createPicker(TheCalendar) {
}
return {
value,
tempValue: undefined,
};
},

componentWillReceiveProps(nextProps: PickerProps) {
if ('value' in nextProps) {
this.setState({
value: nextProps.value,
tempValue: nextProps.value,
});
}
},
Expand All @@ -66,34 +64,25 @@ export default function createPicker(TheCalendar) {
props.onChange(value, (value && value.format(props.format)) || '');
},

handleTempChange(tempValue) {
this.setState({ tempValue });
handleTempChange(value) {
const props = this.props;
if (!('value' in props)) {
this.setState({ value });
}
props.onChange(value, (value && value.format(props.format)) || '');
},

// Clear temp value and trigger onChange when hide DatePicker[showTime] panel
// Trigger onChange when hide DatePicker[showTime] panel
handleOpenChange(open) {
const { showTime, onOpenChange, onChange, format } = this.props;
if (!open) {
// tricky code to avoid triggering onChange multiple times
// when click `Now` button
let tempValue;
this.setState(prevState => {
tempValue = prevState.tempValue;
const nextState = { tempValue: undefined } as any;
if (showTime && tempValue) {
nextState.value = tempValue;
onChange(tempValue, (tempValue && tempValue.format(format)) || '');
}
return nextState;
});
}
const { onOpenChange } = this.props;

if (onOpenChange) {
onOpenChange(open);
}
},

render() {
const { value, tempValue } = this.state;
const { value } = this.state;
const props = omit(this.props, ['onChange']);
const { prefixCls, locale } = props;

Expand Down Expand Up @@ -169,7 +158,7 @@ export default function createPicker(TheCalendar) {
</span>
);

const pickerValue = tempValue || value;
const pickerValue = value;
const localeCode = getLocaleCode(this.context);
if (pickerValue && localeCode) {
pickerValue.locale(localeCode);
Expand Down

0 comments on commit 540292f

Please sign in to comment.