UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

83 lines (82 loc) 5.59 kB
"use client"; import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; const _excluded = ["formElement", "FormStatus", "overwriteProps"]; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import { useCallback, useContext, useMemo, useRef } from 'react'; import { assignPropsWithContext, extendDeep } from '../../../../shared/component-helper'; import FieldProviderContext from './FieldProviderContext'; import DataContext from '../../DataContext/Context'; import SharedContext from '../../../../shared/Context'; function useFieldProvider(props) { var _dataContextRef$curre, _dataContextRef$curre2, _dataContextRef$curre3, _nestedContext$inheri2; const _ref = props || {}, { formElement, FormStatus, overwriteProps } = _ref, restProps = _objectWithoutProperties(_ref, _excluded); const nestedContext = useContext(FieldProviderContext); const inheritedProps = nestedContext === null || nestedContext === void 0 ? void 0 : nestedContext.inheritedContext; const sharedContext = useContext(SharedContext); const dataContextRef = useRef(); dataContextRef.current = useContext(DataContext); const locale = (_dataContextRef$curre = (_dataContextRef$curre2 = dataContextRef.current) === null || _dataContextRef$curre2 === void 0 ? void 0 : (_dataContextRef$curre3 = _dataContextRef$curre2.props) === null || _dataContextRef$curre3 === void 0 ? void 0 : _dataContextRef$curre3.locale) !== null && _dataContextRef$curre !== void 0 ? _dataContextRef$curre : restProps === null || restProps === void 0 ? void 0 : restProps.locale; const nestedFieldProps = useMemo(() => { if (inheritedProps && Object.keys(inheritedProps).length > 0) { return _objectSpread(_objectSpread({}, inheritedProps), restProps); } return restProps; }, [inheritedProps, restProps]); const sharedProviderParams = {}; if (typeof nestedFieldProps.disabled === 'boolean') { sharedProviderParams.formElement = { disabled: nestedFieldProps.disabled }; } if (formElement) { sharedProviderParams.formElement = formElement; } if (FormStatus) { sharedProviderParams.FormStatus = FormStatus; } if (locale) { sharedProviderParams.locale = locale; } sharedProviderParams.translations = useMemo(() => { var _dataContextRef$curre4, _dataContextRef$curre5; const translations = extendDeep({}, sharedContext.translations, restProps === null || restProps === void 0 ? void 0 : restProps.translations, (_dataContextRef$curre4 = dataContextRef.current) === null || _dataContextRef$curre4 === void 0 ? void 0 : (_dataContextRef$curre5 = _dataContextRef$curre4.props) === null || _dataContextRef$curre5 === void 0 ? void 0 : _dataContextRef$curre5.translations); return translations; }, [restProps === null || restProps === void 0 ? void 0 : restProps.translations, sharedContext.translations]); const extend = useCallback(fieldProps => { var _fieldProps$path, _fieldProps$path$spli, _nestedContext$inheri; const { required: requiredByContext } = dataContextRef.current; const key = overwriteProps && (fieldProps === null || fieldProps === void 0 ? void 0 : (_fieldProps$path = fieldProps.path) === null || _fieldProps$path === void 0 ? void 0 : (_fieldProps$path$spli = _fieldProps$path.split('/')) === null || _fieldProps$path$spli === void 0 ? void 0 : _fieldProps$path$spli.pop()); const overwrite = overwriteProps === null || overwriteProps === void 0 ? void 0 : overwriteProps[key]; if (overwrite && fieldProps !== null && fieldProps !== void 0 && fieldProps.schema) { Object.keys(fieldProps.schema).forEach(key => { if (overwrite !== null && overwrite !== void 0 && overwrite[key]) { fieldProps.schema[key] = overwrite[key]; } }); } const props = overwrite ? _objectSpread(_objectSpread({}, fieldProps), overwrite) : fieldProps; const required = requiredByContext !== null && requiredByContext !== void 0 ? requiredByContext : nestedContext === null || nestedContext === void 0 ? void 0 : (_nestedContext$inheri = nestedContext.inheritedContext) === null || _nestedContext$inheri === void 0 ? void 0 : _nestedContext$inheri.required; const value = typeof required !== 'undefined' || Object.keys(nestedFieldProps).length > 0 ? assignPropsWithContext(props, { required }, nestedFieldProps) : props; return value; }, [nestedContext === null || nestedContext === void 0 ? void 0 : (_nestedContext$inheri2 = nestedContext.inheritedContext) === null || _nestedContext$inheri2 === void 0 ? void 0 : _nestedContext$inheri2.required, nestedFieldProps, overwriteProps]); return { extend, inheritedProps: restProps, inheritedContext: nestedFieldProps, sharedProviderParams }; } export default useFieldProvider; //# sourceMappingURL=useFieldProvider.js.map