refactor: using isDirty in forms
This commit is contained in:
@ -1,17 +1,14 @@
|
||||
import { FC } from "react";
|
||||
import { isEqual } from "lodash";
|
||||
import { Button, Group } from "@mantine/core";
|
||||
import { UseFormReturnType } from "@mantine/form";
|
||||
import { DealForm } from "@/app/deals/drawers/DealEditorDrawer/tabs/GeneralTab/GeneralTab";
|
||||
import { DealSchema } from "@/lib/client";
|
||||
|
||||
type Props = {
|
||||
form: UseFormReturnType<DealForm>;
|
||||
initialValues: Partial<DealSchema>;
|
||||
onDelete: () => void;
|
||||
};
|
||||
|
||||
const Footer: FC<Props> = ({ form, initialValues, onDelete }) => {
|
||||
const Footer: FC<Props> = ({ form, onDelete }) => {
|
||||
return (
|
||||
<Group
|
||||
justify={"space-between"}
|
||||
@ -19,14 +16,14 @@ const Footer: FC<Props> = ({ form, initialValues, onDelete }) => {
|
||||
<Group wrap={"nowrap"}>
|
||||
<Button
|
||||
type={"submit"}
|
||||
disabled={isEqual(form.values, initialValues)}
|
||||
disabled={!form.isDirty()}
|
||||
variant={"filled"}>
|
||||
Сохранить
|
||||
</Button>
|
||||
<Button
|
||||
type={"reset"}
|
||||
onClick={() => form.reset()}
|
||||
disabled={isEqual(form.values, initialValues)}
|
||||
disabled={!form.isDirty()}
|
||||
variant={"default"}>
|
||||
Отменить
|
||||
</Button>
|
||||
|
||||
@ -29,7 +29,7 @@ const GeneralTab: FC<Props> = ({ value, onDelete, onChange }) => {
|
||||
});
|
||||
|
||||
const onSubmit = (values: DealForm) => {
|
||||
form.setInitialValues(values);
|
||||
form.resetDirty();
|
||||
onChange({
|
||||
...values,
|
||||
board: values.board!,
|
||||
@ -63,7 +63,6 @@ const GeneralTab: FC<Props> = ({ value, onDelete, onChange }) => {
|
||||
/>
|
||||
<Footer
|
||||
form={form}
|
||||
initialValues={value}
|
||||
onDelete={() => onDelete(value)}
|
||||
/>
|
||||
</Stack>
|
||||
|
||||
Reference in New Issue
Block a user