-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Truncate: improve handling of non-string children #57261
Conversation
61b3259
to
2871be5
Compare
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.
LGTM, thanks!
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.
Interesting, yes it looks like the Truncate
/Text
primitives are designed to be quite permissive with the type of their children!
I think the approach here is good (and of course the added tests!), although I'd like to suggest a few more things to mitigate consumer confusion:
- We should mention in the
children
prop description that truncation won't happen unless it's string/number. - I noticed that
Text
actually throws when you try to use the highlight feature with a non-string. Obviously for back-compat reasons we can't start throwing here now, but maybe it merits awarn()
? (No strong opinion on this one)
Thank you for the review, @ntsekouras and @mirka !
Sounds good, I went ahead and implemented the suggested changes. |
childrenAsText = children.toString(); | ||
} else { | ||
// eslint-disable-next-line no-console | ||
console.warn( |
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 think it's preferable to use @wordpress/warning
?
} else { | ||
// eslint-disable-next-line no-console | ||
console.warn( | ||
`Truncate: text truncation has been disabled, since it is only available when passing 'children' of type 'string' or 'number'` |
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.
`Truncate: text truncation has been disabled, since it is only available when passing 'children' of type 'string' or 'number'` | |
`wp.components.Truncate: text truncation has been disabled, since it is only available when passing 'children' of type 'string' or 'number'` |
Probably better for clarity, since Truncate
is used under the hood for a handful of more common components and the consumer may not be aware of what "Truncate" is specifically.
Looking more into unit tests failures, it emerges that a lot of components use The problem with that is that the gutenberg/packages/components/src/text/hook.ts Lines 144 to 183 in 830d13b
Therefore, I don't think that showing a warning makes a lot of sense because it would just pollute the console for a lot of Text` consumers. |
ff93065
to
23818de
Compare
Flaky tests detected in 23818de. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7286939991
|
What?
This PR introduces a few changes to the
Truncate
component to make it more resilient to different types ofchildren
received:number
children
have types other thanstring
ornumber
, the component renders thechildren
without any attempted truncation (instead of not rendering thechildren
at all)Why?
While working on #55625, I discovered that
Truncate
only handleschildren
of typestring
, and otherwise renders an empty<span />
tag.The component falsely advertises that its
children
are of typeReactNode
, while in reality the component only knows how to deal withstring
.This behaviour is not great, and can cause confusion and disruption in the consumers of
Truncate
How?
Tweaked the logic of the truncate hook:
children
that are notstring
ofnumber
type, the component simply no-ops and renderschildren
as-is