feat(FieldError): expose elementType prop to control host element#9759
feat(FieldError): expose elementType prop to control host element#9759fellipeutaka wants to merge 4 commits intoadobe:mainfrom
Conversation
Fixes adobe#9757 — Option C: expose elementType on FieldErrorProps so users can set elementType="div" when wrapping block-level children (e.g. <ul>) without triggering the DOMElement render mismatch warning.
| expect(element.tagName).toBe('DIV'); | ||
| }); | ||
|
|
||
| it('allows block-level children when elementType is div', async () => { |
There was a problem hiding this comment.
Does this throw any sort of error? As far as I know jest won't do html validation
There was a problem hiding this comment.
You're right — jsdom doesn't validate HTML, so that test was meaningless. Replaced it with two tests that actually verify the console.warn behavior: one that asserts no warning fires when elementType="div" matches the render prop's returned element, and one that asserts the warning does fire when they mismatch (the original bug). The key insight is that children must also be provided so FieldErrorInner doesn't early-return before reaching Text/DOMElement.
|
You'll need to sign the CLA and close and re-open the issue for the build to pass. |
snowystinger
left a comment
There was a problem hiding this comment.
Thanks for fixing up those tests.
To the team, technically you can render
<FieldError elementType="div">
<ul><li>Error</li></ul>
</FieldError>
So the render prop + the elementType is only if you want to take over the rendered element entirely AND have it be a div.
Summary
Closes #9757
Implements Option C from the issue: expose
elementTypeonFieldErrorPropsso users can control the host element without triggering the render-mismatch warning.Problem
FieldErrordelegates toText, which defaultselementTypeto'span'. TheDOMElementutility warns whenrenderreturns an element that doesn't matchElementType. SinceTextalways passes'span'as the expected type, anyrenderoverride returning a block element (e.g.<div>) fires a spurious warning — even thoughFieldErroris typed forHTMLDivElement.Solution
Add
elementType?: stringtoFieldErrorPropsand pass it through toText. This lets users opt in to a block-level host element without going through therenderprop:Changes
packages/react-aria-components/src/FieldError.tsx— addelementTypetoFieldErrorPropsand forward toTextpackages/react-aria-components/test/FieldError.test.js— add tests for default tag,elementTypeoverride, and block-level children