Skip to content

Commit

Permalink
feat(FormGroup): support id on fieldset legend (#9074)
Browse files Browse the repository at this point in the history
* feat(FormGroup): support `id` on `fieldset legend`

* docs(FormGroup): add `legendId` knob

* chore: update snapshots

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
emyarod and kodiakhq[bot] committed Jul 8, 2021
1 parent 81e9cc6 commit 4fc56c3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
3 changes: 3 additions & 0 deletions packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2907,6 +2907,9 @@ Map {
"invalid": Object {
"type": "bool",
},
"legendId": Object {
"type": "node",
},
"legendText": Object {
"isRequired": true,
"type": "node",
Expand Down
8 changes: 6 additions & 2 deletions packages/react/src/components/FormGroup/FormGroup-story.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import mdx from './FormGroup.mdx';

const props = () => ({
disabled: boolean('Disabled (disabled)', false),
legendText: text('Legend (legendText)', 'FormGroup Legend'),
legendId: text('Legend ID (legendId)', 'formgroup-legend-id'),
legendText: text('Legend text (legendText)', 'FormGroup Legend'),
hasMargin: boolean('Fieldset has bottom margin (hasMargin)', true),
});

Expand All @@ -32,7 +33,10 @@ export default {
};

export const _Default = () => (
<FormGroup legendText="FormGroup Legend" style={{ maxWidth: '400px' }}>
<FormGroup
legendId="formgroup-legend-id"
legendText="FormGroup Legend"
style={{ maxWidth: '400px' }}>
<TextInput
id="one"
labelText="First Name"
Expand Down
16 changes: 14 additions & 2 deletions packages/react/src/components/FormGroup/FormGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { settings } from 'carbon-components';
const { prefix } = settings;

const FormGroup = ({
legendId,
legendText,
invalid,
children,
Expand All @@ -31,8 +32,13 @@ const FormGroup = ({
<fieldset
{...(invalid && { 'data-invalid': '' })}
className={classNamesFieldset}
{...other}>
<legend className={classNamesLegend}>{legendText}</legend>
{...other}
aria-labelledby={other['aria-labelledby'] || legendId}>
<legend
className={classNamesLegend}
id={legendId || other['aria-labelledby']}>
{legendText}
</legend>
{children}
{message ? (
<div className={`${prefix}--form__requirements`}>{messageText}</div>
Expand Down Expand Up @@ -62,6 +68,12 @@ FormGroup.propTypes = {
*/
invalid: PropTypes.bool,

/**
* Provide id for the fieldset <legend> which corresponds to the fieldset
* `aria-labelledby`
*/
legendId: PropTypes.node,

/**
* Provide the text to be rendered inside of the fieldset <legend>
*/
Expand Down

0 comments on commit 4fc56c3

Please sign in to comment.