-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use react-hook-form to validate Settings/DataModel/Field Ab…
…out form Part of #4295
- Loading branch information
1 parent
831ee2d
commit 16cfb12
Showing
10 changed files
with
313 additions
and
249 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 0 additions & 70 deletions
70
...wenty-front/src/modules/settings/data-model/components/SettingsObjectFieldFormSection.tsx
This file was deleted.
Oops, something went wrong.
23 changes: 0 additions & 23 deletions
23
...les/settings/data-model/components/__stories__/SettingsObjectFieldFormSection.stories.tsx
This file was deleted.
Oops, something went wrong.
87 changes: 87 additions & 0 deletions
87
...c/modules/settings/data-model/fields/forms/components/SettingsDataModelFieldAboutForm.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { Controller, useFormContext } from 'react-hook-form'; | ||
import styled from '@emotion/styled'; | ||
import { z } from 'zod'; | ||
|
||
import { FieldMetadataItem } from '@/object-metadata/types/FieldMetadataItem'; | ||
import { fieldMetadataItemSchema } from '@/object-metadata/validation-schemas/fieldMetadataItemSchema'; | ||
import { IconPicker } from '@/ui/input/components/IconPicker'; | ||
import { TextArea } from '@/ui/input/components/TextArea'; | ||
import { TextInput } from '@/ui/input/components/TextInput'; | ||
|
||
export const settingsDataModelFieldAboutFormSchema = | ||
fieldMetadataItemSchema.pick({ | ||
description: true, | ||
icon: true, | ||
label: true, | ||
}); | ||
|
||
type SettingsDataModelFieldAboutFormValues = z.infer< | ||
typeof settingsDataModelFieldAboutFormSchema | ||
>; | ||
|
||
type SettingsDataModelFieldAboutFormProps = { | ||
disabled?: boolean; | ||
fieldMetadataItem?: FieldMetadataItem; | ||
}; | ||
|
||
const StyledInputsContainer = styled.div` | ||
display: flex; | ||
gap: ${({ theme }) => theme.spacing(2)}; | ||
margin-bottom: ${({ theme }) => theme.spacing(2)}; | ||
width: 100%; | ||
`; | ||
|
||
export const SettingsDataModelFieldAboutForm = ({ | ||
disabled, | ||
fieldMetadataItem, | ||
}: SettingsDataModelFieldAboutFormProps) => { | ||
const { control } = useFormContext<SettingsDataModelFieldAboutFormValues>(); | ||
|
||
return ( | ||
<> | ||
<StyledInputsContainer> | ||
<Controller | ||
name="icon" | ||
control={control} | ||
defaultValue={fieldMetadataItem?.icon ?? 'IconUsers'} | ||
render={({ field: { onChange, value } }) => ( | ||
<IconPicker | ||
disabled={disabled} | ||
selectedIconKey={value} | ||
onChange={({ iconKey }) => onChange(iconKey)} | ||
variant="primary" | ||
/> | ||
)} | ||
/> | ||
<Controller | ||
name="label" | ||
control={control} | ||
defaultValue={fieldMetadataItem?.label} | ||
render={({ field: { onChange, value } }) => ( | ||
<TextInput | ||
placeholder="Employees" | ||
value={value} | ||
onChange={onChange} | ||
disabled={disabled} | ||
fullWidth | ||
/> | ||
)} | ||
/> | ||
</StyledInputsContainer> | ||
<Controller | ||
name="description" | ||
control={control} | ||
defaultValue={fieldMetadataItem?.description} | ||
render={({ field: { onChange, value } }) => ( | ||
<TextArea | ||
placeholder="Write a description" | ||
minRows={4} | ||
value={value ?? undefined} | ||
onChange={onChange} | ||
disabled={disabled} | ||
/> | ||
)} | ||
/> | ||
</> | ||
); | ||
}; |
31 changes: 31 additions & 0 deletions
31
...ata-model/fields/forms/components/__stories__/SettingsDataModelFieldAboutForm.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Meta, StoryObj } from '@storybook/react'; | ||
|
||
import { mockedPersonObjectMetadataItem } from '@/object-record/record-field/__mocks__/fieldDefinitions'; | ||
import { ComponentDecorator } from '~/testing/decorators/ComponentDecorator'; | ||
|
||
import { SettingsDataModelFieldAboutForm } from '../SettingsDataModelFieldAboutForm'; | ||
|
||
const meta: Meta<typeof SettingsDataModelFieldAboutForm> = { | ||
title: 'Modules/Settings/DataModel/SettingsDataModelFieldAboutForm', | ||
component: SettingsDataModelFieldAboutForm, | ||
decorators: [ComponentDecorator], | ||
}; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof SettingsDataModelFieldAboutForm>; | ||
|
||
export const Default: Story = {}; | ||
|
||
export const WithDefaultValues: Story = { | ||
args: { | ||
fieldMetadataItem: mockedPersonObjectMetadataItem.fields.find( | ||
({ name }) => name === 'name', | ||
)!, | ||
}, | ||
}; | ||
|
||
export const Disabled: Story = { | ||
args: { | ||
disabled: true, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
...rc/modules/settings/data-model/fields/forms/validation-schemas/settingsFieldFormSchema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { settingsDataModelFieldAboutFormSchema } from '@/settings/data-model/fields/forms/components/SettingsDataModelFieldAboutForm'; | ||
|
||
export const settingsFieldFormSchema = settingsDataModelFieldAboutFormSchema; |
Oops, something went wrong.