Skip to content

Commit 63fa2aa

Browse files
committed
feat(forms): include form help
1 parent f58b4e6 commit 63fa2aa

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed

demo/FormExamples.jsx

+21-5
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export function FormExamples() {
105105
>
106106
<div className="row">
107107
<div className="col">
108-
<FormGroupInput name="textField" label="Text field" disabled />
108+
<FormGroupInput name="textField" label="Text field" disabled help="Text field help" />
109109
</div>
110110
<div className="col">
111111
<FormGroupInput name="textField2" label="Text field 2" required placeholder="Fill some value" />
@@ -131,13 +131,14 @@ export function FormExamples() {
131131
options={['1234', '2345', '3456']}
132132
placeholder="Type some numbers"
133133
disabled
134+
help="Autocomplete help"
134135
/>
135136
</div>
136137
<div className="col">
137138
<FormGroupAutocomplete
138139
name="autocompleteField2"
139140
label="Autocomplete that opens on focus"
140-
options={(formData, searchValue) => {
141+
options={(_, searchValue) => {
141142
if (searchValue.length < 2) {
142143
return ['Abcde', 'Fghij', 'klmno'];
143144
}
@@ -171,7 +172,13 @@ export function FormExamples() {
171172

172173
<div className="row">
173174
<div className="col-4">
174-
<FormGroupSelect name="selectField" label="Select field (list)" options={['A', 'B', 'C']} disabled />
175+
<FormGroupSelect
176+
name="selectField"
177+
label="Select field (list)"
178+
options={['A', 'B', 'C']}
179+
disabled
180+
help="Select help"
181+
/>
175182
</div>
176183
<div className="col-4">
177184
<FormGroupSelect name="selectNumberField" label="Select field (number list)" options={[1, 2, 3, 4]} />
@@ -228,7 +235,14 @@ export function FormExamples() {
228235

229236
<div className="row">
230237
<div className="col">
231-
<FormGroupSwitch id="switchFieldId" name="switchField" label="Switch field" trueLabel="ON" falseLabel="OFF" />
238+
<FormGroupSwitch
239+
id="switchFieldId"
240+
name="switchField"
241+
label="Switch field"
242+
trueLabel="ON"
243+
falseLabel="OFF"
244+
help="Switch help"
245+
/>
232246
</div>
233247
<div className="col">
234248
<FormGroupSwitch id="switchFieldId2" name="switchField2" label="Switch field 2" disabled />
@@ -239,6 +253,7 @@ export function FormExamples() {
239253
name="checkboxField"
240254
label="Checkbox field"
241255
valueLabel="Checkbox description"
256+
help="Checkbox help"
242257
/>
243258
</div>
244259
<div className="col">
@@ -263,6 +278,7 @@ export function FormExamples() {
263278
label: 'C',
264279
},
265280
]}
281+
help="Radio help"
266282
/>
267283
</div>
268284
<div className="col">
@@ -285,7 +301,7 @@ export function FormExamples() {
285301
</div>
286302
</div>
287303

288-
<FormGroupTextarea name="textareaField" label="Textarea field" />
304+
<FormGroupTextarea name="textareaField" label="Textarea field" help="Textarea help" />
289305
<FormGroupTextarea name="textareaField2" label="Textarea field 2" disabled rows="1" />
290306

291307
{[0, 1].map((index) => (

src/forms/FormGroup.jsx

+11-1
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ import { FormSelect } from './FormSelect';
99
import { FormSwitch } from './FormSwitch';
1010
import { FormTextarea } from './FormTextarea';
1111
import { FormValidationFeedback } from './FormValidationFeedback';
12+
import { FormHelp } from './FormHelp';
1213

13-
export function FormGroup({ children, name, feedback, mockInvalidSibling, ...props }) {
14+
export function FormGroup({ children, name, feedback, mockInvalidSibling, help, ...props }) {
1415
return (
1516
<div className="form-group">
1617
<FormLabel {...props} />
1718
{children}
1819
{feedback && <FormValidationFeedback mockInvalidSibling={mockInvalidSibling} name={name} />}
20+
{help && <FormHelp message={help} />}
1921
</div>
2022
);
2123
}
@@ -28,6 +30,7 @@ FormGroup.defaultProps = {
2830
FormGroup.propTypes = {
2931
children: PropTypes.oneOfType([PropTypes.node, PropTypes.arrayOf(PropTypes.node)]),
3032
feedback: PropTypes.bool,
33+
help: PropTypes.node,
3134
label: PropTypes.node.isRequired,
3235
mockInvalidSibling: PropTypes.bool,
3336
name: PropTypes.string.isRequired,
@@ -44,6 +47,7 @@ export function FormGroupAutocomplete(props) {
4447
FormGroupAutocomplete.propTypes = {
4548
disabled: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
4649
filter: PropTypes.func,
50+
help: PropTypes.node,
4751
id: PropTypes.string,
4852
label: PropTypes.node.isRequired,
4953
name: PropTypes.string.isRequired,
@@ -70,6 +74,7 @@ export function FormGroupCheckbox(props) {
7074
FormGroupCheckbox.propTypes = {
7175
disabled: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
7276
falseLabel: PropTypes.node,
77+
help: PropTypes.node,
7378
id: PropTypes.string.isRequired,
7479
label: PropTypes.node.isRequired,
7580
name: PropTypes.string.isRequired,
@@ -87,6 +92,7 @@ export function FormGroupInput(props) {
8792

8893
FormGroupInput.propTypes = {
8994
disabled: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
95+
help: PropTypes.node,
9096
id: PropTypes.string,
9197
label: PropTypes.node.isRequired,
9298
max: PropTypes.string,
@@ -126,6 +132,7 @@ FormGroupRadio.defaultProps = {
126132

127133
FormGroupRadio.propTypes = {
128134
disabled: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
135+
help: PropTypes.node,
129136
id: PropTypes.string,
130137
inline: PropTypes.bool,
131138
label: PropTypes.node.isRequired,
@@ -149,6 +156,7 @@ export function FormGroupSelect(props) {
149156

150157
FormGroupSelect.propTypes = {
151158
disabled: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
159+
help: PropTypes.node,
152160
id: PropTypes.string,
153161
label: PropTypes.node.isRequired,
154162
name: PropTypes.string.isRequired,
@@ -181,6 +189,7 @@ export function FormGroupSwitch(props) {
181189
FormGroupSwitch.propTypes = {
182190
disabled: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
183191
falseLabel: PropTypes.node,
192+
help: PropTypes.node,
184193
id: PropTypes.string.isRequired,
185194
label: PropTypes.node.isRequired,
186195
name: PropTypes.string.isRequired,
@@ -198,6 +207,7 @@ export function FormGroupTextarea(props) {
198207

199208
FormGroupTextarea.propTypes = {
200209
disabled: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
210+
help: PropTypes.node,
201211
id: PropTypes.string,
202212
label: PropTypes.node.isRequired,
203213
name: PropTypes.string.isRequired,

src/forms/FormHelp.jsx

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
import { formatClasses } from '../utils/attributes';
4+
5+
export function FormHelp({ id, inline, message }) {
6+
return (
7+
<small id={`${id}-help`} className={formatClasses(['text-muted', inline ? 'ml-2' : 'form-text'])}>
8+
{message}
9+
</small>
10+
);
11+
}
12+
13+
FormHelp.defaultProps = {
14+
inline: false,
15+
};
16+
17+
FormHelp.propTypes = {
18+
id: PropTypes.string,
19+
message: PropTypes.node.isRequired,
20+
inline: PropTypes.bool,
21+
};

0 commit comments

Comments
 (0)