@mui/x-date-pickers
Version:
The community edition of the Date and Time Picker components (MUI X).
43 lines (41 loc) • 1.3 kB
JavaScript
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useOpenState = void 0;
var React = _interopRequireWildcard(require("react"));
const useOpenState = ({
open,
onOpen,
onClose
}) => {
const isControllingOpenProp = React.useRef(typeof open === 'boolean').current;
const [openState, setIsOpenState] = React.useState(false);
// It is required to update inner state in useEffect in order to avoid situation when
// Our component is not mounted yet, but `open` state is set to `true` (for example initially opened)
React.useEffect(() => {
if (isControllingOpenProp) {
if (typeof open !== 'boolean') {
throw new Error('You must not mix controlling and uncontrolled mode for `open` prop');
}
setIsOpenState(open);
}
}, [isControllingOpenProp, open]);
const setIsOpen = React.useCallback(newIsOpen => {
if (!isControllingOpenProp) {
setIsOpenState(newIsOpen);
}
if (newIsOpen && onOpen) {
onOpen();
}
if (!newIsOpen && onClose) {
onClose();
}
}, [isControllingOpenProp, onOpen, onClose]);
return {
isOpen: openState,
setIsOpen
};
};
exports.useOpenState = useOpenState;
;