forked from mui/mui-x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
129 lines (105 loc) · 3.49 KB
/
index.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* eslint-disable class-methods-use-this */
import defaultDayjs, { Dayjs } from 'dayjs';
import weekOfYear from 'dayjs/plugin/weekOfYear';
import BaseAdapterDayjs from '@date-io/dayjs';
import { FieldFormatTokenMap, MuiPickersAdapter, AdapterFormats } from '../models';
import { buildWarning } from '../internals/utils/warning';
const localeNotFoundWarning = buildWarning([
'Your locale has not been found.',
'Either the locale key is not a supported one. Locales supported by dayjs are available here: https://github.com/iamkun/dayjs/tree/dev/src/locale',
"Or you forget to import the locale with `require('dayjs/locale/{localeUsed}')`",
'fallback on English locale',
]);
const formatTokenMap: FieldFormatTokenMap = {
// Year
YY: 'year',
YYYY: 'year',
// Month
M: 'month',
MM: 'month',
MMM: { sectionType: 'month', contentType: 'letter' },
MMMM: { sectionType: 'month', contentType: 'letter' },
// Day of the month
D: 'day',
DD: 'day',
Do: 'day',
// Day of the week
d: 'weekDay',
dd: { sectionType: 'weekDay', contentType: 'letter' },
ddd: { sectionType: 'weekDay', contentType: 'letter' },
dddd: { sectionType: 'weekDay', contentType: 'letter' },
// Meridiem
A: 'meridiem',
a: 'meridiem',
// Hours
H: 'hours',
HH: 'hours',
h: 'hours',
hh: 'hours',
// Minutes
m: 'minutes',
mm: 'minutes',
// Seconds
s: 'seconds',
ss: 'seconds',
};
interface Opts {
locale?: string;
/** Make sure that your dayjs instance extends customParseFormat and advancedFormat */
instance?: typeof defaultDayjs;
formats?: Partial<AdapterFormats>;
}
export class AdapterDayjs extends BaseAdapterDayjs implements MuiPickersAdapter<Dayjs> {
public isMUIAdapter = true;
constructor(options: Opts) {
super(options);
defaultDayjs.extend(weekOfYear);
}
public formatTokenMap = formatTokenMap;
public escapedCharacters = { start: '[', end: ']' };
private getLocaleFormats = () => {
const locales = this.rawDayJsInstance.Ls ?? defaultDayjs.Ls;
const locale = this.locale || 'en';
let localeObject = locales[locale];
if (localeObject === undefined) {
localeNotFoundWarning();
localeObject = locales.en;
}
return localeObject.formats;
};
public is12HourCycleInCurrentLocale = () => {
/* istanbul ignore next */
return /A|a/.test(this.getLocaleFormats().LT || '');
};
/**
* The current getFormatHelperText method uses an outdated format parsing logic.
* We should use this one in the future to support all localized formats.
*/
public expandFormat = (format: string) => {
const localeFormats = this.getLocaleFormats();
// @see https://github.com/iamkun/dayjs/blob/dev/src/plugin/localizedFormat/index.js
const t = (formatBis: string) =>
formatBis.replace(
/(\[[^\]]+])|(MMMM|MM|DD|dddd)/g,
(_: string, a: string, b: string) => a || b.slice(1),
);
return format.replace(
/(\[[^\]]+])|(LTS?|l{1,4}|L{1,4})/g,
(_: string, a: string, b: string) => {
const B = b && b.toUpperCase();
return (
a ||
localeFormats[b as keyof typeof localeFormats] ||
t(localeFormats[B as keyof typeof localeFormats] as string)
);
},
);
};
// Redefined here just to show how it can be written using expandFormat
public getFormatHelperText = (format: string) => {
return this.expandFormat(format).replace(/a/gi, '(a|p)m').toLocaleLowerCase();
};
public getWeekNumber = (date: Dayjs) => {
return date.week();
};
}