-
Notifications
You must be signed in to change notification settings - Fork 832
/
Copy pathDatePickerToolbar.tsx
74 lines (67 loc) · 2.21 KB
/
DatePickerToolbar.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
import * as React from 'react';
import clsx from 'clsx';
import PickerToolbar from '../_shared/PickerToolbar';
import Typography from '@material-ui/core/Typography';
import { DatePickerView } from './DatePicker';
import { useUtils } from '../_shared/hooks/useUtils';
import { makeStyles } from '@material-ui/core/styles';
import { ToolbarComponentProps } from '../Picker/Picker';
import { isYearAndMonthViews, isYearOnlyView } from '../_helpers/date-utils';
export const useStyles = makeStyles(
{
dateTitleLandscape: {
margin: 'auto 16px auto auto',
},
penIcon: {
position: 'relative',
top: 4,
},
},
{ name: 'MuiPickersDatePickerRoot' }
);
export const DatePickerToolbar: React.FC<ToolbarComponentProps> = ({
date,
views,
isLandscape,
isMobileKeyboardViewOpen,
toggleMobileKeyboardView,
toolbarFormat,
toolbarTitle = 'SELECT DATE',
}) => {
const utils = useUtils();
const classes = useStyles();
const dateText = React.useMemo(() => {
if (toolbarFormat) {
return utils.formatByString(date, toolbarFormat);
}
if (isYearOnlyView(views as DatePickerView[])) {
return utils.format(date, 'year');
}
if (isYearAndMonthViews(views as DatePickerView[])) {
return utils.format(date, 'month');
}
// Little localization hack (Google is doing the same for android native pickers):
// For english localization it is convenient to include weekday into the date "Mon, Jun 1"
// For other locales using strings like "June 1", without weekday
return /en/.test(utils.getCurrentLocaleCode())
? utils.format(date, 'normalDateWithWeekday')
: utils.format(date, 'normalDate');
}, [date, toolbarFormat, utils, views]);
return (
<PickerToolbar
toolbarTitle={toolbarTitle}
isMobileKeyboardViewOpen={isMobileKeyboardViewOpen}
toggleMobileKeyboardView={toggleMobileKeyboardView}
isLandscape={isLandscape}
penIconClassName={classes.penIcon}
>
<Typography
variant="h4"
children={dateText}
data-mui-test="datepicker-toolbar-date"
align={isLandscape ? 'left' : 'center'}
className={clsx({ [classes.dateTitleLandscape]: isLandscape })}
/>
</PickerToolbar>
);
};