Skip to content

Commit

Permalink
[@mantine/dates] TimeInput: Fix am/pm input not working with keyboard…
Browse files Browse the repository at this point in the history
… events with React 18 (#1322)
  • Loading branch information
nrayburn-tech authored Apr 25, 2022
1 parent 707db1d commit 6cc79d8
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
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';

0 comments on commit 6cc79d8

Please sign in to comment.