-
Notifications
You must be signed in to change notification settings - Fork 536
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
Make FormControl
externally extensible (via hook)
#3632
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
2d9c093
Make `FormControl` extensible via context + hook
iansan5653 380c757
Create sweet-turtles-applaud.md
iansan5653 41d53c0
Dont call hook in `InlineAutocomplete` since it gets called farther d…
iansan5653 86daf8b
Don't call hook in `Autocomplete`
iansan5653 6b34654
Add comments on imports
iansan5653 e88f28d
Remove old comment
iansan5653 ebb0ba5
Remove `id` override from `AutocompleteInput`
iansan5653 2a73a98
Merge branch 'main' of https://github.com/primer/react into formcontr…
iansan5653 bac5870
Change version bump to `minor`
iansan5653 b32e2dc
Revert "Remove `id` override from `AutocompleteInput`"
iansan5653 eb900da
Suppress warning when passed ID == context ID
iansan5653 11120db
Don't export `FormControlContext` directly
iansan5653 8e470af
Don't export `FormControlContext` type
iansan5653 edf8fe4
Revert component changes
iansan5653 5ff0518
Move warning & cloning logic back into `FormControl` :(
iansan5653 cda6870
Allow calling `useForwardedProps` without passing a props object
iansan5653 cbcb273
Expose `useFormControlForwardedProps` directly
iansan5653 ffedcc1
Add tests for `useFormControlForwardedProps`
iansan5653 38bcc16
Add story for `useFormControlForwardedProps`
iansan5653 b21bb48
Fix import
iansan5653 b5677e5
Update snapshot
iansan5653 51b5e83
Fix `InlineAutocomplete`
iansan5653 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 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,7 @@ | ||
--- | ||
"@primer/react": minor | ||
--- | ||
|
||
Allow consumers to make components that are compatible with `FormControl` by reading forwarded props in from the `useFormControlForwardedProps` hook | ||
|
||
<!-- Changed components: FormControl --> |
This file contains 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 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 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,51 @@ | ||
import {createContext, useContext} from 'react' | ||
import {FormValidationStatus} from '../utils/types/FormValidationStatus' | ||
import {FormControlProps} from './FormControl' | ||
|
||
interface FormControlContext extends Pick<FormControlProps, 'disabled' | 'id' | 'required'> { | ||
captionId?: string | ||
validationMessageId?: string | ||
validationStatus?: FormValidationStatus | ||
} | ||
|
||
const FormControlContext = createContext<FormControlContext | null>(null) | ||
|
||
export const FormControlContextProvider = FormControlContext.Provider | ||
|
||
/** This is the private/internal interface for subcomponents of `FormControl`. */ | ||
export function useFormControlContext(): FormControlContext { | ||
return useContext(FormControlContext) ?? {} | ||
} | ||
|
||
interface FormControlForwardedProps extends Omit<FormControlContext, 'captionId' | 'validationMessageId'> { | ||
['aria-describedby']?: string | ||
} | ||
|
||
/** | ||
* Make any component compatible with `FormControl`'s automatic wiring up of accessibility attributes & validation by | ||
* reading the props from this hook and merging them with the passed-in props. If used outside of `FormControl`, this | ||
* hook has no effect. | ||
* | ||
* @param externalProps The external props passed to this component. If provided, these props will be merged with the | ||
* `FormControl` props, with external props taking priority. | ||
*/ | ||
export function useFormControlForwardedProps<P>(externalProps: P): P & FormControlForwardedProps | ||
/** | ||
* Make any component compatible with `FormControl`'s automatic wiring up of accessibility attributes & validation by | ||
* reading the props from this hook and handling them / assigning them to the underlying form control. If used outside | ||
* of `FormControl`, this hook has no effect. | ||
*/ | ||
export function useFormControlForwardedProps(): FormControlForwardedProps | ||
export function useFormControlForwardedProps(externalProps: FormControlForwardedProps = {}) { | ||
const context = useContext(FormControlContext) | ||
if (!context) return externalProps | ||
|
||
return { | ||
disabled: context.disabled, | ||
id: context.id, | ||
required: context.required, | ||
validationStatus: context.validationStatus, | ||
['aria-describedby']: [context.validationMessageId, context.captionId].filter(Boolean).join(' ') || undefined, | ||
...externalProps, | ||
} | ||
} |
This file contains 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 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 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 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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export {useFormControlForwardedProps} from './_FormControlContext' | ||
export {default} from './FormControl' |
This file contains 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,67 @@ | ||
import React from 'react' | ||
import {Meta} from '@storybook/react' | ||
import {BaseStyles, FormControl, ThemeProvider, theme, useFormControlForwardedProps} from '..' | ||
|
||
export default { | ||
title: 'Hooks/useFormControlForwardedProps', | ||
decorators: [ | ||
Story => { | ||
return ( | ||
<ThemeProvider theme={theme}> | ||
<BaseStyles> | ||
<Story /> | ||
</BaseStyles> | ||
</ThemeProvider> | ||
) | ||
}, | ||
], | ||
argTypes: { | ||
disabled: { | ||
type: 'boolean', | ||
}, | ||
required: { | ||
type: 'boolean', | ||
}, | ||
label: { | ||
type: 'string', | ||
}, | ||
caption: { | ||
type: 'string', | ||
}, | ||
type: { | ||
control: { | ||
type: 'select', | ||
description: "Type of the input, showing how the `type` prop can be forwarded to the input's props", | ||
}, | ||
options: ['text', 'number', 'password', 'email', 'search', 'tel', 'url'], | ||
}, | ||
}, | ||
} as Meta | ||
|
||
interface ArgTypes { | ||
disabled: boolean | ||
required: boolean | ||
label: string | ||
caption: string | ||
type: string | ||
} | ||
|
||
/** A custom input that is not a Primer `TextInput` but still supports autowiring with `FormControl`. */ | ||
const CustomInput = (externalProps: {type: string}) => { | ||
const props = useFormControlForwardedProps(externalProps) | ||
return <input {...props} /> | ||
} | ||
|
||
export const AutowiredCustomInput = ({ | ||
label = 'Custom input', | ||
caption = 'This is not a Primer input, but it still has `aria-describedby` and similar attributes applied automatically', | ||
required = false, | ||
disabled = false, | ||
type = 'text', | ||
}: ArgTypes) => ( | ||
<FormControl disabled={disabled} required={required}> | ||
<FormControl.Label>{label}</FormControl.Label> | ||
<CustomInput type={type} /> | ||
{caption && <FormControl.Caption>{caption}</FormControl.Caption>} | ||
</FormControl> | ||
) |
This file contains 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 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 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 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One downside of this is that if two components inside the same
FormControl
call this hook, they could have the same ID. But that's an invalid structure anyway -FormControl
is singular so this shouldn't be a problem.