Skip to content

Commit 1be2f4c

Browse files
arthurbalduiniArthur Balduini
and
Arthur Balduini
authored
[pickers] Add onTouchStart handler for TimeClock (#14305)
Co-authored-by: Arthur Balduini <[email protected]>
1 parent f1ab734 commit 1be2f4c

File tree

2 files changed

+59
-14
lines changed

2 files changed

+59
-14
lines changed

packages/x-date-pickers/src/TimeClock/Clock.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ export function Clock<TDate extends PickerValidDate>(inProps: ClockProps<TDate>)
261261
handleValueChange(newSelectedValue, isFinish);
262262
};
263263

264-
const handleTouchMove = (event: React.TouchEvent) => {
264+
const handleTouchSelection = (event: React.TouchEvent) => {
265265
isMoving.current = true;
266266
setTime(event, 'shallow');
267267
};
@@ -347,7 +347,8 @@ export function Clock<TDate extends PickerValidDate>(inProps: ClockProps<TDate>)
347347
<ClockClock className={classes.clock}>
348348
<ClockSquareMask
349349
data-mui-test="clock"
350-
onTouchMove={handleTouchMove}
350+
onTouchMove={handleTouchSelection}
351+
onTouchStart={handleTouchSelection}
351352
onTouchEnd={handleTouchEnd}
352353
onMouseUp={handleMouseUp}
353354
onMouseMove={handleMouseMove}

packages/x-date-pickers/src/TimeClock/tests/TimeClock.test.tsx

+56-12
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ describe('<TimeClock />', () => {
196196
const onChangeMock = spy();
197197
render(<TimeClock value={adapterToUse.date('2019-01-01')} onChange={onChangeMock} readOnly />);
198198

199-
fireTouchChangedEvent(screen.getByMuiTest('clock'), 'touchmove', selectEvent);
199+
fireTouchChangedEvent(screen.getByMuiTest('clock'), 'touchstart', selectEvent);
200200
expect(onChangeMock.callCount).to.equal(0);
201201

202202
// hours are not disabled
@@ -224,7 +224,7 @@ describe('<TimeClock />', () => {
224224
const onChangeMock = spy();
225225
render(<TimeClock value={adapterToUse.date('2019-01-01')} onChange={onChangeMock} disabled />);
226226

227-
fireTouchChangedEvent(screen.getByMuiTest('clock'), 'touchmove', selectEvent);
227+
fireTouchChangedEvent(screen.getByMuiTest('clock'), 'touchstart', selectEvent);
228228
expect(onChangeMock.callCount).to.equal(0);
229229

230230
// hours are disabled
@@ -252,7 +252,7 @@ describe('<TimeClock />', () => {
252252
},
253253
],
254254
},
255-
'20:--': {
255+
'19:--': {
256256
changedTouches: [
257257
{
258258
clientX: 66,
@@ -292,7 +292,7 @@ describe('<TimeClock />', () => {
292292
/>,
293293
);
294294

295-
fireTouchChangedEvent(screen.getByMuiTest('clock'), 'touchmove', clockTouchEvent['13:--']);
295+
fireTouchChangedEvent(screen.getByMuiTest('clock'), 'touchstart', clockTouchEvent['13:--']);
296296

297297
expect(handleChange.callCount).to.equal(1);
298298
const [date, selectionState] = handleChange.firstCall.args;
@@ -316,7 +316,7 @@ describe('<TimeClock />', () => {
316316
/>,
317317
);
318318

319-
fireTouchChangedEvent(screen.getByMuiTest('clock'), 'touchmove', clockTouchEvent['--:20']);
319+
fireTouchChangedEvent(screen.getByMuiTest('clock'), 'touchstart', clockTouchEvent['--:20']);
320320

321321
expect(handleChange.callCount).to.equal(1);
322322
const [date, selectionState] = handleChange.firstCall.args;
@@ -338,7 +338,7 @@ describe('<TimeClock />', () => {
338338
/>,
339339
);
340340

341-
fireTouchChangedEvent(screen.getByMuiTest('clock'), 'touchmove', clockTouchEvent['--:20']);
341+
fireTouchChangedEvent(screen.getByMuiTest('clock'), 'touchstart', clockTouchEvent['--:20']);
342342

343343
expect(handleChange.callCount).to.equal(0);
344344
});
@@ -356,7 +356,7 @@ describe('<TimeClock />', () => {
356356
/>,
357357
);
358358

359-
fireTouchChangedEvent(screen.getByMuiTest('clock'), 'touchmove', clockTouchEvent['--:20']);
359+
fireTouchChangedEvent(screen.getByMuiTest('clock'), 'touchstart', clockTouchEvent['--:20']);
360360

361361
expect(handleChange.callCount).to.equal(0);
362362
});
@@ -374,7 +374,7 @@ describe('<TimeClock />', () => {
374374
/>,
375375
);
376376

377-
fireTouchChangedEvent(screen.getByMuiTest('clock'), 'touchmove', clockTouchEvent['20:--']);
377+
fireTouchChangedEvent(screen.getByMuiTest('clock'), 'touchstart', clockTouchEvent['19:--']);
378378

379379
expect(handleChange.callCount).to.equal(0);
380380
});
@@ -392,7 +392,7 @@ describe('<TimeClock />', () => {
392392
/>,
393393
);
394394

395-
fireTouchChangedEvent(screen.getByMuiTest('clock'), 'touchmove', clockTouchEvent['20:--']);
395+
fireTouchChangedEvent(screen.getByMuiTest('clock'), 'touchstart', clockTouchEvent['19:--']);
396396

397397
expect(handleChange.callCount).to.equal(0);
398398
});
@@ -427,7 +427,7 @@ describe('<TimeClock />', () => {
427427
/>,
428428
);
429429

430-
fireTouchChangedEvent(screen.getByMuiTest('clock'), 'touchmove', clockTouchEvent['--:10']);
430+
fireTouchChangedEvent(screen.getByMuiTest('clock'), 'touchstart', clockTouchEvent['--:10']);
431431

432432
expect(handleChange.callCount).to.equal(1);
433433
const [date, selectionState] = handleChange.firstCall.args;
@@ -449,7 +449,7 @@ describe('<TimeClock />', () => {
449449
/>,
450450
);
451451

452-
fireTouchChangedEvent(screen.getByMuiTest('clock'), 'touchmove', clockTouchEvent['--:20']);
452+
fireTouchChangedEvent(screen.getByMuiTest('clock'), 'touchstart', clockTouchEvent['--:20']);
453453

454454
expect(handleChange.callCount).to.equal(0);
455455
});
@@ -467,10 +467,54 @@ describe('<TimeClock />', () => {
467467
/>,
468468
);
469469

470-
fireTouchChangedEvent(screen.getByMuiTest('clock'), 'touchmove', clockTouchEvent['--:20']);
470+
fireTouchChangedEvent(screen.getByMuiTest('clock'), 'touchstart', clockTouchEvent['--:20']);
471471

472472
expect(handleChange.callCount).to.equal(0);
473473
});
474+
475+
it('should select enabled hour on touch and drag', () => {
476+
const handleChange = spy();
477+
const handleViewChange = spy();
478+
render(
479+
<TimeClock
480+
ampm={false}
481+
value={adapterToUse.date('2018-01-01')}
482+
onChange={handleChange}
483+
onViewChange={handleViewChange}
484+
/>,
485+
);
486+
487+
fireTouchChangedEvent(screen.getByMuiTest('clock'), 'touchstart', clockTouchEvent['13:--']);
488+
fireTouchChangedEvent(screen.getByMuiTest('clock'), 'touchmove', clockTouchEvent['19:--']);
489+
490+
expect(handleChange.callCount).to.equal(2);
491+
const [date, selectionState] = handleChange.lastCall.args;
492+
expect(date).toEqualDateTime(new Date(2018, 0, 1, 19));
493+
expect(selectionState).to.equal('shallow');
494+
expect(handleViewChange.callCount).to.equal(0);
495+
});
496+
497+
it('should select enabled hour and move to next view on touch end', () => {
498+
const handleChange = spy();
499+
const handleViewChange = spy();
500+
render(
501+
<TimeClock
502+
ampm={false}
503+
value={adapterToUse.date('2018-01-01')}
504+
onChange={handleChange}
505+
onViewChange={handleViewChange}
506+
/>,
507+
);
508+
509+
fireTouchChangedEvent(screen.getByMuiTest('clock'), 'touchstart', clockTouchEvent['13:--']);
510+
fireTouchChangedEvent(screen.getByMuiTest('clock'), 'touchend', clockTouchEvent['13:--']);
511+
512+
expect(handleChange.callCount).to.equal(2);
513+
const [date, selectionState] = handleChange.lastCall.args;
514+
expect(date).toEqualDateTime(new Date(2018, 0, 1, 13));
515+
expect(selectionState).to.equal('partial');
516+
expect(handleViewChange.callCount).to.equal(1);
517+
});
474518
});
475519

476520
describe('default value', () => {

0 commit comments

Comments
 (0)