Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

patch: pr7872 #7913

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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