-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
DatePickerToolbar.tsx
172 lines (155 loc) · 5.35 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import * as React from 'react';
import PropTypes from 'prop-types';
import Typography from '@mui/material/Typography';
import { styled, SxProps, Theme, useThemeProps } from '@mui/material/styles';
import { unstable_composeClasses as composeClasses } from '@mui/utils';
import { PickersToolbar } from '../internals/components/PickersToolbar';
import { useLocaleText, useUtils } from '../internals/hooks/useUtils';
import { BaseToolbarProps, ExportedBaseToolbarProps } from '../internals/models/props/toolbar';
import { isYearOnlyView, isYearAndMonthViews } from '../internals/utils/views';
import { DateView } from '../internals/models';
import {
DatePickerToolbarClasses,
getDatePickerToolbarUtilityClass,
} from './datePickerToolbarClasses';
export interface DatePickerToolbarProps<TDate> extends BaseToolbarProps<TDate | null, DateView> {
classes?: Partial<DatePickerToolbarClasses>;
sx?: SxProps<Theme>;
}
export interface ExportedDatePickerToolbarProps extends ExportedBaseToolbarProps {}
const useUtilityClasses = (ownerState: DatePickerToolbarProps<any>) => {
const { classes } = ownerState;
const slots = {
root: ['root'],
title: ['title'],
};
return composeClasses(slots, getDatePickerToolbarUtilityClass, classes);
};
const DatePickerToolbarRoot = styled(PickersToolbar, {
name: 'MuiDatePickerToolbar',
slot: 'Root',
overridesResolver: (_, styles) => styles.root,
})({});
/**
* @ignore - do not document.
*/
const DatePickerToolbarTitle = styled(Typography, {
name: 'MuiDatePickerToolbar',
slot: 'Title',
overridesResolver: (_, styles) => styles.title,
})<{ ownerState: DatePickerToolbarProps<any> }>(({ ownerState }) => ({
...(ownerState.isLandscape && {
margin: 'auto 16px auto auto',
}),
}));
type DatePickerToolbarComponent = (<TDate>(
props: DatePickerToolbarProps<TDate> & React.RefAttributes<HTMLDivElement>,
) => JSX.Element) & { propTypes?: any };
const DatePickerToolbar = React.forwardRef(function DatePickerToolbar<TDate>(
inProps: DatePickerToolbarProps<TDate>,
ref: React.Ref<HTMLDivElement>,
) {
const props = useThemeProps({ props: inProps, name: 'MuiDatePickerToolbar' });
const {
value,
isLandscape,
onChange,
toolbarFormat,
toolbarPlaceholder = '––',
views,
...other
} = props;
const utils = useUtils<TDate>();
const localeText = useLocaleText<TDate>();
const classes = useUtilityClasses(props);
const dateText = React.useMemo(() => {
if (!value) {
return toolbarPlaceholder;
}
if (toolbarFormat) {
return utils.formatByString(value, toolbarFormat);
}
if (isYearOnlyView(views as DateView[])) {
return utils.format(value, 'year');
}
if (isYearAndMonthViews(views as DateView[])) {
return utils.format(value, '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(value, 'normalDateWithWeekday')
: utils.format(value, 'normalDate');
}, [value, toolbarFormat, toolbarPlaceholder, utils, views]);
const ownerState = props;
return (
<DatePickerToolbarRoot
ref={ref}
toolbarTitle={localeText.datePickerToolbarTitle}
isLandscape={isLandscape}
className={classes.root}
{...other}
>
<DatePickerToolbarTitle
variant="h4"
data-mui-test="datepicker-toolbar-date"
align={isLandscape ? 'left' : 'center'}
ownerState={ownerState}
className={classes.title}
>
{dateText}
</DatePickerToolbarTitle>
</DatePickerToolbarRoot>
);
}) as DatePickerToolbarComponent;
DatePickerToolbar.propTypes = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit the TypeScript types and run "yarn proptypes" |
// ----------------------------------------------------------------------
classes: PropTypes.object,
/**
* className applied to the root component.
*/
className: PropTypes.string,
disabled: PropTypes.bool,
/**
* If `true`, show the toolbar even in desktop mode.
* @default `true` for Desktop, `false` for Mobile.
*/
hidden: PropTypes.bool,
isLandscape: PropTypes.bool.isRequired,
onChange: PropTypes.func.isRequired,
/**
* Callback called when a toolbar is clicked
* @template TView
* @param {TView} view The view to open
*/
onViewChange: PropTypes.func.isRequired,
readOnly: PropTypes.bool,
sx: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])),
PropTypes.func,
PropTypes.object,
]),
titleId: PropTypes.string,
/**
* Toolbar date format.
*/
toolbarFormat: PropTypes.string,
/**
* Toolbar value placeholder—it is displayed when the value is empty.
* @default "––"
*/
toolbarPlaceholder: PropTypes.node,
value: PropTypes.any,
/**
* Currently visible picker view.
*/
view: PropTypes.oneOf(['day', 'month', 'year']).isRequired,
views: PropTypes.arrayOf(
PropTypes.oneOf(['day', 'hours', 'minutes', 'month', 'seconds', 'year']).isRequired,
).isRequired,
} as any;
export { DatePickerToolbar };