-
Notifications
You must be signed in to change notification settings - Fork 646
Let FormControl accept any input #1968
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d2bf844
renders any non-FormControl components passed into FormControl in the…
mperrotti fa6883f
adds changeset
mperrotti 58ff21a
Merge branch 'main' into mp/form-control-any-input
mperrotti e11010f
appease the linter
mperrotti 31096d7
Merge branch 'main' of github.com:primer/react into mp/form-control-a…
mperrotti 8d311d1
Merge branch 'mp/form-control-any-input' of github.com:primer/react i…
mperrotti 31d09d6
Update docs/content/FormControl.mdx
mperrotti ad742b0
Merge branch 'main' into mp/form-control-any-input
mperrotti 5312212
addresses PR feedback
mperrotti aa500e9
Merge branch 'main' into mp/form-control-any-input
mperrotti File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@primer/react': patch | ||
| --- | ||
|
|
||
| Instead of rendering unexpected FormControl children before the rest of the content, we render them in the same spot we'd normally render a Primer input component |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,11 @@ export type FormControlProps = { | |
| * If true, the user must specify a value for the input before the owning form can be submitted | ||
| */ | ||
| required?: boolean | ||
| /** | ||
| * The direction the content flows. | ||
| * Vertical layout is used by default, and horizontal layout is used for checkbox and radio inputs. | ||
| */ | ||
| layout?: 'horizontal' | 'vertical' | ||
| } & SxProp | ||
|
|
||
| export interface FormControlContext extends Pick<FormControlProps, 'disabled' | 'id' | 'required'> { | ||
|
|
@@ -32,7 +37,7 @@ export interface FormControlContext extends Pick<FormControlProps, 'disabled' | | |
| } | ||
|
|
||
| const FormControl = React.forwardRef<HTMLDivElement, FormControlProps>( | ||
| ({children, disabled: disabledProp, id: idProp, required, sx}, ref) => { | ||
| ({children, disabled: disabledProp, layout, id: idProp, required, sx}, ref) => { | ||
| const expectedInputComponents = [Autocomplete, Checkbox, Radio, Select, TextInput, TextInputWithTokens, Textarea] | ||
| const choiceGroupContext = useContext(CheckboxOrRadioGroupContext) | ||
| const disabled = choiceGroupContext?.disabled || disabledProp | ||
|
|
@@ -56,20 +61,7 @@ const FormControl = React.forwardRef<HTMLDivElement, FormControlProps>( | |
| const isChoiceInput = | ||
| React.isValidElement(InputComponent) && (InputComponent.type === Checkbox || InputComponent.type === Radio) | ||
|
|
||
| if (!InputComponent) { | ||
| // eslint-disable-next-line no-console | ||
| console.warn( | ||
| `To correctly render this field with the correct ARIA attributes passed to the input, please pass one of the component from @primer/react as a direct child of the FormControl component: ${expectedInputComponents.reduce( | ||
| (acc, componentName) => { | ||
| acc += `\n- ${componentName.displayName}` | ||
|
|
||
| return acc | ||
| }, | ||
| '' | ||
| )}`, | ||
| 'If you are using a custom input component, please be sure to follow WCAG guidelines to make your form control accessible.' | ||
| ) | ||
| } else { | ||
| if (InputComponent) { | ||
| if (inputProps?.id) { | ||
| // eslint-disable-next-line no-console | ||
| console.warn( | ||
|
|
@@ -135,7 +127,7 @@ const FormControl = React.forwardRef<HTMLDivElement, FormControlProps>( | |
| {slots => { | ||
| const isLabelHidden = React.isValidElement(slots.Label) && slots.Label.props.visuallyHidden | ||
|
|
||
| return isChoiceInput ? ( | ||
| return isChoiceInput || layout === 'horizontal' ? ( | ||
| <Box ref={ref} display="flex" alignItems={slots.LeadingVisual ? 'center' : undefined} sx={sx}> | ||
| <Box sx={{'> input': {marginLeft: 0, marginRight: 0}}}> | ||
| {React.isValidElement(InputComponent) && | ||
|
|
@@ -183,13 +175,8 @@ const FormControl = React.forwardRef<HTMLDivElement, FormControlProps>( | |
| display="flex" | ||
| flexDirection="column" | ||
| width="100%" | ||
| sx={{...(isLabelHidden ? {'> *:not(label) + *': {marginTop: 2}} : {'> * + *': {marginTop: 2}}), ...sx}} | ||
| sx={{...(isLabelHidden ? {'> *:not(label) + *': {marginTop: 1}} : {'> * + *': {marginTop: 1}}), ...sx}} | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This has nothing to do with accepting different input components. |
||
| > | ||
| {React.Children.toArray(children).filter( | ||
| child => | ||
| React.isValidElement(child) && | ||
| !expectedInputComponents.some(inputComponent => child.type === inputComponent) | ||
| )} | ||
| {slots.Label} | ||
| {React.isValidElement(InputComponent) && | ||
| React.cloneElement(InputComponent, { | ||
|
|
@@ -199,6 +186,11 @@ const FormControl = React.forwardRef<HTMLDivElement, FormControlProps>( | |
| validationStatus, | ||
| ['aria-describedby']: [validationMessageId, captionId].filter(Boolean).join(' ') | ||
| })} | ||
| {React.Children.toArray(children).filter( | ||
| child => | ||
| React.isValidElement(child) && | ||
| !expectedInputComponents.some(inputComponent => child.type === inputComponent) | ||
| )} | ||
| {validationChild && <ValidationAnimationContainer show>{slots.Validation}</ValidationAnimationContainer>} | ||
| {slots.Caption} | ||
| </Box> | ||
|
|
@@ -209,6 +201,10 @@ const FormControl = React.forwardRef<HTMLDivElement, FormControlProps>( | |
| } | ||
| ) | ||
|
|
||
| FormControl.defaultProps = { | ||
| layout: 'vertical' | ||
| } | ||
|
|
||
| export default Object.assign(FormControl, { | ||
| Caption: FormControlCaption, | ||
| Label: FormControlLabel, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.