How to dim savebutton if form does not change by useForm?
How to dim savebutton if form does not change by useForm?
useForm in refine, you can utilize the saveButtonProps returned by the useForm hook. This prop automatically handles the disabled state of the save button based on whether the form has changed or not. Here's how you can implement it:saveButtonProps is passed to the <Edit> component, which will automatically handle the disabled state of the save button Ant Design UI - Save Button (https://refine.dev/docs/ui-integrations/ant-design/components/buttons/save-button/).formState.isDirty property to determine if the form has changed:formState.isDirty property from the useForm hook in React Hook Form. This property will be true if the form data has been changed from its initial values, and false if the form data matches the initial values (even if changes were made and then undone).isDirty property is used to disable the save button when the form data matches the initial values. This means that if a user makes changes to the form and then undoes those changes, the save button will be disabled.<Edit>formState.isDirtyformState.isDirtyisDirtyimport { Edit, useForm } from "@refinedev/antd";
import { Form, Input } from "antd";
const PostEdit: React.FC = () => {
const { formProps, saveButtonProps } = useForm<IPost>();
return (
<Edit saveButtonProps={saveButtonProps}>
<Form {...formProps} layout="vertical">
<Form.Item
label="Title"
name="title"
rules={[
{
required: true,
},
]}
>
<Input />
</Form.Item>
</Form>
</Edit>
);
};const { register, formState: { isDirty } } = useForm();
// In your JSX:
<button
disabled={!isDirty}
type="submit"
>
Save
</button>const { register, formState: { isDirty } } = useForm();
// In your JSX:
<button
disabled={!isDirty}
type="submit"
>
Save
</button>