diff --git a/src/components/Input/DateInput.js b/src/components/Input/DateInput.js
index 76c1db1d3..e2e4b4209 100644
--- a/src/components/Input/DateInput.js
+++ b/src/components/Input/DateInput.js
@@ -250,7 +250,12 @@ export default class DateInput extends React.Component {
componentDidUpdate(prevProps, prevState) {
this.setInputValue();
if (this.props.onClose && this.state.open !== prevState.open && !this.state.open) {
- const value = this.props.value !== undefined ? this.props.value : this.state.value;
+ // A pick updates state.value in the same update that closes the picker, before the
+ // parent has re-rendered with the new value prop, so prefer it when it just changed.
+ const value =
+ this.props.value === undefined || this.state.value !== prevState.value
+ ? this.state.value
+ : this.props.value;
const date = this.props.parse(value, this.props.dateFormat);
if (date) {
@@ -281,6 +286,7 @@ export default class DateInput extends React.Component {
locale,
onBlur,
onChange,
+ onClose,
parse,
positionFixed,
value,
diff --git a/src/components/Input/DateInput.spec.js b/src/components/Input/DateInput.spec.js
index 7ee4c9c2a..73b44c09b 100644
--- a/src/components/Input/DateInput.spec.js
+++ b/src/components/Input/DateInput.spec.js
@@ -529,6 +529,63 @@ describe('', () => {
});
});
+ describe('onClose', () => {
+ function ControlledDateInput(props) {
+ const [value, setValue] = React.useState('1/15/2020');
+ return setValue(v)} {...props} />;
+ }
+
+ it('should call onClose with the picked date when uncontrolled', () => {
+ const onClose = sinon.spy();
+ const component = mount();
+ component.find('input').simulate('focus');
+
+ const day = component.find('Day').first();
+ const expectedDate = day.props().day.date;
+ day.simulate('click');
+
+ sinon.assert.calledOnce(onClose);
+ assert(isSameDay(onClose.firstCall.args[0], expectedDate));
+ assert.equal(onClose.firstCall.args[1], true);
+ });
+
+ it('should call onClose with the picked date when controlled', () => {
+ const onClose = sinon.spy();
+ const component = mount();
+ component.find('input').simulate('focus');
+
+ const day = component.find('Day').first();
+ const expectedDate = day.props().day.date;
+ day.simulate('click');
+
+ sinon.assert.calledOnce(onClose);
+ assert(isSameDay(onClose.firstCall.args[0], expectedDate));
+ assert.equal(onClose.firstCall.args[1], true);
+ });
+
+ it('should call onClose with the current value prop when closed without picking', () => {
+ const onClose = sinon.spy();
+ const component = mount();
+ const input = component.find('input');
+
+ input.simulate('focus');
+ component.find('Day').first().simulate('click');
+ component.setProps({ value: '3/4/2021' });
+ onClose.resetHistory();
+
+ input.simulate('focus');
+ input.simulate('keydown', { key: 'Esc', keyCode: 27, which: 27 });
+
+ sinon.assert.calledOnce(onClose);
+ assert(isSameDay(onClose.firstCall.args[0], new Date(2021, 2, 4)));
+ });
+
+ it('should not pass onClose through to the input', () => {
+ const component = mount( {}} />);
+ assert.equal(component.find('input').prop('onClose'), undefined);
+ });
+ });
+
it('should call custom parse function', () => {
const callback = sinon.spy(() => new Date(2003, 0, 2));
mount();