Skip to content

Commit 6c7983a

Browse files
scottpchow23claude
andcommitted
fix: clear DateInput when controlled value prop becomes undefined
When a controlled DateInput had its value prop change from a date string to undefined, the input visually retained the old date. This adds reset logic in componentDidUpdate to clear both internal state and the DOM element when the value transitions from truthy to falsy. OWNER-221 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 1ea3fdc commit 6c7983a

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

src/components/Input/DateInput.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,13 @@ export default class DateInput extends React.Component {
248248
}
249249

250250
componentDidUpdate(prevProps, prevState) {
251+
if (prevProps.value && !this.props.value) {
252+
this.setState({ value: '' });
253+
if (this.inputEl) {
254+
this.inputEl.value = '';
255+
this.inputEl.setAttribute('value', '');
256+
}
257+
}
251258
this.setInputValue();
252259
if (this.props.onClose && this.state.open !== prevState.open && !this.state.open) {
253260
const value = this.props.value !== undefined ? this.props.value : this.state.value;

src/components/Input/DateInput.spec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,28 @@ describe('<DateInput />', () => {
560560
});
561561
});
562562

563+
describe('controlled value', () => {
564+
it('should clear displayed value when value prop changes to undefined', () => {
565+
const component = mount(<DateInput value="1/1/2025" />);
566+
const input = component.find('input');
567+
assert.equal(input.getDOMNode().value, '1/1/2025');
568+
569+
component.setProps({ value: undefined });
570+
component.update();
571+
assert.equal(input.getDOMNode().value, '');
572+
});
573+
574+
it('should clear displayed value when value prop changes to empty string', () => {
575+
const component = mount(<DateInput value="1/1/2025" />);
576+
const input = component.find('input');
577+
assert.equal(input.getDOMNode().value, '1/1/2025');
578+
579+
component.setProps({ value: '' });
580+
component.update();
581+
assert.equal(input.getDOMNode().value, '');
582+
});
583+
});
584+
563585
describe('accessibility', () => {
564586
it('should contain screen reader only label for buttons', () => {
565587
const component = mount(<DateInput />);

0 commit comments

Comments
 (0)