-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
index.ts
106 lines (87 loc) · 2.67 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
/* eslint-disable class-methods-use-this */
import defaultMoment, { LongDateFormatKey } from 'moment';
import BaseAdapterMoment from '@date-io/moment';
import { FieldFormatTokenMap, MuiPickersAdapter } from '../models';
// From https://momentjs.com/docs/#/displaying/format/
const formatTokenMap: FieldFormatTokenMap = {
// Year
Y: '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
E: 'weekDay',
e: 'weekDay',
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',
k: 'hours',
kk: 'hours',
// Minutes
m: 'minutes',
mm: 'minutes',
// Seconds
s: 'seconds',
ss: 'seconds',
};
export class AdapterMoment
extends BaseAdapterMoment
implements MuiPickersAdapter<defaultMoment.Moment>
{
public isMUIAdapter = true;
public formatTokenMap = formatTokenMap;
public escapedCharacters = { start: '[', end: ']' };
/**
* 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) => {
// @see https://github.com/moment/moment/blob/develop/src/lib/format/format.js#L6
const localFormattingTokens = /(\[[^[]*\])|(\\)?(LTS|LT|LL?L?L?|l{1,4})|./g;
return format
.match(localFormattingTokens)!
.map((token) => {
const firstCharacter = token[0];
if (firstCharacter === 'L' || firstCharacter === ';') {
return defaultMoment
.localeData(this.getCurrentLocaleCode())
.longDateFormat(token as LongDateFormatKey);
}
return token;
})
.join('');
};
public getCurrentLocaleCode = () => {
return this.locale || defaultMoment.locale();
};
// 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: defaultMoment.Moment) => {
return date.week();
};
public getWeekdays = () => {
return defaultMoment.weekdaysShort(true);
};
public is12HourCycleInCurrentLocale = () => {
return /A|a/.test(defaultMoment.localeData(this.getCurrentLocaleCode()).longDateFormat('LT'));
};
}