Skip to content

Commit 167c8d4

Browse files
hussam-i-amjonrohan
andauthoredDec 6, 2024··
fix(useFormControlForwardedProps): do not pass through validationStatus (#5376)
* fix(useFormControlForwardedProps): do not pass through validationStatus * Create kind-kiwis-crash.md --------- Co-authored-by: Jon Rohan <yes@jonrohan.codes>
1 parent 844e41f commit 167c8d4

File tree

3 files changed

+10
-13
lines changed

3 files changed

+10
-13
lines changed
 

‎.changeset/kind-kiwis-crash.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@primer/react": minor
3+
---
4+
5+
fix(useFormControlForwardedProps): do not pass through validationStatus

‎packages/react/src/FormControl/_FormControlContext.tsx

+3-11
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function useFormControlContext(): FormControlContext {
1717
return useContext(FormControlContext) ?? {}
1818
}
1919

20-
interface FormControlForwardedProps extends Omit<FormControlContext, 'captionId' | 'validationMessageId'> {
20+
interface FormControlForwardedProps extends Pick<FormControlProps, 'disabled' | 'id' | 'required'> {
2121
['aria-describedby']?: string
2222
}
2323

@@ -29,22 +29,14 @@ interface FormControlForwardedProps extends Omit<FormControlContext, 'captionId'
2929
* @param externalProps The external props passed to this component. If provided, these props will be merged with the
3030
* `FormControl` props, with external props taking priority.
3131
*/
32-
export function useFormControlForwardedProps<P>(externalProps: P): P & FormControlForwardedProps
33-
/**
34-
* Make any component compatible with `FormControl`'s automatic wiring up of accessibility attributes & validation by
35-
* reading the props from this hook and handling them / assigning them to the underlying form control. If used outside
36-
* of `FormControl`, this hook has no effect.
37-
*/
38-
export function useFormControlForwardedProps(): FormControlForwardedProps
39-
export function useFormControlForwardedProps(externalProps: FormControlForwardedProps = {}) {
32+
export function useFormControlForwardedProps<P>(externalProps: P): P & FormControlForwardedProps {
4033
const context = useContext(FormControlContext)
41-
if (!context) return externalProps
34+
if (!context) return externalProps as P & FormControlForwardedProps
4235

4336
return {
4437
disabled: context.disabled,
4538
id: context.id,
4639
required: context.required,
47-
validationStatus: context.validationStatus,
4840
['aria-describedby']: [context.validationMessageId, context.captionId].filter(Boolean).join(' ') || undefined,
4941
...externalProps,
5042
}

‎packages/react/src/__tests__/FormControl.test.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ describe('FormControl', () => {
458458
describe('useFormControlForwardedProps', () => {
459459
describe('when used outside FormControl', () => {
460460
test('returns empty object when no props object passed', () => {
461-
const result = renderHook(() => useFormControlForwardedProps())
461+
const result = renderHook(() => useFormControlForwardedProps({}))
462462
expect(result.result.current).toEqual({})
463463
})
464464

@@ -472,7 +472,7 @@ describe('useFormControlForwardedProps', () => {
472472
test('provides context value when no props object is passed', () => {
473473
const id = 'test-id'
474474

475-
const {result} = renderHook(() => useFormControlForwardedProps(), {
475+
const {result} = renderHook(() => useFormControlForwardedProps({}), {
476476
wrapper: ({children}: {children: React.ReactNode}) => (
477477
<FormControl id={id} disabled required>
478478
<FormControl.Label>Label</FormControl.Label>

0 commit comments

Comments
 (0)
Please sign in to comment.