-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use native
fieldset
instead of div
by default for <Fieldset />
…
…component (#3237) * improve TypeScript types for `Fieldset` component * use `fieldset` instead of `div` by default * only apply `role="group"` when not using a native `fieldset` * apply `disabled` attribute This is necessary if we want to make use of the default fieldset tag (which also disables native form elements) * adjust tests reflecting new changes * conditionally apply props based on rendered element * add `useResolvedTag` hook This allows us to compute the `tag` name of a component. We can use a shortcut based on the `props.as` and/or the `DEFAULT_XXX_TAG` of a component. If this is not known/passed, then we compute it based on the `ref` instead which requires an actual re-render. * use `useResolvedTag` hook * reflect change in `Field` related test * update changelog * inline variable
- Loading branch information
1 parent
8c3499c
commit f740050
Showing
5 changed files
with
97 additions
and
12 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { useCallback, useState } from 'react' | ||
|
||
/** | ||
* Resolve the actual rendered tag of a DOM node. If the `tag` provided is | ||
* already a string we can use that as-is. This will happen when the `as` prop is | ||
* not used or when it's used with a string value. | ||
* | ||
* If an actual component is used, then we need to do some more work because | ||
* then we actually need to render the component to know what the tag name is. | ||
*/ | ||
export function useResolvedTag<T extends React.ElementType>(tag: T) { | ||
let tagName = typeof tag === 'string' ? tag : undefined | ||
let [resolvedTag, setResolvedTag] = useState<string | undefined>(tagName) | ||
|
||
return [ | ||
// The resolved tag name | ||
tagName ?? resolvedTag, | ||
|
||
// This callback should be passed to the `ref` of a component | ||
useCallback( | ||
(ref: any) => { | ||
// Tag name is already known and it's a string, no need to re-render | ||
if (tagName) return | ||
|
||
if (ref instanceof HTMLElement) { | ||
// Tag name is not known yet, render the component to find out | ||
setResolvedTag(ref.tagName.toLowerCase()) | ||
} | ||
}, | ||
[tagName] | ||
), | ||
] as const | ||
} |