Skip to content
This repository was archived by the owner on Dec 27, 2022. It is now read-only.

fix(tags): initialvalues and playground documentation form #596

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions src/components/Form/__stories__/Playground.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import type { ChangeEvent } from 'react'
import { useState } from 'react'
import {
CheckboxField,
DateField,
Form,
RadioField,
RichSelectField,
Submit,
SubmitErrorAlert,
TagsField,
TextBoxField,
} from '../..'
import { emailRegex, mockErrors } from '../../../mocks/mockErrors'
Expand All @@ -17,7 +20,7 @@ export const Playground: ComponentStory<typeof Form> = args => {

return (
<Form {...args}>
<Stack gap={2}>
<Stack gap={3}>
<Checkbox
checked={state}
onChange={(event: ChangeEvent<HTMLInputElement>) =>
Expand All @@ -42,6 +45,20 @@ export const Playground: ComponentStory<typeof Form> = args => {
required
regex={[emailRegex]}
/>
<Stack gap={2} direction="row">
<RadioField name="choice" value="1" required>
1
</RadioField>
<RadioField name="choice" value="2">
2
</RadioField>
<RadioField name="choice" value="3">
3
</RadioField>
</Stack>

<DateField name="date" label="birthday" />

<RichSelectField
multiple
noTopLabel
Expand All @@ -55,6 +72,9 @@ export const Playground: ComponentStory<typeof Form> = args => {
{ label: 'Devops', value: 'devops' },
]}
/>

<TagsField name="tags" placeholder="Tags..." tags={[]} />

<CheckboxField name="receiveEmailUpdates">
I&apos;d like to receive news updates
</CheckboxField>
Expand All @@ -67,5 +87,12 @@ export const Playground: ComponentStory<typeof Form> = args => {

Playground.args = {
errors: mockErrors,
initialValues: { receiveEmailUpdates: true },
initialValues: {
receiveEmailUpdates: true,
choice: '2',
tags: ['cloud', 'of', 'choice'],
},
onRawSubmit: values => {
console.log('Submit', values)
},
}
24 changes: 16 additions & 8 deletions src/components/TagsField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@ import type { ComponentProps } from 'react'
import { useFormField } from '../../hooks'
import type { BaseFieldProps } from '../../types'

export type TagsFieldProps<T = unknown, K = string> = BaseFieldProps<T, K> &
type TagsProp = ComponentProps<typeof Tags>['tags']

export type TagsFieldProps<T = TagsProp, K = string> = BaseFieldProps<T, K> &
Partial<
Pick<ComponentProps<typeof Tags>, 'tags' | 'variant' | 'onChange'>
Pick<
ComponentProps<typeof Tags>,
| 'tags'
| 'variant'
| 'onChange'
| 'placeholder'
| 'disabled'
| 'className'
| 'id'
>
> & {
className?: string
disabled?: boolean
id?: string
name: string
placeholder: string
required?: boolean
}

Expand All @@ -27,11 +34,12 @@ export const TagsField = ({
validate,
variant,
}: TagsFieldProps): JSX.Element => {
const { input } = useFormField(name, {
const { input } = useFormField<TagsProp>(name, {
disabled,
required,
type: 'text',
validate,
value: tags,
})

return (
Expand All @@ -46,7 +54,7 @@ export const TagsField = ({
}}
placeholder={placeholder}
variant={variant}
tags={tags}
tags={input.value}
/>
)
}