Skip to content

Commit 3a1ce72

Browse files
authored
Merge pull request #15 from NHSDigital/form-disable-error-state
Added story to show disableErrorFromComponents on form
2 parents 209bf26 + cf2ad0e commit 3a1ce72

File tree

1 file changed

+71
-56
lines changed

1 file changed

+71
-56
lines changed

stories/Input.stories.tsx

Lines changed: 71 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,75 @@
11
/* eslint-disable import/no-extraneous-dependencies */
2-
import React, { useState, SyntheticEvent } from 'react';
2+
import React, { useState, SyntheticEvent, FunctionComponent } from 'react';
33
import { storiesOf } from '@storybook/react';
44
import centered from '@storybook/addon-centered';
55
import { Input, Form, Button } from '../src';
66

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

9+
interface Props {
10+
disableErrorFromComponents?: boolean;
11+
}
12+
13+
const ExampleForm: FunctionComponent<Props> = (props) => {
14+
const { disableErrorFromComponents } = props;
15+
const [firstName, setFirstName] = useState<string>('');
16+
const [middleName, setMiddleName] = useState<string>('');
17+
const [lastName, setLastName] = useState<string>('');
18+
const [firstNameError, setFirstNameError] = useState<string>('');
19+
const [middleNameError, setMiddleNameError] = useState<string>('');
20+
const [lastNameError, setLastNameError] = useState<string>('');
21+
22+
const onSubmit = (e: SyntheticEvent<HTMLFormElement>) => {
23+
e.preventDefault();
24+
setFirstNameError(firstName.length === 0 ? 'You must enter a first name' : null);
25+
setMiddleNameError(middleName.length === 0 ? 'You must enter a middle name' : null);
26+
setLastNameError(lastName.length === 0 ? 'You must enter a last name' : null);
27+
};
28+
29+
return (
30+
<Form onSubmit={onSubmit} disableErrorFromComponents={disableErrorFromComponents}>
31+
<Input
32+
className="nhsuk-u-margin-bottom-3"
33+
type="text"
34+
aria-label="First name input"
35+
value={firstName}
36+
onChange={e => setFirstName(e.currentTarget.value)}
37+
error={firstNameError}
38+
width="10"
39+
>
40+
First Name
41+
</Input>
42+
43+
<Input
44+
className="nhsuk-u-margin-bottom-3"
45+
label="middle name"
46+
type="text"
47+
aria-label="middle name input"
48+
value={middleName}
49+
onChange={e => setMiddleName(e.currentTarget.value)}
50+
error={middleNameError}
51+
width="10"
52+
>
53+
Middle name
54+
</Input>
55+
56+
<Input
57+
className="nhsuk-u-margin-bottom-3"
58+
label="Last name"
59+
type="text"
60+
aria-label="Last name input"
61+
value={lastName}
62+
onChange={e => setLastName(e.currentTarget.value)}
63+
error={lastNameError}
64+
width="10"
65+
>
66+
Last name
67+
</Input>
68+
<Button style={{ display: 'block' }} type="submit">Submit</Button>
69+
</Form>
70+
);
71+
};
72+
973
stories
1074
.addDecorator(centered)
1175
.add('Standard', () => (
@@ -59,58 +123,9 @@ stories
59123
National Insurance Number
60124
</Input>
61125
))
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-
});
126+
.add('Multiple Error States', () => (
127+
<ExampleForm />
128+
))
129+
.add('Form Error State Disabled', () => (
130+
<ExampleForm disableErrorFromComponents />
131+
));

0 commit comments

Comments
 (0)