@mui/x-date-pickers-pro
Version:
The Pro plan edition of the Date and Time Picker components (MUI X).
164 lines • 5.02 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
const _excluded = ["props"];
import * as React from 'react';
import { useSlotProps } from '@mui/base/utils';
import { useLicenseVerifier } from '@mui/x-license';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { PickersLayout } from '@mui/x-date-pickers/PickersLayout';
import { executeInTheNextEventLoopTick, getActiveElement, usePicker, PickersPopper } from '@mui/x-date-pickers/internals';
import { useEnrichedRangePickerFieldProps } from '../useEnrichedRangePickerFieldProps';
import { getReleaseInfo } from '../../utils/releaseInfo';
import { useRangePosition } from '../useRangePosition';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const releaseInfo = getReleaseInfo();
export const useDesktopRangePicker = _ref => {
let {
props
} = _ref,
pickerParams = _objectWithoutPropertiesLoose(_ref, _excluded);
useLicenseVerifier('x-date-pickers-pro', releaseInfo);
const {
slots,
slotProps,
className,
sx,
format,
formatDensity,
enableAccessibleFieldDOMStructure,
selectedSections,
onSelectedSectionsChange,
timezone,
label,
inputRef,
name,
readOnly,
disabled,
autoFocus,
disableOpenPicker,
localeText,
reduceAnimations
} = props;
const fieldContainerRef = React.useRef(null);
const anchorRef = React.useRef(null);
const popperRef = React.useRef(null);
const startFieldRef = React.useRef(null);
const endFieldRef = React.useRef(null);
const initialView = React.useRef(props.openTo ?? null);
const fieldType = slots.field.fieldType ?? 'multi-input';
const {
rangePosition,
onRangePositionChange
} = useRangePosition(props, fieldType === 'single-input' ? startFieldRef : undefined);
const {
open,
actions,
layoutProps,
renderCurrentView,
shouldRestoreFocus,
fieldProps: pickerFieldProps
} = usePicker(_extends({}, pickerParams, {
props,
wrapperVariant: 'desktop',
autoFocusView: false,
fieldRef: rangePosition === 'start' ? startFieldRef : endFieldRef,
additionalViewProps: {
rangePosition,
onRangePositionChange
}
}));
React.useEffect(() => {
if (layoutProps.view) {
initialView.current = layoutProps.view;
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const handleBlur = () => {
executeInTheNextEventLoopTick(() => {
if (fieldContainerRef.current?.contains(getActiveElement(document)) || popperRef.current?.contains(getActiveElement(document))) {
return;
}
actions.onDismiss();
});
};
const Field = slots.field;
const fieldProps = useSlotProps({
elementType: Field,
externalSlotProps: slotProps?.field,
additionalProps: _extends({}, pickerFieldProps, {
readOnly,
disabled,
className,
sx,
format,
formatDensity,
enableAccessibleFieldDOMStructure,
selectedSections,
onSelectedSectionsChange,
timezone,
autoFocus: autoFocus && !props.open,
ref: fieldContainerRef
}, fieldType === 'single-input' ? {
inputRef,
name
} : {}),
ownerState: props
});
const enrichedFieldProps = useEnrichedRangePickerFieldProps({
wrapperVariant: 'desktop',
fieldType,
open,
actions,
readOnly,
disableOpenPicker,
label,
localeText,
onBlur: handleBlur,
rangePosition,
onRangePositionChange,
pickerSlotProps: slotProps,
pickerSlots: slots,
fieldProps,
anchorRef,
startFieldRef,
endFieldRef,
currentView: layoutProps.view !== props.openTo ? layoutProps.view : undefined,
initialView: initialView.current ?? undefined,
onViewChange: layoutProps.onViewChange
});
const slotPropsForLayout = _extends({}, slotProps, {
tabs: _extends({}, slotProps?.tabs, {
rangePosition,
onRangePositionChange
}),
toolbar: _extends({}, slotProps?.toolbar, {
rangePosition,
onRangePositionChange
})
});
const Layout = slots?.layout ?? PickersLayout;
const renderPicker = () => /*#__PURE__*/_jsxs(LocalizationProvider, {
localeText: localeText,
children: [/*#__PURE__*/_jsx(Field, _extends({}, enrichedFieldProps)), /*#__PURE__*/_jsx(PickersPopper, _extends({
role: "tooltip",
placement: "bottom-start",
containerRef: popperRef,
anchorEl: anchorRef.current,
onBlur: handleBlur
}, actions, {
open: open,
slots: slots,
slotProps: slotProps,
shouldRestoreFocus: shouldRestoreFocus,
reduceAnimations: reduceAnimations,
children: /*#__PURE__*/_jsx(Layout, _extends({}, layoutProps, slotProps?.layout, {
slots: slots,
slotProps: slotPropsForLayout,
children: renderCurrentView()
}))
}))]
});
return {
renderPicker
};
};