Skip to content

Commit

Permalink
Add new toolbarFormat prop, closes mui#1345
Browse files Browse the repository at this point in the history
  • Loading branch information
dmtrKovalenko committed Feb 24, 2020
1 parent bc20e28 commit 0b9df54
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 2 deletions.
26 changes: 26 additions & 0 deletions docs/prop-types.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions lib/src/DatePicker/DatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export type DatePickerView = 'year' | 'date' | 'month';
export interface BaseDatePickerProps extends ExportedCalendarViewProps {
/** Callback firing on year change @DateIOType */
onYearChange?: (date: MaterialUiPickersDate) => void;
/** Date format, that is displaying in toolbar */
toolbarFormat?: string;
}

export type DatePickerProps = BaseDatePickerProps &
Expand Down
7 changes: 6 additions & 1 deletion lib/src/DatePicker/DatePickerToolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,17 @@ export const DatePickerToolbar: React.FC<ToolbarComponentProps> = ({
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');
}
Expand All @@ -46,7 +51,7 @@ export const DatePickerToolbar: React.FC<ToolbarComponentProps> = ({
return /en/.test(utils.getCurrentLocaleCode())
? utils.format(date, 'normalDateWithWeekday')
: utils.format(date, 'normalDate');
}, [date, utils, views]);
}, [date, toolbarFormat, utils, views]);

return (
<PickerToolbar
Expand Down
2 changes: 2 additions & 0 deletions lib/src/DateTimePicker/DateTimePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export interface DateTimePickerViewsProps extends BaseDateTimePickerProps {
minDateTime?: ParsableDate;
/** Minimal selectable moment of time with binding to date, to set max time in each day use `maxTime` */
maxDateTime?: ParsableDate;
/** Date format, that is displaying in toolbar */
toolbarFormat?: string;
}

export type DateTimePickerProps = WithDateInputProps &
Expand Down
7 changes: 6 additions & 1 deletion lib/src/DateTimePicker/DateTimePickerToolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const DateTimePickerToolbar: React.FC<ToolbarComponentProps> = ({
hideTabs,
dateRangeIcon,
timeIcon,
toolbarFormat,
isMobileKeyboardViewOpen,
toggleMobileKeyboardView,
toolbarTitle = 'SELECT DATE & TIME',
Expand Down Expand Up @@ -77,7 +78,11 @@ export const DateTimePickerToolbar: React.FC<ToolbarComponentProps> = ({
variant="h4"
onClick={() => setOpenView('date')}
selected={openView === 'date'}
label={utils.format(date, 'shortDate')}
label={
toolbarFormat
? utils.formatByString(date, toolbarFormat)
: utils.format(date, 'shortDate')
}
/>
</div>

Expand Down
2 changes: 2 additions & 0 deletions lib/src/Picker/makePickerWithState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export function makePickerWithStateAndWrapper<
shouldDisableTime,
strictCompareDates,
timeIcon,
toolbarFormat,
ToolbarComponent = DefaultToolbarComponent,
value,
views,
Expand Down Expand Up @@ -147,6 +148,7 @@ export function makePickerWithStateAndWrapper<
showToolbar={showToolbar}
strictCompareDates={strictCompareDates}
timeIcon={timeIcon}
toolbarFormat={toolbarFormat}
ToolbarComponent={ToolbarComponent}
// @ts-ignore
toolbarTitle={toolbarTitle || restPropsForTextField.label}
Expand Down

0 comments on commit 0b9df54

Please sign in to comment.