@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
95 lines (94 loc) • 5.03 kB
JavaScript
"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, _nestedContext$inheri2;
const {
formElement,
FormStatus,
overwriteProps,
...restProps
} = props || {};
const nestedContext = (0, _react.useContext)(_FieldProviderContext.default);
const inheritedProps = nestedContext === null || nestedContext === void 0 ? void 0 : nestedContext.inheritedContext;
const sharedContext = (0, _react.useContext)(_Context2.default);
const dataContextRef = (0, _react.useRef)();
dataContextRef.current = (0, _react.useContext)(_Context.default);
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 = (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)(() => {
var _dataContextRef$curre3;
if (restProps.translations === undefined) {
return restProps.translations;
}
const translations = (0, _componentHelper.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 = (0, _react.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 ? (0, _componentHelper.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
};
}
var _default = exports.default = useFieldProvider;
//# sourceMappingURL=useFieldProvider.js.map