@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
126 lines (125 loc) • 5.48 kB
JavaScript
;
"use client";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = useDataValue;
var _react = require("react");
var _index = _interopRequireWildcard(require("../utils/json-pointer/index.js"));
var _DataContextRefContext = _interopRequireDefault(require("../DataContext/DataContextRefContext.js"));
var _IterateItemContext = _interopRequireDefault(require("../Iterate/IterateItemContext.js"));
var _usePath = _interopRequireDefault(require("./usePath.js"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function useDataValue(pathProp, value, options = {}) {
const providedDataContextRef = (0, _react.useContext)(_DataContextRefContext.default);
const fallbackDataContextRef = (0, _react.useRef)(undefined);
const dataContextRef = providedDataContextRef !== null && providedDataContextRef !== void 0 ? providedDataContextRef : fallbackDataContextRef;
const iterateItemContext = (0, _react.useContext)(_IterateItemContext.default);
const snapshotVersionRef = (0, _react.useRef)(0);
const {
pathType = 'auto'
} = options;
const {
makePath,
makeIteratePath
} = (0, _usePath.default)();
const resolvePath = (0, _react.useCallback)(path => {
if (pathType === 'absolute') {
return makePath(path);
}
if (pathType === 'iterate') {
return makeIteratePath(path);
}
if (iterateItemContext) {
if (path.startsWith('//')) {
return makePath(path);
}
const currentItemPath = makeIteratePath('/');
if (path === currentItemPath || path.startsWith(`${currentItemPath}/`)) {
return path;
}
return makeIteratePath(path);
}
return makePath(path);
}, [iterateItemContext, makeIteratePath, makePath, pathType]);
const subscribablePaths = (0, _react.useMemo)(() => {
if (!pathProp) {
return undefined;
}
const paths = Array.isArray(pathProp) ? pathProp : [pathProp];
const normalizedPaths = paths.filter(_index.isPath).map(path => resolvePath(path));
return normalizedPaths.length > 0 ? normalizedPaths : undefined;
}, [pathProp, resolvePath]);
const subscribablePath = typeof pathProp === 'string' && subscribablePaths?.length === 1 ? subscribablePaths[0] : undefined;
const subscribe = (0, _react.useCallback)(callback => {
if (subscribablePaths?.length && dataContextRef.current?.subscribeDataValue) {
const handleUpdate = () => {
snapshotVersionRef.current += 1;
callback();
};
const unsubscribers = subscribablePaths.map(path => dataContextRef.current.subscribeDataValue(path, handleUpdate));
return () => {
unsubscribers.forEach(unsubscribe => unsubscribe());
};
}
return () => undefined;
}, [dataContextRef, subscribablePaths]);
const getSnapshot = (0, _react.useCallback)(() => {
if (subscribablePath) {
return dataContextRef.current?.getDataValue?.(subscribablePath);
}
return snapshotVersionRef.current;
}, [dataContextRef, subscribablePath]);
const subscribedValue = (0, _react.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
const get = (0, _react.useCallback)((selector, data = dataContextRef.current?.internalDataRef?.current) => {
if (selector === '/') {
return data;
}
return _index.default.has(data, selector) ? _index.default.get(data, selector) : undefined;
}, [dataContextRef]);
const getValueByPath = (0, _react.useCallback)((path, data = undefined) => {
if ((0, _index.isPath)(path)) {
return get(makePath(path), data);
}
}, [get, makePath]);
const getValueByIteratePath = (0, _react.useCallback)(path => {
if ((0, _index.isPath)(path)) {
return get(makeIteratePath(path));
}
}, [get, makeIteratePath]);
const moveValueToPath = (0, _react.useCallback)((path, value, object = {}) => {
if (path !== '/' && (0, _index.isPath)(path)) {
_index.default.set(object, path, value);
return object;
}
return value;
}, []);
const getData = (0, _react.useCallback)((path, options) => {
if ((0, _index.isPath)(path)) {
const value = getValueByPath(path);
if (options?.includeCurrentPath && path !== '/') {
return moveValueToPath(path, value);
}
return value;
}
}, [getValueByPath, moveValueToPath]);
const getSourceValue = (0, _react.useCallback)(source => {
if (typeof source === 'string' && (0, _index.isPath)(source)) {
return get(resolvePath(source));
}
return source;
}, [get, resolvePath]);
if (typeof pathProp === 'string') {
value = subscribablePath ? subscribedValue : getSourceValue(pathProp);
}
return {
getSourceValue,
getValueByPath,
getValueByIteratePath,
moveValueToPath,
getData,
value
};
}
//# sourceMappingURL=useDataValue.js.map