UNPKG

@mantine/form

Version:

Mantine form management library

56 lines (55 loc) 2.15 kB
"use client"; import { changeErrorIndices } from "../../lists/change-error-indices.mjs"; import { reorderErrors } from "../../lists/reorder-errors.mjs"; import { reorderPath } from "../../paths/reorder-path.mjs"; import { insertPath } from "../../paths/insert-path.mjs"; import { removePath } from "../../paths/remove-path.mjs"; import { replacePath } from "../../paths/replace-path.mjs"; import { useCallback } from "react"; //#region packages/@mantine/form/src/hooks/use-form-list/use-form-list.ts function useFormList({ $values, $errors, $status, $watch }) { return { reorderListItem: useCallback((path, payload) => { const previousValues = $values.refValues.current; $status.clearFieldDirty(path); $errors.setErrors((errs) => reorderErrors(path, payload, errs)); $values.setValues({ values: reorderPath(path, payload, $values.refValues.current), updateState: true }); $watch.notifyWatchSubscribers(previousValues); }, []), removeListItem: useCallback((path, index) => { const previousValues = $values.refValues.current; $status.clearFieldDirty(path); $errors.setErrors((errs) => changeErrorIndices(path, index, errs, -1)); $values.setValues({ values: removePath(path, index, $values.refValues.current), updateState: true }); $watch.notifyWatchSubscribers(previousValues); }, []), insertListItem: useCallback((path, item, index) => { const previousValues = $values.refValues.current; $status.clearFieldDirty(path); $errors.setErrors((errs) => changeErrorIndices(path, index, errs, 1)); $values.setValues({ values: insertPath(path, item, index, $values.refValues.current), updateState: true }); $watch.notifyWatchSubscribers(previousValues); }, []), replaceListItem: useCallback((path, index, item) => { const previousValues = $values.refValues.current; $status.clearFieldDirty(path); $values.setValues({ values: replacePath(path, item, index, $values.refValues.current), updateState: true }); $watch.notifyWatchSubscribers(previousValues); }, []) }; } //#endregion export { useFormList }; //# sourceMappingURL=use-form-list.mjs.map