UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

89 lines (88 loc) 4.66 kB
"use client"; import { useCallback, useContext, useMemo, useRef } from 'react'; import { assignPropsWithContext, extendDeep } from "../../../../shared/component-helper.js"; import FieldProviderContext from "./FieldProviderContext.js"; import DataContext from "../../DataContext/Context.js"; import SharedContext from "../../../../shared/Context.js"; function useFieldProvider(props) { var _dataContextRef$curre, _dataContextRef$curre2, _nestedContext$inheri2; const { formElement, FormStatus, overwriteProps, ...restProps } = props || {}; 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 || (_dataContextRef$curre2 = _dataContextRef$curre2.props) === null || _dataContextRef$curre2 === void 0 ? void 0 : _dataContextRef$curre2.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 { ...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 = useMemo(() => { var _dataContextRef$curre3; if (restProps.translations === undefined) { return restProps.translations; } const translations = extendDeep({}, sharedContext.translations, restProps === null || restProps === void 0 ? void 0 : restProps.translations, (_dataContextRef$curre3 = dataContextRef.current) === null || _dataContextRef$curre3 === void 0 || (_dataContextRef$curre3 = _dataContextRef$curre3.props) === null || _dataContextRef$curre3 === void 0 ? void 0 : _dataContextRef$curre3.translations); return translations; }, [restProps === null || restProps === void 0 ? void 0 : restProps.translations, sharedContext.translations]); if (mergedTranslations !== undefined) { sharedProviderParams.translations = mergedTranslations; } const extend = useCallback(fieldProps => { var _fieldProps$path, _nestedContext$inheri; const { required: requiredByContext } = dataContextRef.current; const key = overwriteProps && (fieldProps === null || fieldProps === void 0 || (_fieldProps$path = fieldProps.path) === null || _fieldProps$path === void 0 || (_fieldProps$path = _fieldProps$path.split('/')) === null || _fieldProps$path === void 0 ? void 0 : _fieldProps$path.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 ? { ...fieldProps, ...overwrite } : fieldProps; const required = requiredByContext !== null && requiredByContext !== void 0 ? requiredByContext : nestedContext === null || nestedContext === 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 || (_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