Skip to content

Commit

Permalink
fix(DateInput): use zero values with keyboard input (#7872)
Browse files Browse the repository at this point in the history
* fix(DateInput): use zero values with keyboard input

* fix: use date-fns functions
  • Loading branch information
BlackySoul committed Nov 7, 2024
1 parent a8f0e1a commit 13ccfb3
Show file tree
Hide file tree
Showing 2 changed files with 24 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['DateInput__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
6 changes: 5 additions & 1 deletion packages/vkui/src/components/DateInput/DateInput.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react';
import { Icon16Clear, Icon20CalendarOutline } from '@vkontakte/icons';
import { classNames } from '@vkontakte/vkjs';
import { startOfDay, startOfMinute } from 'date-fns';
import { useAdaptivity } from '../../hooks/useAdaptivity';
import { useDateInput } from '../../hooks/useDateInput';
import { useExternRef } from '../../hooks/useExternRef';
Expand Down Expand Up @@ -175,7 +176,10 @@ export const DateInput = ({
}

if (isMatch(formattedValue, mask)) {
onChange?.(parse(formattedValue, mask, value ?? new Date()));
const now = new Date();
onChange?.(
parse(formattedValue, mask, value ?? (enableTime ? startOfMinute(now) : startOfDay(now))),
);
}
},
[enableTime, maxElement, onChange, value],
Expand Down

0 comments on commit 13ccfb3

Please sign in to comment.