Skip to content

Commit

Permalink
[TimePicker] Handle Space and Enter on TimeClock component (#14151
Browse files Browse the repository at this point in the history
)

Co-authored-by: Arthur Balduini <arthurbalduini@Arthurs-MacBook-Pro.local>
  • Loading branch information
arthurbalduini and Arthur Balduini authored Aug 22, 2024
1 parent c94b26d commit a270b3d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/x-date-pickers/src/TimeClock/Clock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,11 @@ export function Clock<TDate extends PickerValidDate>(inProps: ClockProps<TDate>)
handleValueChange(viewValue - keyboardControlStep, 'partial');
event.preventDefault();
break;
case 'Enter':
case ' ':
handleValueChange(viewValue, 'finish');
event.preventDefault();
break;
default:
// do nothing
}
Expand Down
41 changes: 41 additions & 0 deletions packages/x-date-pickers/src/TimeClock/tests/TimeClock.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,47 @@ describe('<TimeClock />', () => {
expect(reason).to.equal('partial');
});

[
{
keyName: 'Enter',
keyValue: 'Enter',
},
{
keyName: 'Space',
keyValue: ' ',
},
].forEach(({ keyName, keyValue }) => {
it(`sets value on ${keyName} press`, () => {
const handleChange = spy();
render(
<TimeClock
autoFocus
defaultValue={adapterToUse.date('2019-01-01T04:20:00')}
onChange={handleChange}
/>,
);
const listbox = screen.getByRole('listbox');

fireEvent.keyDown(listbox, { key: 'ArrowDown' });
fireEvent.keyDown(listbox, { key: keyValue });

expect(handleChange.callCount).to.equal(2);
let [newDate, reason] = handleChange.lastCall.args;

expect(adapterToUse.getHours(newDate)).to.equal(3);
expect(reason).to.equal('partial');

fireEvent.keyDown(listbox, { key: 'ArrowUp' });
fireEvent.keyDown(listbox, { key: keyValue });

expect(handleChange.callCount).to.equal(4);
[newDate, reason] = handleChange.lastCall.args;

expect(adapterToUse.getMinutes(newDate)).to.equal(21);
expect(reason).to.equal('finish');
});
});

it('should display options, but not update value when readOnly prop is passed', function test() {
// Only run in supported browsers
if (typeof Touch === 'undefined') {
Expand Down

0 comments on commit a270b3d

Please sign in to comment.