@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
156 lines (155 loc) • 6.22 kB
JavaScript
;
"use client";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = require("react");
var _index = _interopRequireDefault(require("../../utils/json-pointer/index.js"));
var _Context = _interopRequireDefault(require("../Context.js"));
var _DataContextRefContext = _interopRequireDefault(require("../DataContextRefContext.js"));
var _withComponentMarkers = _interopRequireDefault(require("../../../../shared/helpers/withComponentMarkers.js"));
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function At(props) {
const {
path = '/',
iterate,
children
} = props;
const dataContext = (0, _react.useContext)(_Context.default);
const {
data: contextData,
handlePathChange: handlePathChangeDataContext,
handlePathChangeUnvalidated: handlePathChangeUnvalidatedDataContext,
updateDataValue: updateDataValueDataContext,
subscribeDataValue,
getDataValue
} = dataContext;
const subscribe = (0, _react.useCallback)(callback => {
var _subscribeDataValue;
return (_subscribeDataValue = subscribeDataValue?.(path, callback)) !== null && _subscribeDataValue !== void 0 ? _subscribeDataValue : noop;
}, [path, subscribeDataValue]);
const getSnapshot = (0, _react.useCallback)(() => {
if (getDataValue) {
return getDataValue(path);
}
return getValueByPath(contextData, path);
}, [contextData, getDataValue, path]);
const data = (0, _react.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
const internalDataRef = (0, _react.useRef)(data);
internalDataRef.current = data;
const makeScopedPath = (0, _react.useCallback)(changePath => joinPath(path, changePath), [path]);
const handlePathChange = (0, _react.useCallback)((changePath, value) => {
return handlePathChangeDataContext(makeScopedPath(changePath), value);
}, [handlePathChangeDataContext, makeScopedPath]);
const handlePathChangeUnvalidated = (0, _react.useCallback)((changePath, value, options) => {
return handlePathChangeUnvalidatedDataContext(makeScopedPath(changePath), value, options);
}, [handlePathChangeUnvalidatedDataContext, makeScopedPath]);
const updateDataValue = (0, _react.useCallback)((changePath, value, options) => {
return updateDataValueDataContext(makeScopedPath(changePath), value, options);
}, [makeScopedPath, updateDataValueDataContext]);
const scopedSubscribeDataValue = (0, _react.useCallback)((changePath, callback) => {
var _subscribeDataValue2;
return (_subscribeDataValue2 = subscribeDataValue?.(makeScopedPath(changePath), callback)) !== null && _subscribeDataValue2 !== void 0 ? _subscribeDataValue2 : noop;
}, [makeScopedPath, subscribeDataValue]);
const scopedGetDataValue = (0, _react.useCallback)(changePath => {
if (getDataValue) {
return getDataValue(makeScopedPath(changePath));
}
return getValueByPath(data, changePath);
}, [data, getDataValue, makeScopedPath]);
const scopedDataContext = (0, _react.useMemo)(() => ({
...dataContext,
data,
internalDataRef,
handlePathChange,
handlePathChangeUnvalidated,
updateDataValue,
subscribeDataValue: scopedSubscribeDataValue,
getDataValue: scopedGetDataValue
}), [data, dataContext, handlePathChange, handlePathChangeUnvalidated, internalDataRef, scopedGetDataValue, scopedSubscribeDataValue, updateDataValue]);
if (iterate) {
if (!Array.isArray(data)) {
return null;
}
return (0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
children: data.map((element, index) => {
const itemPath = joinPath(path, `/${index}`);
const makeScopedItemPath = changePath => {
return joinPath(itemPath, changePath);
};
return (0, _jsxRuntime.jsx)(ScopedContextProvider, {
value: {
...scopedDataContext,
data: element,
internalDataRef: {
current: element
},
handlePathChange: (changePath, value) => {
return handlePathChangeDataContext(makeScopedItemPath(changePath), value);
},
handlePathChangeUnvalidated: (changePath, value, options) => {
return handlePathChangeUnvalidatedDataContext(makeScopedItemPath(changePath), value, options);
},
updateDataValue: (changePath, value, options) => {
return updateDataValueDataContext(makeScopedItemPath(changePath), value, options);
},
subscribeDataValue: (changePath, callback) => {
var _subscribeDataValue3;
return (_subscribeDataValue3 = subscribeDataValue?.(makeScopedItemPath(changePath), callback)) !== null && _subscribeDataValue3 !== void 0 ? _subscribeDataValue3 : noop;
},
getDataValue: changePath => {
if (getDataValue) {
return getDataValue(makeScopedItemPath(changePath));
}
return getValueByPath(element, changePath);
}
},
children: children
}, `element${index}`);
})
});
}
return (0, _jsxRuntime.jsx)(ScopedContextProvider, {
value: scopedDataContext,
children: children
});
}
function ScopedContextProvider({
value,
children
}) {
const dataContextRef = (0, _react.useRef)(undefined);
dataContextRef.current = value;
return (0, _jsxRuntime.jsx)(_Context.default, {
value: value,
children: (0, _jsxRuntime.jsx)(_DataContextRefContext.default, {
value: dataContextRef,
children: children
})
});
}
function getValueByPath(data, path) {
if (path === '/') {
return data;
}
return data && _index.default.has(data, path) ? _index.default.get(data, path) : undefined;
}
function joinPath(path, changePath) {
if (!changePath || changePath === '/') {
return path;
}
if (!path || path === '/') {
return changePath;
}
return `${path}${changePath}`;
}
function noop() {
return undefined;
}
(0, _withComponentMarkers.default)(At, {
_supportsSpacingProps: true
});
var _default = exports.default = At;
//# sourceMappingURL=At.js.map