-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] Problems with useForm inside Custom Page (antd UI framework) #1729
Comments
Hey @eliasfeijo 👋, You can use refetch function of queryResult of useForm for "RefreshButton". For more details; https://refine.dev/docs/core/hooks/useForm/#action-edit |
Hello @omeraplak, thank you for the quick response! Previously, I didn't see this section on the documentation (about the "edit" action and the Fixed code:
import {
IResourceComponentsProps,
useNavigation,
useTranslate,
} from "@pankod/refine-core";
import { Edit, Form, Input, RefreshButton, useForm } from "@pankod/refine-antd";
import { useParams } from "react-router-dom";
import { IEmployee } from "interfaces";
import { useContext, useEffect } from "react";
import { AppContext } from "contexts/AppContext";
export const EmployeeEdit: React.FC<IResourceComponentsProps> = () => {
const { state } = useContext(AppContext);
const { id } = useParams();
const t = useTranslate();
const { push } = useNavigation();
const {
form,
setId,
formProps,
saveButtonProps,
queryResult,
onFinish,
formLoading,
} = useForm<IEmployee>({
action: "edit",
resource: `stores/${state.storeId}/employees`,
});
useEffect(() => {
setId(id);
}, [setId, id]);
const onSubmit = async () => {
await onFinish?.();
push("/employees");
};
return (
<Edit
isLoading={formLoading}
saveButtonProps={saveButtonProps}
resource={`stores/${state.storeId}/employees`}
title={t("employees.titles.edit")}
recordItemId={id}
pageHeaderProps={{
extra: [
<RefreshButton
resourceName={`stores/${state.storeId}/employees`}
recordItemId={id}
onClick={() => {
queryResult?.refetch();
}}
/>,
],
}}
>
<Form form={form} {...formProps} layout="vertical" onFinish={onSubmit}>
<Form.Item
label={t("employees.fields.name")}
name="name"
rules={[
{
required: true,
},
]}
>
<Input />
</Form.Item>
</Form>
</Edit>
);
}; |
Now that I'm thinking about it, it would be even better if we could just set the Is it possible, @omeraplak? It's totally not a critical feature, but it would be nice to have in a future version of refine. :) |
Hey @eliasfeijo , Actually, you don't need to override RefreshButton's onClick function. It should work this way.
|
I'm trying to make an Edit page for a resource, using a Custom Page. But I'm having some problems with the
useForm
hook, using the antdForm
component.useForm
hook doesn't automatically fetch the initial data, to populate the Form. I had to manually fetch it, using the hookuseOne
, and fill the data inside the form. I'm already setting theresource
parameter inside theuseForm
hook andEdit
component (withrecordItemId
aswell), but it only works for the form submit.<Input>
fields inside the<Form.Item>
fields don't update the value prop that I've set, using a variable withuseState
. But it works fine if I put the<Input>
field outside theForm
component. I've fixed it using theform
return value of the hookuseForm
, and manually calling the functionform.setFieldsValue({ name });
when the field value changes.<RefreshButton>
uses the URL from the page to refetch the resource, instead of using the providedresource
parameter of theuseForm
hook. I had to manually set the RefreshButton inside the Form (usingpageHeaderProps
), and use therefetch
return value of the hookuseOne
, and set the data manually for it to work.Here is the code that I've implemented to make it work:
App.tsx
:pages/employees/edit.tsx
(Custom Page):I was expecting that the
useForm
hook behaved the same asresources
for Custom Pages, so that I wouldn't have to implement all this logic by myself, so I'm opening this issue as a bug.The text was updated successfully, but these errors were encountered: