-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathmisc.ts
72 lines (67 loc) · 2.31 KB
/
misc.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import sinon from 'sinon';
import { MuiPickersAdapter, PickerValidDate } from '@mui/x-date-pickers/models';
import { PickerComponentFamily } from './describe.types';
import { OpenPickerParams } from './openPicker';
export const stubMatchMedia = (matches = true) =>
sinon.stub().returns({
matches,
addListener: () => {},
removeListener: () => {},
});
const getChangeCountForComponentFamily = (componentFamily: PickerComponentFamily) => {
switch (componentFamily) {
case 'clock':
case 'multi-section-digital-clock':
return 3;
default:
return 1;
}
};
export const getExpectedOnChangeCount = (
componentFamily: PickerComponentFamily,
params: OpenPickerParams,
) => {
if (componentFamily === 'digital-clock') {
return getChangeCountForComponentFamily(componentFamily);
}
if (params.type === 'date-time') {
return (
getChangeCountForComponentFamily(componentFamily) +
getChangeCountForComponentFamily(
params.variant === 'desktop' ? 'multi-section-digital-clock' : 'clock',
)
);
}
if (componentFamily === 'picker' && params.type === 'time') {
return getChangeCountForComponentFamily(
params.variant === 'desktop' ? 'multi-section-digital-clock' : 'clock',
);
}
if (componentFamily === 'picker' && params.type === 'date-time-range') {
return (
getChangeCountForComponentFamily(componentFamily) +
getChangeCountForComponentFamily('multi-section-digital-clock')
);
}
if (componentFamily === 'clock') {
// the `TimeClock` fires change for both touch move and touch end
// but does not have meridiem control
return (getChangeCountForComponentFamily(componentFamily) - 1) * 2;
}
return getChangeCountForComponentFamily(componentFamily);
};
export const getDateOffset = <TDate extends PickerValidDate>(
adapter: MuiPickersAdapter<TDate>,
date: TDate,
) => {
const utcHour = adapter.getHours(adapter.setTimezone(adapter.startOfDay(date), 'UTC'));
const cleanUtcHour = utcHour > 12 ? 24 - utcHour : -utcHour;
return cleanUtcHour * 60;
};
export const formatFullTimeValue = <TDate extends PickerValidDate>(
adapter: MuiPickersAdapter<TDate>,
value: TDate,
) => {
const hasMeridiem = adapter.is12HourCycleInCurrentLocale();
return adapter.format(value, hasMeridiem ? 'fullTime12h' : 'fullTime24h');
};