UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

147 lines (146 loc) 5.58 kB
"use strict"; "use client"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = GenerateSchema; exports.schemaParams = void 0; var _push = _interopRequireDefault(require("core-js-pure/stable/instance/push.js")); var _react = require("react"); var _index = _interopRequireWildcard(require("../utils/json-pointer/index.js")); var _Context = _interopRequireDefault(require("../DataContext/Context.js")); 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 _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } const schemaParams = exports.schemaParams = ['minLength', 'maxLength', 'pattern', 'description', 'min', 'max', 'multipleOf', 'exclusiveMinimum', 'exclusiveMaximum']; function GenerateSchema(props) { const { generateRef, filterData, log, children } = props || {}; const { fieldInternalsRef, valueInternalsRef, getDataValue, data, hasContext } = (0, _react.useContext)(_Context.default); const dataRef = (0, _react.useRef)({}); dataRef.current = data; const generate = (0, _react.useCallback)(() => { var _getDataValue; const schema = Object.entries(fieldInternalsRef?.current || {}).reduce((acc, [path, { props }]) => { if ((0, _index.isPath)(path)) { const objectKey = path.substring(1); const pathList = objectKey.split('/'); const slashCount = pathList.length; const type = props.valueType || 'string'; const propertyValue = { type }; for (const prop of schemaParams) { if (props[prop]) { propertyValue[prop] = props[prop]; } } if (slashCount > 1) { const nestedPath = ['']; for (const path of pathList) { (0, _push.default)(nestedPath).call(nestedPath, path); const pathToSet = nestedPath.join('/properties/'); const isLast = nestedPath.length - 1 === pathList.length; const existingValue = _index.default.has(acc, pathToSet) ? _index.default.get(acc, pathToSet) : null; const pathValue = isLast ? propertyValue : existingValue; if (isLast) { if (filterData?.[pathToSet] !== false) { _index.default.set(acc, pathToSet, pathValue); } } else { const pathValue = { type: 'object', ...existingValue }; const required = []; if (props.required) { (0, _push.default)(required).call(required, pathList.at(-1)); } if (existingValue?.required) { (0, _push.default)(required).call(required, ...existingValue.required); } if (required.length > 0) { pathValue.required = required; } if (filterData?.[pathToSet] !== false) { _index.default.set(acc, pathToSet, pathValue); } } } } else { if (filterData?.[path] !== false) { _index.default.set(acc.properties, path, propertyValue); } if (props.required) { var _context; (0, _push.default)(_context = acc.required).call(_context, objectKey); } } } return acc; }, { type: 'object', properties: {}, required: [] }); const propsOfFields = Object.entries(fieldInternalsRef?.current || {}).reduce((acc, [path, { props }]) => { if ((0, _index.isPath)(path)) { const propertyValue = {}; for (const prop in props) { if (props[prop] !== undefined && typeof props[prop] !== 'function') { propertyValue[prop] = props[prop]; } } _index.default.set(acc, path, propertyValue); } return acc; }, {}); const propsOfValues = Object.entries(valueInternalsRef?.current || {}).reduce((acc, [path, { props }]) => { if ((0, _index.isPath)(path)) { const propertyValue = {}; for (const prop in props) { if (props[prop] !== undefined && typeof props[prop] !== 'function') { propertyValue[prop] = props[prop]; } } _index.default.set(acc, path, propertyValue); } return acc; }, {}); if (schema.required.length === 0) { delete schema.required; } return { schema, data: (_getDataValue = getDataValue?.('/')) !== null && _getDataValue !== void 0 ? _getDataValue : dataRef.current, propsOfFields, propsOfValues }; }, [fieldInternalsRef, filterData, getDataValue, valueInternalsRef]); if (hasContext) { if (log) { console.log(generate().schema); } if (generateRef) { const mutableGenerateRef = generateRef; mutableGenerateRef.current = generate; } } return children; } //# sourceMappingURL=GenerateSchema.js.map