@mantine/form
Version:
Mantine form management library
39 lines (38 loc) • 1.57 kB
JavaScript
"use client";
import { clearListState } from "./clear-list-state.mjs";
//#region packages/@mantine/form/src/lists/change-error-indices.ts
/**
* Gets the part of the key after the path which can be an index
*/
function getIndexFromKeyAfterPath(key, path) {
const split = key.substring(path.length + 1).split(".")[0];
return parseInt(split, 10);
}
/**
* Changes the indices of every error that is after the given `index` with the given `change` at the given `path`.
* This requires that the errors are in the format of `path.index` and that the index is a number.
*/
function changeErrorIndices(path, index, errors, change) {
if (index === void 0) return errors;
const pathString = `${String(path)}`;
let clearedErrors = errors;
if (change === -1) clearedErrors = clearListState(`${pathString}.${index}`, clearedErrors);
const cloned = { ...clearedErrors };
const changedKeys = /* @__PURE__ */ new Set();
Object.entries(clearedErrors).filter(([key]) => {
if (!key.startsWith(`${pathString}.`)) return false;
const currIndex = getIndexFromKeyAfterPath(key, pathString);
if (Number.isNaN(currIndex)) return false;
return currIndex >= index;
}).forEach(([key, value]) => {
const currIndex = getIndexFromKeyAfterPath(key, pathString);
const newKey = key.replace(`${pathString}.${currIndex}`, `${pathString}.${currIndex + change}`);
cloned[newKey] = value;
changedKeys.add(newKey);
if (!changedKeys.has(key)) delete cloned[key];
});
return cloned;
}
//#endregion
export { changeErrorIndices };
//# sourceMappingURL=change-error-indices.mjs.map