Skip to content

Commit

Permalink
fix(DatePicker,DateRangePicker): fix the date change when the input d…
Browse files Browse the repository at this point in the history
…ate is incomplete (#3719)
  • Loading branch information
simonguo committed Apr 3, 2024
1 parent f044445 commit 24f162f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/DateInput/DateField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ export const useDateField = (format: string, localize: Locale['localize'], date?
return null;
}

// Invalid Date
return new Date('');
} else if (type === 'day' && value === 0) {
// Invalid Date. If the type is day and the value is 0, it is considered an invalid date.
return new Date('');
}

Expand Down
20 changes: 19 additions & 1 deletion src/DateInput/test/DateInputSpec.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { format } from 'date-fns';
import { format, isValid } from 'date-fns';
import { testStandardProps, testControlledUnControlled, testFormControl } from '@test/utils';
import { render, screen, fireEvent } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
Expand Down Expand Up @@ -130,6 +130,24 @@ describe('DateInput', () => {
expect(screen.getByRole('textbox')).to.have.value('20 11月 2023');
});

// fix: #3715
it('Should return invalid value in `onChange` callback', () => {
const onChange = sinon.spy();

render(<DateInput onChange={onChange} format="dd" />);

const input = screen.getByRole('textbox');

userEvent.click(input);
userEvent.keyboard('0');

expect(isValid(onChange.lastCall.firstArg)).to.be.false;

userEvent.keyboard('05');

expect(isValid(onChange.lastCall.firstArg)).to.be.true;
});

describe('DateInput - KeyPress', () => {
it('Should increase year when pressing ArrowUp ', () => {
testKeyPress({
Expand Down

0 comments on commit 24f162f

Please sign in to comment.