Skip to content

Commit 6816a15

Browse files
author
Luke Pearson
committed
Added story to show disableErrorFromComponents on form
1 parent 8fee468 commit 6816a15

File tree

1 file changed

+70
-55
lines changed

1 file changed

+70
-55
lines changed

stories/Input.stories.tsx

Lines changed: 70 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,74 @@ import { Input, Form, Button } from '../src';
66

77
const stories = storiesOf('Input', module);
88

9+
const form = (disableErrorFromComponents: boolean) => (
10+
() => {
11+
const [firstName, setFirstName] = useState<string>('');
12+
const [middleName, setMiddleName] = useState<string>('');
13+
const [lastName, setLastName] = useState<string>('');
14+
const [firstNameError, setFirstNameError] = useState<string>('');
15+
const [middleNameError, setMiddleNameError] = useState<string>('');
16+
const [lastNameError, setLastNameError] = useState<string>('');
17+
18+
const onSubmit = (e: SyntheticEvent<HTMLFormElement>) => {
19+
e.preventDefault();
20+
setFirstNameError(firstName.length === 0 ? 'You must enter a first name' : null);
21+
setMiddleNameError(middleName.length === 0 ? 'You must enter a middle name' : null);
22+
setLastNameError(lastName.length === 0 ? 'You must enter a last name' : null);
23+
};
24+
25+
return (
26+
<Form onSubmit={onSubmit} disableErrorFromComponents={disableErrorFromComponents}>
27+
<Input
28+
className="nhsuk-u-margin-bottom-3"
29+
autoFocus
30+
name="first_name"
31+
type="string"
32+
aria-label="First name input"
33+
value={firstName || ''}
34+
onChange={e => setFirstName(e.currentTarget.value)}
35+
error={firstNameError}
36+
autoComplete={process.env.NODE_ENV === 'development' ? '' : undefined}
37+
width="10"
38+
>
39+
First Name
40+
</Input>
41+
42+
<Input
43+
className="nhsuk-u-margin-bottom-3"
44+
label="middle name"
45+
name="middle_name"
46+
type="string"
47+
aria-label="middle name input"
48+
value={middleName || ''}
49+
onChange={e => setMiddleName(e.currentTarget.value)}
50+
error={middleNameError}
51+
autoComplete={process.env.NODE_ENV === 'development' ? '' : undefined}
52+
width="10"
53+
>
54+
Middle name
55+
</Input>
56+
57+
<Input
58+
className="nhsuk-u-margin-bottom-3"
59+
label="Last name"
60+
name="last_name"
61+
type="string"
62+
aria-label="Last name input"
63+
value={lastName || ''}
64+
onChange={e => setLastName(e.currentTarget.value)}
65+
error={lastNameError}
66+
autoComplete={process.env.NODE_ENV === 'development' ? '' : undefined}
67+
width="10"
68+
>
69+
Last name
70+
</Input>
71+
<Button style={{ display: 'block' }} type="submit">Submit</Button>
72+
</Form>
73+
);
74+
}
75+
);
76+
977
stories
1078
.addDecorator(centered)
1179
.add('Standard', () => (
@@ -59,58 +127,5 @@ stories
59127
National Insurance Number
60128
</Input>
61129
))
62-
.add('Multiple Error States', () => {
63-
const [firstName, setFirstName] = useState<string>('');
64-
const [lastName, setLastName] = useState<string>('');
65-
const [firstNameError, setFirstNameError] = useState<string>('');
66-
const [lastNameError, setLastNameError] = useState<string>('');
67-
68-
const onSubmit = (e: SyntheticEvent<HTMLFormElement>) => {
69-
e.preventDefault();
70-
71-
if (firstNameError) {
72-
setFirstNameError('');
73-
setLastNameError('');
74-
} else {
75-
setFirstNameError('Error');
76-
setLastNameError('Error');
77-
}
78-
};
79-
80-
return (
81-
<Form onSubmit={onSubmit}>
82-
<Input
83-
id="FirstnameX"
84-
style={{ marginBottom: 10 }}
85-
autoFocus
86-
name="first_name"
87-
type="string"
88-
aria-label="First name input"
89-
value={firstName || ''}
90-
onChange={e => setFirstName(e.currentTarget.value)}
91-
error={firstNameError}
92-
autoComplete={process.env.NODE_ENV === 'development' ? '' : undefined}
93-
width="10"
94-
>
95-
First Name
96-
</Input>
97-
<Input
98-
style={{ marginBottom: 10 }}
99-
autoFocus
100-
label="Last name"
101-
name="last_name"
102-
type="string"
103-
aria-label="Last name input"
104-
value={lastName || ''}
105-
onChange={e => setLastName(e.currentTarget.value)}
106-
error={lastNameError}
107-
autoComplete={process.env.NODE_ENV === 'development' ? '' : undefined}
108-
width="10"
109-
>
110-
Last name
111-
</Input>
112-
113-
<Button type="submit">Submit</Button>
114-
</Form>
115-
);
116-
});
130+
.add('Multiple Error States', form(false))
131+
.add('Form Error State Disabled', form(true));

0 commit comments

Comments
 (0)