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

[@mantine/dates] AmPmInput: Fix onChange being called twice #1322

Merged
merged 1 commit into from
Apr 25, 2022
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react';
import { render } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { AmPmInput } from './AmPmInput';

describe('@mantine/dates/TimeInputBase/AmPmInput', () => {
it.each([['a'], ['p'], ['{arrowdown}']])('triggers onChange once for %s', (value) => {
const spy = jest.fn();
const { container } = render(<AmPmInput onChange={spy} size="md" />);
userEvent.type(container.querySelector('input'), value);
expect(spy).toHaveBeenCalledTimes(1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ export const AmPmInput = forwardRef<HTMLInputElement, AmPmSelectProps>(
}

if (event.key === 'p' || event.nativeEvent.code === 'KeyP') {
onChange('pm', false);
event.preventDefault();
onChange('pm', true);
}

if (event.key === 'a' || event.nativeEvent.code === 'KeyA') {
onChange('am', false);
event.preventDefault();
onChange('am', true);
}
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import dayjs from 'dayjs';
import React from 'react';
import { Container } from '@mantine/core';
import { TimeRangeInput } from '@mantine/dates';

const code = `
import dayjs from 'dayjs';
import { TimeRangeInput } from '@mantine/dates';

function Demo() {
return (
<TimeRangeInput
label="Appointment time"
format="12"
defaultValue={[new Date(), dayjs(new Date()).add(30, 'minutes').toDate()]}
/>
);
}
`;

function Demo() {
return (
<Container size={340}>
<TimeRangeInput
label="Appointment time"
format="12"
defaultValue={[new Date(), dayjs(new Date()).add(30, 'minutes').toDate()]}
/>
</Container>
);
}

export const format: MantineDemo = {
type: 'demo',
code,
component: Demo,
};
1 change: 1 addition & 0 deletions src/mantine-demos/src/demos/dates/TimeRangeInput/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { usage } from './TimeRangeInput.demo.usage';
export { withSeconds } from './TimeRangeInput.demo.withSeconds';
export { format } from './TimeRangeInput.demo.format';