UNPKG

@mui/x-date-pickers

Version:

The community edition of the Date and Time Picker components (MUI X).

121 lines (120 loc) 4.88 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.useViews = useViews; var React = _interopRequireWildcard(require("react")); var _useEventCallback = _interopRequireDefault(require("@mui/utils/useEventCallback")); var _utils = require("@mui/utils"); let warnedOnceNotValidView = false; function useViews({ onChange, onViewChange, openTo, view: inView, views, autoFocus, focusedView: inFocusedView, onFocusedViewChange }) { if (process.env.NODE_ENV !== 'production') { if (!warnedOnceNotValidView) { if (inView != null && !views.includes(inView)) { console.warn(`MUI X: \`view="${inView}"\` is not a valid prop.`, `It must be an element of \`views=["${views.join('", "')}"]\`.`); warnedOnceNotValidView = true; } if (inView == null && openTo != null && !views.includes(openTo)) { console.warn(`MUI X: \`openTo="${openTo}"\` is not a valid prop.`, `It must be an element of \`views=["${views.join('", "')}"]\`.`); warnedOnceNotValidView = true; } } } const previousOpenTo = React.useRef(openTo); const previousViews = React.useRef(views); const defaultView = React.useRef(views.includes(openTo) ? openTo : views[0]); const [view, setView] = (0, _utils.unstable_useControlled)({ name: 'useViews', state: 'view', controlled: inView, default: defaultView.current }); const defaultFocusedView = React.useRef(autoFocus ? view : null); const [focusedView, setFocusedView] = (0, _utils.unstable_useControlled)({ name: 'useViews', state: 'focusedView', controlled: inFocusedView, default: defaultFocusedView.current }); React.useEffect(() => { // Update the current view when `openTo` or `views` props change if (previousOpenTo.current && previousOpenTo.current !== openTo || previousViews.current && previousViews.current.some(previousView => !views.includes(previousView))) { setView(views.includes(openTo) ? openTo : views[0]); previousViews.current = views; previousOpenTo.current = openTo; } }, [openTo, setView, view, views]); const viewIndex = views.indexOf(view); const previousView = views[viewIndex - 1] ?? null; const nextView = views[viewIndex + 1] ?? null; const handleFocusedViewChange = (0, _useEventCallback.default)((viewToFocus, hasFocus) => { if (hasFocus) { // Focus event setFocusedView(viewToFocus); } else { // Blur event setFocusedView(prevFocusedView => viewToFocus === prevFocusedView ? null : prevFocusedView // If false the blur is due to view switching ); } onFocusedViewChange?.(viewToFocus, hasFocus); }); const handleChangeView = (0, _useEventCallback.default)(newView => { // always keep the focused view in sync handleFocusedViewChange(newView, true); if (newView === view) { return; } setView(newView); if (onViewChange) { onViewChange(newView); } }); const goToNextView = (0, _useEventCallback.default)(() => { if (nextView) { handleChangeView(nextView); } }); const setValueAndGoToNextView = (0, _useEventCallback.default)((value, currentViewSelectionState, selectedView) => { const isSelectionFinishedOnCurrentView = currentViewSelectionState === 'finish'; const hasMoreViews = selectedView ? // handles case like `DateTimePicker`, where a view might return a `finish` selection state // but when it's not the final view given all `views` -> overall selection state should be `partial`. views.indexOf(selectedView) < views.length - 1 : Boolean(nextView); const globalSelectionState = isSelectionFinishedOnCurrentView && hasMoreViews ? 'partial' : currentViewSelectionState; onChange(value, globalSelectionState, selectedView); // Detects if the selected view is not the active one. // Can happen if multiple views are displayed, like in `DesktopDateTimePicker` or `MultiSectionDigitalClock`. if (selectedView && selectedView !== view) { const nextViewAfterSelected = views[views.indexOf(selectedView) + 1]; if (nextViewAfterSelected) { // move to next view after the selected one handleChangeView(nextViewAfterSelected); } } else if (isSelectionFinishedOnCurrentView) { goToNextView(); } }); return { view, setView: handleChangeView, focusedView, setFocusedView: handleFocusedViewChange, nextView, previousView, // Always return up-to-date default view instead of the initial one (i.e. defaultView.current) defaultView: views.includes(openTo) ? openTo : views[0], goToNextView, setValueAndGoToNextView }; }