UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

101 lines (100 loc) 4.25 kB
"use strict"; "use client"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = require("react"); var _componentHelper = require("../../../../shared/component-helper.js"); var _FieldProviderContext = _interopRequireDefault(require("./FieldProviderContext.js")); var _Context = _interopRequireDefault(require("../../DataContext/Context.js")); var _Context2 = _interopRequireDefault(require("../../../../shared/Context.js")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function useFieldProvider(props) { var _dataContextRef$curre, _dataContextRef$curre2, _dataContextRef$curre3; const { formElement, FormStatus, overwriteProps, ...restProps } = props || {}; const nestedContext = (0, _react.useContext)(_FieldProviderContext.default); const inheritedProps = nestedContext?.inheritedContext; const sharedContext = (0, _react.useContext)(_Context2.default); const dataContextRef = (0, _react.useRef)(undefined); dataContextRef.current = (0, _react.useContext)(_Context.default); const locale = (_dataContextRef$curre = dataContextRef.current?.props?.locale) !== null && _dataContextRef$curre !== void 0 ? _dataContextRef$curre : restProps?.locale; const nestedFieldProps = (0, _react.useMemo)(() => { if (inheritedProps && Object.keys(inheritedProps).length > 0) { return { ...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; } const mergedTranslations = (0, _react.useMemo)(() => { if (restProps.translations === undefined) { return restProps.translations; } const translations = (0, _componentHelper.extendDeep)({}, sharedContext.translations, restProps?.translations, dataContextRef.current?.props?.translations); return translations; }, [restProps?.translations, sharedContext.translations]); if (mergedTranslations !== undefined) { sharedProviderParams.translations = mergedTranslations; } const translationsLoader = (_dataContextRef$curre2 = dataContextRef.current?.props?.translationsLoader) !== null && _dataContextRef$curre2 !== void 0 ? _dataContextRef$curre2 : restProps?.translationsLoader; if (translationsLoader) { sharedProviderParams.translationsLoader = translationsLoader; } const messageFormatter = (_dataContextRef$curre3 = dataContextRef.current?.props?.messageFormatter) !== null && _dataContextRef$curre3 !== void 0 ? _dataContextRef$curre3 : restProps?.messageFormatter; if (messageFormatter) { sharedProviderParams.messageFormatter = messageFormatter; } const extend = (0, _react.useCallback)(fieldProps => { const { required: requiredByContext } = dataContextRef.current; const key = overwriteProps && fieldProps?.path?.split('/')?.pop(); const overwrite = overwriteProps?.[key]; if (overwrite && fieldProps?.schema) { Object.keys(fieldProps.schema).forEach(key => { if (overwrite?.[key]) { fieldProps.schema[key] = overwrite[key]; } }); } const props = overwrite ? { ...fieldProps, ...overwrite } : fieldProps; const required = requiredByContext !== null && requiredByContext !== void 0 ? requiredByContext : nestedContext?.inheritedContext?.required; const value = typeof required !== 'undefined' || Object.keys(nestedFieldProps).length > 0 ? (0, _componentHelper.assignPropsWithContext)(props, { required }, nestedFieldProps) : props; return value; }, [nestedContext?.inheritedContext?.required, nestedFieldProps, overwriteProps]); return { extend, inheritedProps: restProps, inheritedContext: nestedFieldProps, sharedProviderParams }; } var _default = exports.default = useFieldProvider; //# sourceMappingURL=useFieldProvider.js.map