@wener/console
Version:
Base console UI toolkit
34 lines (33 loc) • 1.65 kB
JavaScript
function _type_of(obj) {
"@swc/helpers - typeof";
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
}
export function getDirtyFields(param) {
var dirtyFields = param.formState.dirtyFields, getValues = param.getValues, formValues = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : getValues();
// https://github.com/orgs/react-hook-form/discussions/9472
if ((typeof dirtyFields === "undefined" ? "undefined" : _type_of(dirtyFields)) !== 'object' || dirtyFields === null || !formValues) {
return {};
}
return Object.keys(dirtyFields).reduce(function(accumulator, key) {
var isDirty = dirtyFields[key];
var value = formValues[key];
// If it's an array, apply the logic recursively to each item
if (Array.isArray(isDirty)) {
// eslint-disable-next-line no-underscore-dangle
var _dirtyFields = isDirty.map(function(item, index) {
return getDirtyFields(item, value[index]);
});
if (_dirtyFields.length > 0) {
// eslint-disable-next-line no-param-reassign
accumulator[key] = _dirtyFields;
}
} else if ((typeof isDirty === "undefined" ? "undefined" : _type_of(isDirty)) === 'object' && isDirty !== null) {
// eslint-disable-next-line no-param-reassign
accumulator[key] = getDirtyFields(isDirty, value);
} else if (isDirty) {
// eslint-disable-next-line no-param-reassign
accumulator[key] = value;
}
return accumulator;
}, {});
}