denwa-react-shared
Version:
122 lines (104 loc) • 3.11 kB
Markdown
---
name: admin-forms
description: >
Implement entity editor forms using DrawerForm and specialized inputs.
Covers form submission, language switching, and integration with
AdminTable actions.
type: framework
library: denwa-react-shared
framework: react
library_version: "1.0.88"
requires:
- session-auth
---
Most entity management in the admin panel happens within `Drawer` panels using `BaseDrawerForm`. This component standardizes the appearance of save buttons and multi-language controls.
```tsx
import { BaseDrawerForm, AdminDrawerFormProps } from 'denwa-react-shared';
const UserForm = ({ id, action, onClose, onRefetch }: AdminDrawerFormProps<'create' | 'update' | 'read'>) => {
const { control, handleSubmit } = useForm();
const isReadOnly = action === 'read';
return (
<BaseDrawerForm
saveText="Сохранить"
submitButtonText={action === 'create' ? 'Создать' : 'Обновить'}
isVisibleSubmit={!isReadOnly}
onSubmitClick={handleSubmit(onSave)}
>
<Controller
name="name"
control={control}
render={({ field }) => <Input {...field} disabled={isReadOnly} />}
/>
{/* ... form content */}
</BaseDrawerForm>
);
};
```
If the entity supports multiple languages, enable `isVisibleLanguage` and provide `languagesData`.
```tsx
<BaseDrawerForm
isVisibleLanguage
language={currentLang}
languagesData={[
{ label: 'Русский', value: 'ru' },
{ label: 'English', value: 'en' },
]}
onChangeLang={(lang) => setLang(lang)}
// Optional: Bulk translation button
isVisibleLanguageButton
translateAllText="Перевести все"
onTranslateAllClick={handleTranslate}
>
{/* ... fields */}
</BaseDrawerForm>
```
The `AdminTable` orchestration passes `id`, `action`, `onClose`, and `onRefetch` to the `drawerContent` component.
```tsx
// In AdminTable:
<AdminTable
drawerContent={UserForm}
readAction="read"
createAction="create"
updateAction="update"
// ...
/>
```
Wrong:
```tsx
import { Form } from 'antd';
return <Form>{children}</Form>;
```
Correct:
```tsx
import { BaseDrawerForm } from 'denwa-react-shared';
return <BaseDrawerForm>{children}</BaseDrawerForm>;
```
`BaseDrawerForm` wraps `antd` Form but adds standardized footer buttons and integrated logic for language switching and submission loading states.
Source: maintainer interview
Wrong:
```tsx
const UserForm = ({ action }) => {
return <BaseDrawerForm>{/* inputs are always enabled */}</BaseDrawerForm>;
};
```
Correct:
```tsx
const UserForm = ({ action }) => {
const isReadOnly = action === 'read';
return (
<BaseDrawerForm isVisibleSubmit={!isReadOnly}>
<Input disabled={isReadOnly} />
</BaseDrawerForm>
);
};
```
When `action` is 'read', the form should visually hide the submit button and disable all input fields.
Source: src/shared/ui/admin-table/index.tsx