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

Commit b1b8ed1

Browse files
feat: disable field validation when the field is disabled (#564)
1 parent 7898c99 commit b1b8ed1

File tree

13 files changed

+73
-47
lines changed

13 files changed

+73
-47
lines changed

.storybook/preview.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SCWUITheme, normalize, lightTheme } from '@scaleway/ui'
1+
import { SCWUITheme, normalize, theme } from '@scaleway/ui'
22

33
import { css, ThemeProvider, Global, Theme } from '@emotion/react'
44

@@ -51,19 +51,19 @@ export const parameters =
5151
const adjustedTheme = ancestorTheme =>
5252
({
5353
...ancestorTheme,
54-
...Object.keys(lightTheme).reduce(
54+
...Object.keys(theme).reduce(
5555
(acc, themeItem) => ({
5656
...acc,
5757
[themeItem]: {
5858
...(acc[themeItem] ?? {}),
59-
...lightTheme[themeItem],
59+
...theme[themeItem],
6060
},
6161
}),
6262
ancestorTheme,
6363
),
6464
} as SCWUITheme)
6565

66-
export const globalStyles = (theme: Theme) => css`
66+
export const globalStyles = (_: Theme) => css`
6767
${normalize()}
6868
`
6969

src/components/CheckboxField/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export const CheckboxField = forwardRef(
4949
const { getError } = useErrors()
5050

5151
const { input, meta } = useFormField(name, {
52+
disabled,
5253
required,
5354
type: 'checkbox',
5455
validate,

src/components/DateField/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export const DateField = ({
5858
const { getError } = useErrors()
5959

6060
const { input, meta } = useFormField<Date>(name, {
61+
disabled,
6162
formatOnBlur,
6263
initialValue,
6364
maxDate,

src/components/Form/__stories__/Playground.stories.tsx

Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { Stack } from '@scaleway/ui'
1+
import { Checkbox, Stack } from '@scaleway/ui'
22
import { ComponentStory } from '@storybook/react'
3+
import { ChangeEvent, useState } from 'react'
34
import {
45
CheckboxField,
56
Form,
@@ -10,45 +11,58 @@ import {
1011
} from '../..'
1112
import { emailRegex, mockErrors } from '../../../mocks/mockErrors'
1213

13-
export const Playground: ComponentStory<typeof Form> = args => (
14-
<Form {...args}>
15-
<Stack gap={2}>
16-
<TextBoxField
17-
name="name"
18-
label="Name"
19-
placeholder="John"
20-
required
21-
autoComplete="given-name"
22-
/>
23-
<TextBoxField
24-
name="email"
25-
label="Email"
26-
type="email"
27-
placeholder="john.smith@email.com"
28-
required
29-
regex={[emailRegex]}
30-
/>
31-
<RichSelectField
32-
multiple
33-
noTopLabel
34-
name="topics"
35-
label="Topics "
36-
placeholder="Select topics you're interested in"
37-
options={[
38-
{ label: 'React', value: 'react' },
39-
{ label: 'Webdev', value: 'web' },
40-
{ label: 'Cloud', value: 'cloud' },
41-
{ label: 'Devops', value: 'devops' },
42-
]}
43-
/>
44-
<CheckboxField name="receiveEmailUpdates">
45-
I&apos;d like to receive news updates
46-
</CheckboxField>
47-
<SubmitErrorAlert />
48-
<Submit>Submit</Submit>
49-
</Stack>
50-
</Form>
51-
)
14+
export const Playground: ComponentStory<typeof Form> = args => {
15+
const [state, setState] = useState(false)
16+
17+
return (
18+
<Form {...args}>
19+
<Stack gap={2}>
20+
<Checkbox
21+
checked={state}
22+
onChange={(event: ChangeEvent<HTMLInputElement>) =>
23+
setState(event.target.checked)
24+
}
25+
>
26+
I&apos;m disabling the field name to remove validation
27+
</Checkbox>
28+
<TextBoxField
29+
name="name"
30+
label="Name"
31+
placeholder="John"
32+
required
33+
autoComplete="given-name"
34+
disabled={state}
35+
/>
36+
<TextBoxField
37+
name="email"
38+
label="Email"
39+
type="email"
40+
placeholder="john.smith@email.com"
41+
required
42+
regex={[emailRegex]}
43+
/>
44+
<RichSelectField
45+
multiple
46+
noTopLabel
47+
name="topics"
48+
label="Topics "
49+
placeholder="Select topics you're interested in"
50+
options={[
51+
{ label: 'React', value: 'react' },
52+
{ label: 'Webdev', value: 'web' },
53+
{ label: 'Cloud', value: 'cloud' },
54+
{ label: 'Devops', value: 'devops' },
55+
]}
56+
/>
57+
<CheckboxField name="receiveEmailUpdates">
58+
I&apos;d like to receive news updates
59+
</CheckboxField>
60+
<SubmitErrorAlert />
61+
<Submit>Submit</Submit>
62+
</Stack>
63+
</Form>
64+
)
65+
}
5266

5367
Playground.args = {
5468
errors: mockErrors,

src/components/RadioField/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export const RadioField = ({
4646
})
4747

4848
const error = getError({
49+
disabled,
4950
label,
5051
meta: meta as FieldState<unknown>,
5152
name,

src/components/RichSelectField/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ export const RichSelectField = <
158158
)
159159

160160
const { input, meta } = useFormField<T, HTMLElement, RichSelectOption>(name, {
161+
disabled,
161162
format,
162163
formatOnBlur,
163164
maxLength,

src/components/SelectNumberField/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export const SelectNumberField = ({
5252
className,
5353
}: SelectNumberValueFieldProps) => {
5454
const { input } = useFormField(name, {
55+
disabled,
5556
required,
5657
type: 'number',
5758
validate,

src/components/SelectableCardField/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export const SelectableCardField = ({
5555
const { getError } = useErrors()
5656

5757
const { input, meta } = useFormField(name, {
58+
disabled,
5859
required,
5960
type: type ?? 'radio',
6061
validate,

src/components/TagsField/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export const TagsField = ({
2828
variant,
2929
}: TagsFieldProps): JSX.Element => {
3030
const { input } = useFormField(name, {
31+
disabled,
3132
required,
3233
type: 'text',
3334
validate,

src/components/TextBoxField/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ export const TextBoxField = forwardRef(
113113
allowNull,
114114
beforeSubmit,
115115
defaultValue,
116+
disabled,
116117
format,
117118
formatOnBlur,
118119
initialValue,

0 commit comments

Comments
 (0)