-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathDateField.tsx
157 lines (145 loc) · 4.9 KB
/
DateField.tsx
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import * as React from 'react';
import PropTypes from 'prop-types';
import get from 'lodash/get';
import { Typography, TypographyProps } from '@mui/material';
import { useRecordContext, useTranslate } from 'ra-core';
import { sanitizeFieldRestProps } from './sanitizeFieldRestProps';
import { FieldProps, fieldPropTypes } from './types';
import { genericMemo } from './genericMemo';
/**
* Display a date value as a locale string.
*
* Uses Intl.DateTimeFormat() if available, passing the locales and options props as arguments.
* If Intl is not available, it outputs date as is (and ignores the locales and options props).
*
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString
* @example
* <DateField source="published_at" />
* // renders the record { id: 1234, published_at: new Date('2012-11-07') } as
* <span>07/11/2012</span>
*
* <DateField source="published_at" className="red" />
* // renders the record { id: 1234, new Date('2012-11-07') } as
* <span class="red">07/11/2012</span>
*
* <DateField source="share" options={{ weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }} />
* // renders the record { id: 1234, new Date('2012-11-07') } as
* <span>Wednesday, November 7, 2012</span>
*
* <DateField source="price" locales="fr-FR" options={{ weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }} />
* // renders the record { id: 1234, new Date('2012-11-07') } as
* <span>mercredi 7 novembre 2012</span>
*/
const DateFieldImpl = <
RecordType extends Record<string, any> = Record<string, any>
>(
props: DateFieldProps<RecordType>
) => {
const {
className,
emptyText,
locales,
options,
showTime = false,
showDate = true,
source,
...rest
} = props;
const translate = useTranslate();
if (!showTime && !showDate) {
throw new Error(
'<DateField> cannot have showTime and showDate false at the same time'
);
}
const record = useRecordContext<RecordType>(props);
if (!record) {
return null;
}
const value = get(record, source) as any;
if (value == null || value === '') {
return emptyText ? (
<Typography
component="span"
variant="body2"
className={className}
{...sanitizeFieldRestProps(rest)}
>
{emptyText && translate(emptyText, { _: emptyText })}
</Typography>
) : null;
}
const date =
value instanceof Date
? value
: typeof value === 'string' || typeof value === 'number'
? new Date(value)
: undefined;
let dateOptions = options;
if (
typeof value === 'string' &&
value.length <= 10 &&
!showTime &&
!options
) {
// Input is a date string (e.g. '2022-02-15') without time and time zone.
// Force timezone to UTC to fix issue with people in negative time zones
// who may see a different date when calling toLocaleDateString().
dateOptions = { timeZone: 'UTC' };
}
let dateString = '';
if (showTime && showDate) {
dateString = toLocaleStringSupportsLocales
? date.toLocaleString(locales, options)
: date.toLocaleString();
} else if (showDate) {
dateString = toLocaleStringSupportsLocales
? date.toLocaleDateString(locales, dateOptions)
: date.toLocaleDateString();
} else if (showTime) {
dateString = toLocaleStringSupportsLocales
? date.toLocaleTimeString(locales, options)
: date.toLocaleTimeString();
}
return (
<Typography
component="span"
variant="body2"
className={className}
{...sanitizeFieldRestProps(rest)}
>
{dateString}
</Typography>
);
};
DateFieldImpl.propTypes = {
// @ts-ignore
...Typography.propTypes,
...fieldPropTypes,
locales: PropTypes.oneOfType([
PropTypes.string,
PropTypes.arrayOf(PropTypes.string),
]),
options: PropTypes.object,
showTime: PropTypes.bool,
showDate: PropTypes.bool,
};
DateFieldImpl.displayName = 'DateFieldImpl';
export const DateField = genericMemo(DateFieldImpl);
export interface DateFieldProps<
RecordType extends Record<string, any> = Record<string, any>
> extends FieldProps<RecordType>,
Omit<TypographyProps, 'textAlign'> {
locales?: Intl.LocalesArgument;
options?: Intl.DateTimeFormatOptions;
showTime?: boolean;
showDate?: boolean;
}
const toLocaleStringSupportsLocales = (() => {
// from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString
try {
new Date().toLocaleString('i');
} catch (error) {
return error instanceof RangeError;
}
return false;
})();