Skip to content

Commit af010e5

Browse files
feat(FormGroup): add hasMargin, add story back in (#7936)
* feat(FormGroup): add hasMargin, add story back in * docs(FormGroup): add mdx file, update snapshots Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 3ff729e commit af010e5

File tree

5 files changed

+146
-1
lines changed

5 files changed

+146
-1
lines changed

packages/components/src/components/form/_form.scss

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
margin-bottom: $carbon--spacing-07;
2222
}
2323

24+
.#{$prefix}--fieldset--no-margin {
25+
margin-bottom: 0;
26+
}
27+
2428
.#{$prefix}--form-item {
2529
@include type-style('body-short-01');
2630

@@ -170,6 +174,12 @@
170174
.#{$prefix}--form__helper-text--disabled {
171175
color: $disabled-02;
172176
}
177+
178+
// If a FormGroup is disabled, Form labels and helper text nested inside should also be disabled
179+
fieldset[disabled] .#{$prefix}--label,
180+
fieldset[disabled] .#{$prefix}--form__helper-text {
181+
color: $disabled-02;
182+
}
173183
}
174184

175185
@include exports('form') {

packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2819,6 +2819,7 @@ Map {
28192819
},
28202820
"FormGroup" => Object {
28212821
"defaultProps": Object {
2822+
"hasMargin": true,
28222823
"invalid": false,
28232824
"message": false,
28242825
"messageText": "",
@@ -2830,6 +2831,9 @@ Map {
28302831
"className": Object {
28312832
"type": "string",
28322833
},
2834+
"hasMargin": Object {
2835+
"type": "bool",
2836+
},
28332837
"invalid": Object {
28342838
"type": "bool",
28352839
},
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* Copyright IBM Corp. 2016, 2018
3+
*
4+
* This source code is licensed under the Apache-2.0 license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import React from 'react';
9+
import { boolean, text } from '@storybook/addon-knobs';
10+
import FormGroup from './FormGroup';
11+
import TextInput from '../TextInput';
12+
import RadioButtonGroup from '../RadioButtonGroup';
13+
import RadioButton from '../RadioButton';
14+
import Button from '../Button';
15+
import mdx from './FormGroup.mdx';
16+
17+
const props = () => ({
18+
disabled: boolean('Disabled (disabled)', false),
19+
legendText: text('Legend (legendText)', 'FormGroup Legend'),
20+
hasMargin: boolean('Fieldset has bottom margin (hasMargin)', true),
21+
});
22+
23+
export default {
24+
title: 'Components/FormGroup',
25+
26+
parameters: {
27+
component: FormGroup,
28+
docs: {
29+
page: mdx,
30+
},
31+
},
32+
};
33+
34+
export const _Default = () => (
35+
<FormGroup legendText="FormGroup Legend" style={{ maxWidth: '400px' }}>
36+
<TextInput
37+
id="one"
38+
labelText="First Name"
39+
style={{ marginBottom: '1rem' }}
40+
/>
41+
<TextInput
42+
id="two"
43+
labelText="Last Name"
44+
style={{ marginBottom: '1rem' }}
45+
/>
46+
47+
<RadioButtonGroup
48+
legendText="Radio button heading"
49+
name="radio-button-group"
50+
defaultSelected="radio-1">
51+
<RadioButton labelText="Option 1" value="radio-1" id="radio-1" />
52+
<RadioButton labelText="Option 2" value="radio-2" id="radio-2" />
53+
<RadioButton labelText="Option 3" value="radio-3" id="radio-3" />
54+
</RadioButtonGroup>
55+
</FormGroup>
56+
);
57+
58+
_Default.story = {
59+
name: 'Form Group',
60+
};
61+
62+
export const Playground = () => (
63+
<>
64+
<FormGroup className="test" {...props()} style={{ maxWidth: '400px' }}>
65+
<TextInput
66+
id="one"
67+
labelText="First Name"
68+
style={{ marginBottom: '1rem' }}
69+
/>
70+
<TextInput
71+
id="two"
72+
labelText="Last Name"
73+
style={{ marginBottom: '1rem' }}
74+
/>
75+
76+
<RadioButtonGroup
77+
legendText="Radio button heading"
78+
name="radio-button-group"
79+
defaultSelected="radio-1">
80+
<RadioButton labelText="Option 1" value="radio-1" id="radio-1" />
81+
<RadioButton labelText="Option 2" value="radio-2" id="radio-2" />
82+
<RadioButton labelText="Option 3" value="radio-3" id="radio-3" />
83+
</RadioButtonGroup>
84+
</FormGroup>
85+
<Button>Submit</Button>
86+
</>
87+
);

packages/react/src/components/FormGroup/FormGroup.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ const FormGroup = ({
1919
className,
2020
message,
2121
messageText,
22+
hasMargin,
2223
...other
2324
}) => {
2425
const classNamesLegend = classnames(`${prefix}--label`, className);
25-
const classNamesFieldset = classnames(`${prefix}--fieldset`, className);
26+
const classNamesFieldset = classnames(`${prefix}--fieldset`, className, {
27+
[`${prefix}--fieldset--no-margin`]: !hasMargin,
28+
});
2629

2730
return (
2831
<fieldset
@@ -49,6 +52,11 @@ FormGroup.propTypes = {
4952
*/
5053
className: PropTypes.string,
5154

55+
/**
56+
* Specify whether or not the FormGroup should provide bottom margin
57+
*/
58+
hasMargin: PropTypes.bool,
59+
5260
/**
5361
* Specify whether the <FormGroup> is invalid
5462
*/
@@ -74,6 +82,7 @@ FormGroup.defaultProps = {
7482
invalid: false,
7583
message: false,
7684
messageText: '',
85+
hasMargin: true,
7786
};
7887

7988
export default FormGroup;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Props, Story, Preview } from '@storybook/addon-docs/blocks';
2+
3+
# FormGroup
4+
5+
[Source code](https://github.com/carbon-design-system/carbon/tree/master/packages/react/src/components/FormGroup)
6+
&nbsp;|&nbsp;
7+
[Usage guidelines](https://www.carbondesignsystem.com/components/form/usage)
8+
[Accessibility](https://www.carbondesignsystem.com/components/form/accessibility)
9+
10+
## Table of Contents
11+
12+
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
13+
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
14+
15+
- [Overview](#overview)
16+
- [Component API](#component-api)
17+
- [Feedback](#feedback)
18+
19+
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
20+
21+
## Overview
22+
23+
<Preview>
24+
<Story id="components-formgroup--default" />
25+
</Preview>
26+
27+
## Component API
28+
29+
<Props />
30+
31+
## Feedback
32+
33+
Help us improve this component by providing feedback through, asking questions
34+
on Slack, or updating this file on GitHub
35+
[GitHub](https://github.com/carbon-design-system/carbon/edit/master/packages/react/src/components/FormGroup/FormGroup.mdx).

0 commit comments

Comments
 (0)