From 4dfe5fd2c066e394fa637c7d6eb82b8db105f29a Mon Sep 17 00:00:00 2001 From: Lauren Baar Date: Thu, 15 Oct 2026 15:21:55 -0700 Subject: [PATCH] fix(DateInput): pass the picked date to onClose when controlled Picking a day sets state.value and closes the picker in the same update, before the parent re-renders with the new value prop. onClose read the value prop whenever it was defined, so controlled callers always got the pre-selection value. Prefer state.value when it changed in the closing update; otherwise keep using the value prop so external changes still win. Also stop spreading onClose onto the as an unknown DOM prop. Co-Authored-By: Claude Opus 5.5 --- src/components/Input/DateInput.js | 8 +++- src/components/Input/DateInput.spec.js | 57 ++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) 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();