@mui/x-date-pickers
Version:
The community edition of the Date and Time Picker components (MUI X).
136 lines (130 loc) • 3.57 kB
JavaScript
'use client';
import _extends from "@babel/runtime/helpers/esm/extends";
import * as React from 'react';
import useSlotProps from '@mui/utils/useSlotProps';
import composeClasses from '@mui/utils/composeClasses';
import { PickersActionBar } from "../PickersActionBar/index.js";
import { getPickersLayoutUtilityClass } from "./pickersLayoutClasses.js";
import { PickersShortcuts } from "../PickersShortcuts/index.js";
import { jsx as _jsx } from "react/jsx-runtime";
function toolbarHasView(toolbarProps) {
return toolbarProps.view !== null;
}
const useUtilityClasses = ownerState => {
const {
classes,
isLandscape
} = ownerState;
const slots = {
root: ['root', isLandscape && 'landscape'],
contentWrapper: ['contentWrapper'],
toolbar: ['toolbar'],
actionBar: ['actionBar'],
tabs: ['tabs'],
landscape: ['landscape'],
shortcuts: ['shortcuts']
};
return composeClasses(slots, getPickersLayoutUtilityClass, classes);
};
const usePickerLayout = props => {
const {
wrapperVariant,
onAccept,
onClear,
onCancel,
onSetToday,
view,
views,
onViewChange,
value,
onChange,
onSelectShortcut,
isValid,
isLandscape,
disabled,
readOnly,
children,
slots,
slotProps
// TODO: Remove this "as" hack. It get introduced to mark `value` prop in PickersLayoutProps as not required.
// The true type should be
// - For pickers value: TDate | null
// - For range pickers value: [TDate | null, TDate | null]
} = props;
const classes = useUtilityClasses(props);
// Action bar
const ActionBar = slots?.actionBar ?? PickersActionBar;
const actionBarProps = useSlotProps({
elementType: ActionBar,
externalSlotProps: slotProps?.actionBar,
additionalProps: {
onAccept,
onClear,
onCancel,
onSetToday,
actions: wrapperVariant === 'desktop' ? [] : ['cancel', 'accept']
},
className: classes.actionBar,
ownerState: _extends({}, props, {
wrapperVariant
})
});
const actionBar = /*#__PURE__*/_jsx(ActionBar, _extends({}, actionBarProps));
// Toolbar
const Toolbar = slots?.toolbar;
const toolbarProps = useSlotProps({
elementType: Toolbar,
externalSlotProps: slotProps?.toolbar,
additionalProps: {
isLandscape,
onChange,
value,
view,
onViewChange,
views,
disabled,
readOnly
},
className: classes.toolbar,
ownerState: _extends({}, props, {
wrapperVariant
})
});
const toolbar = toolbarHasView(toolbarProps) && !!Toolbar ? /*#__PURE__*/_jsx(Toolbar, _extends({}, toolbarProps)) : null;
// Content
const content = children;
// Tabs
const Tabs = slots?.tabs;
const tabs = view && Tabs ? /*#__PURE__*/_jsx(Tabs, _extends({
view: view,
onViewChange: onViewChange,
className: classes.tabs
}, slotProps?.tabs)) : null;
// Shortcuts
const Shortcuts = slots?.shortcuts ?? PickersShortcuts;
const shortcutsProps = useSlotProps({
elementType: Shortcuts,
externalSlotProps: slotProps?.shortcuts,
additionalProps: {
isValid,
isLandscape,
onChange: onSelectShortcut
},
className: classes.shortcuts,
ownerState: {
isValid,
isLandscape,
onChange: onSelectShortcut,
wrapperVariant
}
});
const shortcuts = view && !!Shortcuts ? /*#__PURE__*/_jsx(Shortcuts, _extends({}, shortcutsProps)) : null;
return {
toolbar,
content,
tabs,
actionBar,
shortcuts
};
};
export default usePickerLayout;