@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
87 lines (86 loc) • 2.7 kB
JavaScript
"use client";
import { isValidElement, useCallback, useContext, useRef } from 'react';
import pointer from "../utils/json-pointer/index.js";
import DataContext from "../DataContext/Context.js";
export default function ListAllProps(props) {
const {
log,
generateRef,
filterData,
children
} = props || {};
const {
fieldInternalsRef,
valueInternalsRef,
data,
hasContext
} = useContext(DataContext);
const dataRef = useRef({});
dataRef.current = data;
const generate = useCallback(() => {
const includeSchema = Boolean(log);
const propsOfFields = Object.entries((fieldInternalsRef === null || fieldInternalsRef === void 0 ? void 0 : fieldInternalsRef.current) || {}).reduce((acc, [path, {
props
}]) => {
if (path.startsWith('/')) {
const propertyValue = {};
for (const prop in props) {
const value = props[prop];
if (value === undefined) {
continue;
}
if (prop === 'schema') {
if (!includeSchema) {
continue;
}
try {
propertyValue[prop] = typeof value === 'function' ? value(props) : value;
} catch {
if (typeof value !== 'function') {
propertyValue[prop] = value;
}
}
continue;
}
if (typeof value !== 'function' && !isValidElement(value)) {
propertyValue[prop] = value;
}
}
if ((filterData === null || filterData === void 0 ? void 0 : filterData[path]) !== false) {
pointer.set(acc, path, propertyValue);
}
}
return acc;
}, {});
const propsOfValues = Object.entries((valueInternalsRef === null || valueInternalsRef === void 0 ? void 0 : valueInternalsRef.current) || {}).reduce((acc, [path, {
props
}]) => {
if (path.startsWith('/')) {
const propertyValue = {};
for (const prop in props) {
if (props[prop] !== undefined && typeof props[prop] !== 'function' && !isValidElement(props[prop])) {
propertyValue[prop] = props[prop];
}
}
if ((filterData === null || filterData === void 0 ? void 0 : filterData[path]) !== false) {
pointer.set(acc, path, propertyValue);
}
}
return acc;
}, {});
return {
propsOfFields,
propsOfValues
};
}, [fieldInternalsRef, filterData, valueInternalsRef]);
if (hasContext) {
if (log) {
console.log(generate());
}
if (generateRef) {
generateRef.current = generate;
}
}
return children;
}
//# sourceMappingURL=ListAllProps.js.map