ra-core
Version:
Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React
79 lines • 3.98 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useApplyInputDefaultValues = void 0;
const react_1 = require("react");
const react_hook_form_1 = require("react-hook-form");
const get_js_1 = __importDefault(require("lodash/get.js"));
const controller_1 = require("../controller/index.cjs");
const core_1 = require("../core/index.cjs");
/*
* This hook updates the input with the default value if default value is present
* and field input is not already populated or dirty
*/
const useApplyInputDefaultValues = ({ inputProps, isArrayInput, fieldArrayInputControl, }) => {
const { defaultValue, source, disabled } = inputProps;
const finalSource = (0, core_1.useWrappedSource)(source);
const record = (0, controller_1.useRecordContext)(inputProps);
const { getValues, resetField, reset, subscribe } = (0, react_hook_form_1.useFormContext)();
const recordValue = (0, get_js_1.default)(record, finalSource);
const formValue = (0, get_js_1.default)(getValues(), finalSource);
const isDirty = (0, react_1.useRef)(undefined);
(0, react_1.useEffect)(() => {
return subscribe({
// Even though we only need dirtyFields, we subscribe to values as well to
// ensure we properly receive dirtyFields updates for newly added items in an ArrayInput
formState: { values: true, dirtyFields: true },
callback: ({ dirtyFields }) => {
isDirty.current = (0, get_js_1.default)(dirtyFields ?? {}, finalSource, false);
},
});
}, [finalSource, subscribe]);
(0, react_1.useEffect)(() => {
if (defaultValue == null ||
formValue != null ||
// When the input is disabled, its value may always be undefined, no matter the default value.
// This prevents from trying to reset the value indefinitely.
disabled ||
// We check strictly for undefined to avoid setting default value
// when the field is null
recordValue !== undefined ||
isDirty.current === true) {
return;
}
// Side note: For Array Input but checked for all to avoid possible regression
// Since we use get(record, source), if source is like foo.23.bar,
// this effect will run. However we only want to set the default value
// for the subfield bar if the record actually has a value for foo.23
const pathContainsIndex = finalSource
.split('.')
.some(pathPart => numericRegex.test(pathPart));
if (pathContainsIndex) {
const parentPath = finalSource.split('.').slice(0, -1).join('.');
const parentValue = (0, get_js_1.default)(getValues(), parentPath);
if (parentValue == null) {
// the parent is undefined, so we don't want to set the default value
return;
}
}
if (isArrayInput) {
if (!fieldArrayInputControl) {
throw new Error('useApplyInputDefaultValues: No fieldArrayInputControl passed in props for array input usage');
}
// We need to update inputs nested in array using react hook forms
// own array controller rather then the generic reset to prevent control losing
// context of the nested inputs
fieldArrayInputControl.replace(defaultValue);
// resets the form so that control no longer sees the form as dirty after
// defaults applied
reset({}, { keepValues: true });
return;
}
resetField(finalSource, { defaultValue });
});
};
exports.useApplyInputDefaultValues = useApplyInputDefaultValues;
const numericRegex = /^\d+$/;
//# sourceMappingURL=useApplyInputDefaultValues.js.map