-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Components: refactor away and delete the createComponent function
#34331
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
Conversation
|
Size Change: -29 B (0%) Total Size: 1.04 MB
ℹ️ View Unchanged
|
sarayourfriend
left a comment
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.
Looks good to me! Just one note about the displayName but doesn't really matter too much.
Awesome, thank you for taking a look! I will proceed with the rest of the components |
17f00ce to
ff83057
Compare
| * `Heading` will typically render the sizes `1`, `2`, `3`, `4`, `5`, or `6`, which map to `h1`-`h6`. | ||
| * | ||
| * @default 3 | ||
| * @default 2 |
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.
In the useHeading hook (defined in this same file), the default value of level is 2
| ); | ||
|
|
||
| const as = asProp || `h${ level }`; | ||
| const as = ( asProp || `h${ level }` ) as keyof JSX.IntrinsicElements; |
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.
Necessary to avoid a TypeScript error when passing this computed as prop to the View component
| 'aria-level'?: number; | ||
| } = {}; | ||
| if ( typeof as === 'string' && as[ 0 ] !== 'h' ) { | ||
| // if not a semantic `h` element, add a11y props: | ||
| a11yProps.role = 'heading'; | ||
| a11yProps[ 'aria-level' ] = level; | ||
| a11yProps[ 'aria-level' ] = | ||
| typeof level === 'string' ? parseInt( level ) : level; |
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.
Fixes a TypeScript error where the expected value of aria-level can only be number | undefined (and not string)
| */ | ||
| export default function useTruncate( props ) { | ||
| const { | ||
| as = 'span', |
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.
Previously, the Truncate component was being rendered as a span when calling the createComponent function. That function call is being removed in this PR, and so one way to render as a span is to default to it in the hook
4acf8e9 to
ace5aa7
Compare
| */ | ||
| export function useControlLabel( props ) { | ||
| const { | ||
| as = 'label', |
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.
Same motivation as #34331 (comment), but for the ControlLabel component, and with a default value for as of label
|
This PR is now ready for review. I've left inline comments in all of the parts where the changes were not trivial. Also, deleting the |
e4d83f6 to
e3eb15c
Compare
|
Rebased and solved conflicts after merging #34330 |
e3eb15c to
e3c6326
Compare
| * ``` | ||
| */ | ||
| const Text = createComponent( { | ||
| as: 'span', |
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.
This was refactored by assigning span as default value of as in the useText hook
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.
Why the refactor? It's not really equivalent to what was happening before (equivalent would be to add it directly to the View as="span" I think).
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.
I see. Does it mean that, for components using the createComponent function, the as prop is always going to be hardcoded? For example, the Text component will always be rendered as a span, even if we render it as <Text as="p" /> ?
|
This PR has been stale for a while. I've just rebased and pushed a couple of tweaks, it'd be great if it could get reviewed soon 🙇♂️ |
sarayourfriend
left a comment
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.
This is hard to test, it's such a big PR. By any chance would it be possible to split this PR into smaller more easily testable bits? That way you will probably get more traction from different people testing each PR.
| * ``` | ||
| */ | ||
| const Text = createComponent( { | ||
| as: 'span', |
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.
Why the refactor? It's not really equivalent to what was happening before (equivalent would be to add it directly to the View as="span" I think).
I will close this PR and open a series of smaller PRs — one for each of the 14 components, plus the final one to delete the |
Description
This PR deletes the
createComponentutility function, used by some components in the@wordpress/componentspackage.How has this been tested?
Screenshots
N/A
Types of changes
Refactor (deleting + refactoring code)
Checklist:
*.native.jsfiles for terms that need renaming or removal).Closes #34290