Skip to content

Commit

Permalink
Dynamically update PickersToolbar viewType
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasTy committed Feb 14, 2023
1 parent 5911db3 commit 4f136fa
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
DateTimePickerToolbarClasses,
getDateTimePickerToolbarUtilityClass,
} from './dateTimePickerToolbarClasses';
import { resolveViewTypeFromView } from './shared';

export interface DateTimePickerToolbarProps<TDate> extends BaseToolbarProps<TDate, TDate | null> {
classes?: Partial<DateTimePickerToolbarClasses>;
Expand Down Expand Up @@ -122,6 +123,7 @@ export function DateTimePickerToolbar<TDate extends unknown>(
isMobileKeyboardViewOpen={isMobileKeyboardViewOpen}
toggleMobileKeyboardView={toggleMobileKeyboardView}
className={classes.root}
viewType={resolveViewTypeFromView(openView)}
{...other}
isLandscape={false}
ownerState={ownerState}
Expand Down
13 changes: 12 additions & 1 deletion packages/x-date-pickers/src/DateTimePicker/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { DateTimeValidationError } from '../internals/hooks/validation/useDateTi
import { ValidationProps } from '../internals/hooks/validation/useValidation';
import { BasePickerProps } from '../internals/models/props/basePickerProps';
import { ExportedDateInputProps } from '../internals/components/PureDateInput';
import { CalendarOrClockPickerView } from '../internals/models';
import { CalendarOrClockPickerView, ViewType } from '../internals/models';
import { PickerStateValueManager } from '../internals/hooks/usePickerState';
import { parsePickerInputValue, parseNonNullablePickerDate } from '../internals/utils/date-utils';
import { BaseToolbarProps } from '../internals/models/props/baseToolbarProps';
Expand Down Expand Up @@ -146,3 +146,14 @@ export const dateTimePickerValueManager: PickerStateValueManager<any, any, any>
parseInput: parsePickerInputValue,
areValuesEqual: (utils, a, b) => utils.isEqual(a, b),
};

export const resolveViewTypeFromView = (view: CalendarOrClockPickerView): ViewType => {
switch (view) {
case 'year':
case 'month':
case 'day':
return 'calendar';
default:
return 'clock';
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import {
pickersToolbarClasses,
PickersToolbarClasses,
} from './pickersToolbarClasses';
import { ViewType } from '../models';

export interface PickersToolbarProps<TDate, TValue>
extends Pick<
BaseToolbarProps<TDate, TValue>,
'getMobileKeyboardInputViewButtonText' | 'isMobileKeyboardViewOpen' | 'toggleMobileKeyboardView'
> {
className?: string;
viewType?: 'calendar' | 'clock';
viewType?: ViewType;
isLandscape: boolean;
landscapeDirection?: 'row' | 'column';
toolbarTitle: React.ReactNode;
Expand Down Expand Up @@ -83,7 +84,7 @@ const PickersToolbarPenIconButton = styled(IconButton, {
ownerState: PickersToolbarProps<any, any>;
}>({});

const getViewTypeIcon = (viewType: 'calendar' | 'clock') =>
const getViewTypeIcon = (viewType: ViewType) =>
viewType === 'clock' ? <Clock color="inherit" /> : <Calendar color="inherit" />;

type PickersToolbarComponent = (<TDate, TValue>(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { CalendarOrClockPickerView } from '../views';
import { CalendarOrClockPickerView, ViewType } from '../views';
import type { PickerOnChangeFn } from '../../hooks/useViews';
import type { ExportedCalendarPickerProps } from '../../../CalendarPicker/CalendarPicker';
import type { ExportedClockPickerProps } from '../../../ClockPicker/ClockPicker';
Expand All @@ -15,12 +15,12 @@ export interface BaseToolbarProps<TDate, TValue>
* Text for aria label of the button switching between input and interactive view.
* @deprecated Use the translation key `inputModeToggleButtonAriaLabel` instead, see https://mui.com/x/react-date-pickers/localization
* @param {boolean} isKeyboardInputOpen Indicates if the interface is the keyboard input.
* @param {'calendar' | 'clock' } viewType Indicates if the interface is about a date or a time.
* @param {ViewType} viewType Indicates if the interface is about a date or a time.
* @returns {string} The arial label
*/
getMobileKeyboardInputViewButtonText?: (
isKeyboardInputOpen: boolean,
viewType: 'calendar' | 'clock',
viewType: ViewType,
) => string;
isLandscape: boolean;
onChange: PickerOnChangeFn<TDate>;
Expand Down
2 changes: 2 additions & 0 deletions packages/x-date-pickers/src/internals/models/views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ export type CalendarPickerView = 'year' | 'day' | 'month';
export type ClockPickerView = 'hours' | 'minutes' | 'seconds';

export type CalendarOrClockPickerView = CalendarPickerView | ClockPickerView;

export type ViewType = 'calendar' | 'clock';
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { CalendarPickerView, ClockPickerView, MuiPickersAdapter } from '../../internals/models';
import {
CalendarPickerView,
ClockPickerView,
MuiPickersAdapter,
ViewType,
} from '../../internals/models';
/**
* Set the types of the texts in the grid.
*/
Expand All @@ -14,10 +19,7 @@ export interface PickersLocaleText<TDate> {
start: string;
end: string;
calendarViewSwitchingButtonAriaLabel: (currentView: CalendarPickerView) => string;
inputModeToggleButtonAriaLabel: (
isKeyboardInputOpen: boolean,
viewType: 'calendar' | 'clock',
) => string;
inputModeToggleButtonAriaLabel: (isKeyboardInputOpen: boolean, viewType: ViewType) => string;
clockLabelText: (
view: ClockPickerView,
time: TDate | null,
Expand Down

0 comments on commit 4f136fa

Please sign in to comment.