UNPKG

@mui/x-data-grid

Version:

The Community plan edition of the MUI X Data Grid components.

94 lines (92 loc) 3.83 kB
"use strict"; 'use client'; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.objectShallowCompare = void 0; exports.useGridSelector = useGridSelector; var React = _interopRequireWildcard(require("react")); var _useEnhancedEffect = _interopRequireDefault(require("@mui/utils/useEnhancedEffect")); var _fastObjectShallowCompare = require("@mui/x-internals/fastObjectShallowCompare"); var _warning = require("@mui/x-internals/warning"); var _useLazyRef = require("./useLazyRef"); const defaultCompare = Object.is; const objectShallowCompare = exports.objectShallowCompare = _fastObjectShallowCompare.fastObjectShallowCompare; const arrayShallowCompare = (a, b) => { if (a === b) { return true; } return a.length === b.length && a.every((v, i) => v === b[i]); }; const argsEqual = (prev, curr) => { let fn = Object.is; if (curr instanceof Array) { fn = arrayShallowCompare; } else if (curr instanceof Object) { fn = objectShallowCompare; } return fn(prev, curr); }; const createRefs = () => ({ state: null, equals: null, selector: null, args: undefined, storeState: null }); const EMPTY = []; function useGridSelector(apiRef, selector, args = undefined, equals = defaultCompare) { if (process.env.NODE_ENV !== 'production' && !apiRef.current.state) { (0, _warning.warnOnce)(['MUI X: `useGridSelector` has been called before the initialization of the state.', 'This hook can only be used inside the context of the grid.']); } const refs = (0, _useLazyRef.useLazyRef)(createRefs); const didInit = refs.current.selector !== null; const [state, setState] = React.useState( // We don't use an initialization function to avoid allocations didInit ? null : selector(apiRef, args)); refs.current.state = state; if (!didInit) { refs.current.storeState = apiRef.current.store.state; } refs.current.equals = equals; refs.current.selector = selector; const prevArgs = refs.current.args; refs.current.args = args; if (didInit && !argsEqual(prevArgs, args)) { const newState = refs.current.selector(apiRef, refs.current.args); if (!refs.current.equals(refs.current.state, newState)) { refs.current.state = newState; setState(newState); } refs.current.storeState = apiRef.current.store.state; } const updateState = React.useCallback(() => { const storeState = apiRef.current.store.state; if (refs.current.storeState !== storeState) { const newState = refs.current.selector(apiRef, refs.current.args); refs.current.storeState = storeState; if (!refs.current.equals(refs.current.state, newState)) { refs.current.state = newState; setState(newState); } } }, // eslint-disable-next-line react-hooks/exhaustive-deps, react-hooks/use-memo EMPTY); // Why subscribe in an effect instead of during render: a component can render without // ever mounting (e.g. when it suspends during hydration). If it subscribed during render, // it could receive a store update and call `setState` before being mounted (#17077). // Effects only run for mounted components, so subscribing here is safe. // // Using a layout effect because the store may already have changed // between render and mount (e.g. from a child's ref callback or layout effect). // `updateState()` picks up such changes, so the corrected value is shown right away instead of in a second frame. (0, _useEnhancedEffect.default)(() => { updateState(); return apiRef.current.store.subscribe(updateState); // eslint-disable-next-line react-hooks/exhaustive-deps }, EMPTY); return state; }