import * as React from 'react'; import clsx from 'clsx'; import { makeStyles } from '@material-ui/core/styles'; import { useViews } from '../_shared/hooks/useViews'; import { ClockView } from '../views/Clock/ClockView'; import { DateTimePickerView } from '../DateTimePicker'; import { BasePickerProps } from '../typings/BasePicker'; import { DatePickerView } from '../DatePicker/DatePicker'; import { CalendarView } from '../views/Calendar/CalendarView'; import { withDefaultProps } from '../_shared/withDefaultProps'; import { KeyboardDateInput } from '../_shared/KeyboardDateInput'; import { useIsLandscape } from '../_shared/hooks/useIsLandscape'; import { DIALOG_WIDTH, VIEW_HEIGHT } from '../constants/dimensions'; import { PickerSelectionState } from '../_shared/hooks/usePickerState'; import { WrapperVariantContext } from '../wrappers/WrapperVariantContext'; import { MobileKeyboardInputView } from '../views/MobileKeyboardInputView'; import { WithViewsProps, AnyPickerView, SharedPickerProps, CalendarAndClockProps, } from './SharedPickerProps'; export interface ExportedPickerProps extends Omit, CalendarAndClockProps, WithViewsProps { // TODO move out, cause it is DateTimePickerOnly hideTabs?: boolean; dateRangeIcon?: React.ReactNode; timeIcon?: React.ReactNode; } export type PickerProps< TView extends AnyPickerView, TInputValue = any, TDateValue = any > = ExportedPickerProps & SharedPickerProps; const muiComponentConfig = { name: 'MuiPickersBasePicker' }; export const useStyles = makeStyles( { root: { display: 'flex', flexDirection: 'column', }, landscape: { flexDirection: 'row', }, pickerView: { overflowX: 'hidden', width: DIALOG_WIDTH, maxHeight: VIEW_HEIGHT, display: 'flex', flexDirection: 'column', margin: '0 auto', }, pickerViewLandscape: { padding: '0 8px', }, }, muiComponentConfig ); const MobileKeyboardTextFieldProps = { fullWidth: true }; const isDatePickerView = (view: DateTimePickerView) => view === 'year' || view === 'month' || view === 'date'; function Picker({ className, date, DateInputProps, isMobileKeyboardViewOpen, onDateChange, openTo = 'date', orientation, showToolbar, toggleMobileKeyboardView, ToolbarComponent = () => null, toolbarFormat, toolbarPlaceholder, toolbarTitle, views = ['year', 'month', 'date', 'hours', 'minutes', 'seconds'], ...other }: PickerProps) { const classes = useStyles(); const isLandscape = useIsLandscape(views, orientation); const wrapperVariant = React.useContext(WrapperVariantContext); const toShowToolbar = typeof showToolbar === 'undefined' ? wrapperVariant !== 'desktop' : showToolbar; const handleDateChange = React.useCallback( (date: unknown, selectionState?: PickerSelectionState) => { onDateChange(date, wrapperVariant, selectionState); }, [onDateChange, wrapperVariant] ); const { openView, nextView, previousView, setOpenView, handleChangeAndOpenNext } = useViews({ views, openTo, onChange: handleDateChange, isMobileKeyboardViewOpen, toggleMobileKeyboardView, }); return (
{toShowToolbar && ( )}
{isMobileKeyboardViewOpen ? ( ) : ( {(openView === 'year' || openView === 'month' || openView === 'date') && ( )} {(openView === 'hours' || openView === 'minutes' || openView === 'seconds') && ( setOpenView(nextView)} openPreviousView={() => setOpenView(previousView)} nextViewAvailable={!nextView} previousViewAvailable={!previousView || isDatePickerView(previousView)} showViewSwitcher={wrapperVariant === 'desktop'} /> )} )}
); } export default withDefaultProps(muiComponentConfig, Picker);