From 7382c275b3977c4655ca9558f4bf144d314beeff Mon Sep 17 00:00:00 2001 From: erwanMarmelab Date: Thu, 19 Sep 2024 18:18:11 +0200 Subject: [PATCH] Add "standalaone usage" doc section in dialog views --- docs/CreateDialog.md | 110 +++++++++++++++++++++++++++++++++++++++++++ docs/EditDialog.md | 110 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 220 insertions(+) diff --git a/docs/CreateDialog.md b/docs/CreateDialog.md index f7b9cf912b2..9e565ea21da 100644 --- a/docs/CreateDialog.md +++ b/docs/CreateDialog.md @@ -295,3 +295,113 @@ const CompanyShow = () => ( {% endraw %} In the above example, `` is used to create a new employee for the current company. [The `` component](./WithRecord.md) helps to set the new employee company id by default. + +## Standalone Usage + +`` also offer the ability to work standalone, without using the Router's location. + +To allow for standalone usage, they require the following props: + +- `isOpen`: a boolean holding the open/close state +- `open`: a function that will be called when a component needs to open the dialog (e.g. a button) +- `close`: a function that will be called when a component needs to close the dialog (e.g. the dialog's close button) + +**Tip:** These props are exactly the same as what is stored inside a `FormDialogContext`. This means that you can also rather provide your own `FormDialogContext` with these values, and render your dialog component inside it, to activate standalone mode. + +Below is an example of an `` page, including a 'create a new customer' button, that opens a fully controlled ``. + + + +```tsx +import React, { useCallback, useState } from 'react'; +import { + Button, + Datagrid, + DateField, + DateInput, + Edit, + ReferenceManyField, + required, + SelectField, + SelectInput, + SimpleForm, + TextField, + TextInput, + useRecordContext, +} from 'react-admin'; +import { CreateDialog } from '@react-admin/ra-form-layout'; + +const sexChoices = [ + { id: 'male', name: 'Male' }, + { id: 'female', name: 'Female' }, +]; + +const CustomerForm = (props: any) => ( + + + + + + +); + +const EmployerSimpleFormWithFullyControlledDialogs = () => { + const record = useRecordContext(); + + const [isCreateDialogOpen, setIsCreateDialogOpen] = useState(false); + const openCreateDialog = useCallback(() => { + setIsCreateDialogOpen(true); + }, []); + const closeCreateDialog = useCallback(() => { + setIsCreateDialogOpen(false); + }, []); + + return ( + + + + +