From 3e69bfc4ca46881ea7751473b0c92792dcb93dd0 Mon Sep 17 00:00:00 2001 From: Victoria Zhizhonkova Date: Tue, 29 Oct 2024 21:16:42 +0700 Subject: [PATCH] fix(DateInput): use zero values with keyboard input --- .../components/DateInput/DateInput.test.tsx | 21 +++++++++++++++++-- .../src/components/DateInput/DateInput.tsx | 9 +++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/packages/vkui/src/components/DateInput/DateInput.test.tsx b/packages/vkui/src/components/DateInput/DateInput.test.tsx index fe40d79358..6f117b2588 100644 --- a/packages/vkui/src/components/DateInput/DateInput.test.tsx +++ b/packages/vkui/src/components/DateInput/DateInput.test.tsx @@ -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]; @@ -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(); + 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 () => { diff --git a/packages/vkui/src/components/DateInput/DateInput.tsx b/packages/vkui/src/components/DateInput/DateInput.tsx index a3b8f5447c..d0f39585b2 100644 --- a/packages/vkui/src/components/DateInput/DateInput.tsx +++ b/packages/vkui/src/components/DateInput/DateInput.tsx @@ -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],