ra-core
Version:
Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React
49 lines • 2.28 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;
var react_hook_form_1 = require("react-hook-form");
var 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)
var useFormIsDirty = function () {
var dirtyFields = (0, react_hook_form_1.useFormState)().dirtyFields;
return (0, exports.checkHasDirtyFields)(dirtyFields);
};
exports.useFormIsDirty = useFormIsDirty;
var checkHasDirtyFields = function (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(function (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 (var _i = 0, value_1 = value; _i < value_1.length; _i++) {
var item = value_1[_i];
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