UNPKG

@spark-ui/hooks

Version:

Common hooks for Spark UI

62 lines (60 loc) 2.79 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/use-combined-state/index.ts var use_combined_state_exports = {}; __export(use_combined_state_exports, { useCombinedState: () => useCombinedState }); module.exports = __toCommonJS(use_combined_state_exports); // src/use-combined-state/useCombinedState.tsx var import_lodash = __toESM(require("lodash.isequal")); var import_react = require("react"); function useCombinedState(controlledValue, defaultValue, onChange) { const isControlled = controlledValue !== void 0; const { current: initialValue } = (0, import_react.useRef)(isControlled ? controlledValue : defaultValue); const [innerValue, setInnerValue] = (0, import_react.useState)(defaultValue); const value = isControlled ? controlledValue : innerValue; const updater = (0, import_react.useCallback)( (next, shouldUpdateProp = (prevValue, nextValue) => !(0, import_lodash.default)(prevValue, nextValue)) => { const nextValue = typeof next !== "function" ? next : next(value); const shouldUpdate = shouldUpdateProp(value, nextValue); if (shouldUpdate && !isControlled) { setInnerValue(nextValue); } onChange && onChange(nextValue); }, [isControlled, value, onChange] ); return [value, updater, isControlled, initialValue]; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { useCombinedState }); //# sourceMappingURL=index.js.map