ra-core
Version:
Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React
48 lines • 2.19 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkHasDirtyFields = exports.useFormIsDirty = void 0;
const react_hook_form_1 = require("react-hook-form");
const isEmpty_js_1 = __importDefault(require("lodash/isEmpty.js"));
// useFormState().isDirty might differ from useFormState().dirtyFields (https://github.com/react-hook-form/react-hook-form/issues/4740)
const useFormIsDirty = () => {
const { dirtyFields } = (0, react_hook_form_1.useFormState)();
return (0, exports.checkHasDirtyFields)(dirtyFields);
};
exports.useFormIsDirty = useFormIsDirty;
const checkHasDirtyFields = (dirtyFields) => {
// dirtyFields can contains simple keys with boolean values, nested objects or arrays
// We must ignore values that are false
return Object.values(dirtyFields).some(value => {
if (typeof value === 'boolean') {
return value;
}
else if (Array.isArray(value)) {
// Some arrays contain only booleans (scalar arrays), some arrays contain objects (object arrays)
for (const item of value) {
if (item === true) {
return true;
}
// FIXME: because we currently don't set default values correctly for arrays,
// new items are either empty objects, or undefined in dirtyFields. Consider them as dirty.
if ((typeof item === 'object' && (0, isEmpty_js_1.default)(item)) ||
item === undefined) {
return true;
}
if (typeof item === 'object' &&
item !== null &&
(0, exports.checkHasDirtyFields)(item)) {
return true;
}
}
}
else if (typeof value === 'object' && value !== null) {
return (0, exports.checkHasDirtyFields)(value);
}
return false;
});
};
exports.checkHasDirtyFields = checkHasDirtyFields;
//# sourceMappingURL=useFormIsDirty.js.map