Skip to content

Commit

Permalink
fix(DateInput): use zero values with keyboard input
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackySoul committed Oct 29, 2024
1 parent e44a13f commit 3e69bfc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
21 changes: 19 additions & 2 deletions packages/vkui/src/components/DateInput/DateInput.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DateInput } from './DateInput';
import styles from './DateInput.module.css';
import inputLikeStyles from '../InputLike/InputLike.module.css';

const date = new Date(2024, 6, 31, 11, 20);
const date = new Date(2024, 6, 31, 11, 20, 0, 0);

const getInputsLike = (container: HTMLElement) => {
const dateInput = container.getElementsByClassName(styles.input)[0];
Expand Down Expand Up @@ -90,7 +90,24 @@ describe('DateInput', () => {
const normalizedDate = convertInputsToNumbers(inputLikes);
expect(normalizedDate).toEqual([30, 6, 2023, 15, 40]);

expect(onChange).toBeCalledTimes(5);
expect(onChange).toHaveBeenCalledTimes(5);
expect(onChange).toHaveBeenCalledWith(new Date(2023, 5, 30, 15, 40, 0, 0));
});

it('should call onChange with zero sec/ms', async () => {
jest.useFakeTimers();
const onChange = jest.fn();
const { container } = render(<DateInput value={undefined} onChange={onChange} />);
const inputLikes = getInputsLike(container);

const [dates, months, years] = inputLikes;

await userEvent.type(dates, '30');
await userEvent.type(months, '06');
await userEvent.type(years, '2023');

expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange).toHaveBeenCalledWith(new Date(2023, 5, 30, 0, 0, 0, 0));
});

it('should call onChange callback when change data by calendar', async () => {
Expand Down
9 changes: 8 additions & 1 deletion packages/vkui/src/components/DateInput/DateInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,14 @@ export const DateInput = ({
}

if (isMatch(formattedValue, mask)) {
onChange?.(parse(formattedValue, mask, value ?? new Date()));
onChange?.(
parse(
formattedValue,
mask,
value ??
new Date(enableTime ? new Date().setSeconds(0, 0) : new Date().setHours(0, 0, 0, 0)),
),
);
}
},
[enableTime, maxElement, onChange, value],
Expand Down

0 comments on commit 3e69bfc

Please sign in to comment.