Skip to content

Commit e47445d

Browse files
TylerJDevdependabot[bot]lukasoppermannsiddharthkpjoshblack
authored
Allows asterisk in FormControl.Label to be adjustable (#4543)
* Add new props to `FormControl.Label` * Add conditional * Add changeset * Update docs json * Add more examples * chore(deps-dev): bump ejs from 3.1.9 to 3.1.10 (#4549) Bumps [ejs](https://github.com/mde/ejs) from 3.1.9 to 3.1.10. - [Release notes](https://github.com/mde/ejs/releases) - [Commits](mde/ejs@v3.1.9...v3.1.10) --- updated-dependencies: - dependency-name: ejs dependency-type: indirect ... Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * BranchName: Add style for span and add v8 tokens (#4556) * add style for branchName as span adn add v8 tokens * added changeset * Update thin-ligers-turn.md * test(vrt): update snapshots * use not a instead of matching for span --------- Co-authored-by: Siddharth Kshetrapal <[email protected]> Co-authored-by: lukasoppermann <[email protected]> * refactor(Banner): update region to use a dedicated aria-label (#4539) * refactor(Banner): update region to use a dedicated aria-label * chore: add changeset, update config to exclude codesandbox * feat: add aria-label to Banner * Update packages/react/src/Banner/Banner.test.tsx Co-authored-by: Kate Higa <[email protected]> * Update packages/react/src/Banner/Banner.test.tsx Co-authored-by: Kate Higa <[email protected]> * test: update test label with aria-label --------- Co-authored-by: Josh Black <[email protected]> Co-authored-by: Kate Higa <[email protected]> * chore(deps-dev): bump cross-env from 7.0.2 to 7.0.3 (#4561) Bumps [cross-env](https://github.com/kentcdodds/cross-env) from 7.0.2 to 7.0.3. - [Release notes](https://github.com/kentcdodds/cross-env/releases) - [Changelog](https://github.com/kentcdodds/cross-env/blob/master/CHANGELOG.md) - [Commits](kentcdodds/cross-env@v7.0.2...v7.0.3) --- updated-dependencies: - dependency-name: cross-env dependency-type: direct:development update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * chore(deps-dev): bump @babel/plugin-transform-modules-commonjs (#4562) Bumps [@babel/plugin-transform-modules-commonjs](https://github.com/babel/babel/tree/HEAD/packages/babel-plugin-transform-modules-commonjs) from 7.23.3 to 7.24.1. - [Release notes](https://github.com/babel/babel/releases) - [Changelog](https://github.com/babel/babel/blob/main/CHANGELOG.md) - [Commits](https://github.com/babel/babel/commits/v7.24.1/packages/babel-plugin-transform-modules-commonjs) --- updated-dependencies: - dependency-name: "@babel/plugin-transform-modules-commonjs" dependency-type: direct:development update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * chore(deps-dev): bump jest-fail-on-console from 3.1.1 to 3.2.0 (#4563) Bumps [jest-fail-on-console](https://github.com/ValentinH/jest-fail-on-console) from 3.1.1 to 3.2.0. - [Release notes](https://github.com/ValentinH/jest-fail-on-console/releases) - [Commits](ValentinH/jest-fail-on-console@v3.1.1...v3.2.0) --- updated-dependencies: - dependency-name: jest-fail-on-console dependency-type: direct:development update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * feat(FeatureFlags): broaden feature flag type to accept undefined (#4554) * feat(FeatureFlags): loosen feature flag type to accept undefined * chore: add changeset * Update .changeset/grumpy-coats-worry.md --------- Co-authored-by: Josh Black <[email protected]> * prevent form submit (#4551) * deprecate title prop on ActionList.Group component on docs (#4544) * chore: add hydro analytics to storybook (#4558) * chore: add hydro analytics to storybook * chore: use previewHead over managerHead * Update build-docs --------- Co-authored-by: Josh Black <[email protected]> * Revert "Revert "Add support for nested submenus to `ActionMenu`"" (#4486) * Revert "Revert "Add support for nested submenus to `ActionMenu` (#4386)" (#4472)" This reverts commit 82072eb. * just want a change to trigger rebuild --------- Co-authored-by: Siddharth Kshetrapal <[email protected]> Co-authored-by: Pavithra Kodmad <[email protected]> * chore(deps): update typescript to 5.4.5 (#4568) Co-authored-by: Josh Black <[email protected]> * Use dynamic height and width for dialogs (#4567) * Use dynamic height and width for dialogs * Update tall-forks-bathe.md --------- Co-authored-by: Siddharth Kshetrapal <[email protected]> * Make asterisk default, update story scenarios * Update packages/react/src/FormControl/FormControl.docs.json Co-authored-by: Owen Niblock <[email protected]> * Update packages/react/src/internal/components/InputLabel.tsx Co-authored-by: Owen Niblock <[email protected]> --------- Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Lukas Oppermann <[email protected]> Co-authored-by: Siddharth Kshetrapal <[email protected]> Co-authored-by: lukasoppermann <[email protected]> Co-authored-by: Josh Black <[email protected]> Co-authored-by: Josh Black <[email protected]> Co-authored-by: Kate Higa <[email protected]> Co-authored-by: Armağan <[email protected]> Co-authored-by: Ian Sanders <[email protected]> Co-authored-by: Pavithra Kodmad <[email protected]> Co-authored-by: Dusty Greif <[email protected]> Co-authored-by: Owen Niblock <[email protected]>
1 parent 32c6870 commit e47445d

File tree

5 files changed

+58
-3
lines changed

5 files changed

+58
-3
lines changed

.changeset/stupid-suns-cheat.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@primer/react': patch
3+
---
4+
5+
FormControl: Adds new props to `FormControl.Label`, `requiredText` and `requiredIndicator`

packages/react/src/FormControl/FormControl.docs.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@
5656
"defaultValue": "'label'",
5757
"description": "The label element can be changed to a 'legend' when it's being used to label a fieldset, or a 'span' when it's being used to label an element that is not a form input. For example: when using a FormControl to render a labeled SegementedControl, the label should be a 'span'"
5858
},
59+
{
60+
"name": "requiredText",
61+
"type": "string",
62+
"defaultValue": "'*'",
63+
"description": "The text to display when the field is required"
64+
},
65+
{
66+
"name": "requiredIndicator",
67+
"type": "boolean",
68+
"defaultValue": "true",
69+
"description": "Whether to show or hide the required text in the accessibility tree, the required text is still shown visually."
70+
},
5971
{
6072
"name": "sx",
6173
"type": "SystemStyleObject"

packages/react/src/FormControl/FormControl.features.stories.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
Radio,
1111
RadioGroup,
1212
Select,
13+
Text,
1314
TextInput,
1415
TextInputWithTokens,
1516
Textarea,
@@ -306,3 +307,30 @@ export const DisabledInputs = () => (
306307
</FormControl>
307308
</Box>
308309
)
310+
311+
export const CustomRequired = () => (
312+
<Box sx={{display: 'flex', flexDirection: 'column', gap: '1rem'}}>
313+
<FormControl required={true}>
314+
<FormControl.Label requiredText="(required)">Form Input Label</FormControl.Label>
315+
<FormControl.Caption>This is a form field with a custom required indicator</FormControl.Caption>
316+
<TextInput />
317+
</FormControl>
318+
319+
<Text sx={{fontSize: 1}}>Required fields are marked with an asterisk (*)</Text>
320+
<FormControl required={true}>
321+
<FormControl.Label requiredIndicator={false}>Form Input Label</FormControl.Label>
322+
<FormControl.Caption>
323+
This is a form field with a required indicator that is hidden in the accessibility tree
324+
</FormControl.Caption>
325+
<TextInput />
326+
</FormControl>
327+
328+
<FormControl required={false}>
329+
<FormControl.Label requiredText="(optional)" requiredIndicator={false}>
330+
Form Input Label
331+
</FormControl.Label>
332+
<FormControl.Caption>This is a form field that is marked as optional, it is not required</FormControl.Caption>
333+
<TextInput />
334+
</FormControl>
335+
</Box>
336+
)

packages/react/src/FormControl/_FormControlLabel.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ export type Props = {
88
* Whether the label should be visually hidden
99
*/
1010
visuallyHidden?: boolean
11+
requiredText?: string
12+
requiredIndicator?: boolean
1113
id?: string
1214
} & SxProp
1315

1416
const FormControlLabel: React.FC<
1517
React.PropsWithChildren<{htmlFor?: string} & React.ComponentProps<typeof InputLabel> & Props>
16-
> = ({as, children, htmlFor, id, visuallyHidden, sx, ...props}) => {
18+
> = ({as, children, htmlFor, id, visuallyHidden, requiredIndicator = true, requiredText, sx, ...props}) => {
1719
const {disabled, id: formControlId, required} = useFormControlContext()
1820

1921
/**
@@ -26,6 +28,8 @@ const FormControlLabel: React.FC<
2628
id,
2729
visuallyHidden,
2830
required,
31+
requiredText,
32+
requiredIndicator,
2933
disabled,
3034
sx,
3135
...props,
@@ -36,6 +40,8 @@ const FormControlLabel: React.FC<
3640
visuallyHidden,
3741
htmlFor: htmlFor || formControlId,
3842
required,
43+
requiredText,
44+
requiredIndicator,
3945
disabled,
4046
sx,
4147
...props,

packages/react/src/internal/components/InputLabel.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import VisuallyHidden from '../../_VisuallyHidden'
66
type BaseProps = SxProp & {
77
disabled?: boolean
88
required?: boolean
9+
requiredText?: string
10+
requiredIndicator?: boolean
911
visuallyHidden?: boolean
1012
id?: string
1113
}
@@ -28,6 +30,8 @@ const InputLabel: React.FC<React.PropsWithChildren<Props>> = ({
2830
htmlFor,
2931
id,
3032
required,
33+
requiredText,
34+
requiredIndicator,
3135
visuallyHidden,
3236
sx,
3337
as = 'label',
@@ -52,10 +56,10 @@ const InputLabel: React.FC<React.PropsWithChildren<Props>> = ({
5256
}}
5357
{...props}
5458
>
55-
{required ? (
59+
{required || requiredText ? (
5660
<Box display="flex" as="span">
5761
<Box mr={1}>{children}</Box>
58-
<span>*</span>
62+
<span aria-hidden={requiredIndicator ? undefined : true}>{requiredText ?? '*'}</span>
5963
</Box>
6064
) : (
6165
children

0 commit comments

Comments
 (0)