Search code examples
reactjsadmin-on-restreact-admin

Create custom form in react-admin


I am using Edit and SimpleForm from react-admin. How do I create a custom form to allow customised action and type on submit?

App.js

<Resource name="category" list={CategoryList} edit={CategoryEdit}  />

index.js

<Edit actions={<CategoryEditActions />} title={<CategoryTitle />} {...props} >
    <SimpleForm>
      <DisabledInput source="id" />
      <DisabledInput source="code" />
      <TextInput source="name" />
   </SimpleForm>

Here the api call would be /category/:categoryId with PUT request. I want to modify url to /category/:categoryId/test with method as POST. Is there any way to customize this?

I have handled this in my CustomDataProvider -

 case UPDATE:
             if(resource === 'category'){
                    options.method = 'POST';
                    url = `${apiUrl}/${resource}/${params.id}/test`;
                } else {
                    options.method = 'PUT';
                    url = `${apiUrl}/${resource}/${params.id}`;
                }
                break;

Is there any other way to handle it?


Solution

  • This is the job of your dataProvider in react-admin (restClient in admin-on-rest). You'll have to create a custom one:

    You'll have to check the resource and type then build the fetch options by yourself.