-
Notifications
You must be signed in to change notification settings - Fork 13.3k
fix(react): don't render false boolean props as attributes #31294
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
Open
ShaneK
wants to merge
3
commits into
major-9.0
Choose a base branch
from
FW-7393-2
base: major-9.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
44 changes: 44 additions & 0 deletions
44
packages/react-router/test/base/src/pages/disabled-button/DisabledButton.tsx
This file contains hidden or 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,44 @@ | ||
| import { IonButton, IonContent, IonHeader, IonPage, IonTitle, IonToolbar } from '@ionic/react'; | ||
| import React, { useState } from 'react'; | ||
|
|
||
| /** | ||
| * `<IonButton disabled={false}>` must not render a `disabled="false"` attribute | ||
| * on the host. ion-button is a routing-wrapped component (createRoutingComponent), | ||
| * so this validates the fix end-to-end with a real component in a real browser. | ||
| */ | ||
| const DisabledButton: React.FC = () => { | ||
| const [toggleDisabled, setToggleDisabled] = useState(false); | ||
|
|
||
| return ( | ||
| <IonPage data-pageid="disabled-button"> | ||
| <IonHeader> | ||
| <IonToolbar> | ||
| <IonTitle>Disabled Button</IonTitle> | ||
| </IonToolbar> | ||
| </IonHeader> | ||
| <IonContent> | ||
| <IonButton id="btn-false" disabled={false}> | ||
| disabled false | ||
| </IonButton> | ||
|
|
||
| <IonButton id="btn-true" disabled={true}> | ||
| disabled true | ||
| </IonButton> | ||
|
|
||
| <IonButton id="btn-aria" aria-expanded={false}> | ||
| aria-expanded false | ||
| </IonButton> | ||
|
|
||
| <IonButton id="btn-toggle" disabled={toggleDisabled}> | ||
| toggle target | ||
| </IonButton> | ||
|
|
||
| <IonButton id="btn-do-toggle" onClick={() => setToggleDisabled((v) => !v)}> | ||
| toggle | ||
| </IonButton> | ||
| </IonContent> | ||
| </IonPage> | ||
| ); | ||
| }; | ||
|
|
||
| export default DisabledButton; |
53 changes: 53 additions & 0 deletions
53
packages/react-router/test/base/tests/e2e/playwright/disabled-button.spec.ts
This file contains hidden or 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,53 @@ | ||
| import { test, expect } from '@playwright/test'; | ||
|
|
||
| import { ionPageVisible, withTestingMode } from './utils/test-utils'; | ||
|
|
||
| /** | ||
| * `<IonButton disabled={false}>` must not leave a `disabled="false"` attribute | ||
| * on the host. ion-button is routing-wrapped (createRoutingComponent), so this | ||
| * is the real end-to-end check with an actual @ionic/react component. | ||
| */ | ||
| test.describe('IonButton disabled boolean attribute', () => { | ||
| test.beforeEach(async ({ page }) => { | ||
| await page.goto(withTestingMode('/disabled-button')); | ||
| await ionPageVisible(page, 'disabled-button'); | ||
| }); | ||
|
|
||
| test('disabled={false} should not render a disabled attribute', async ({ page }, testInfo) => { | ||
| testInfo.annotations.push({ | ||
| type: 'issue', | ||
| description: 'https://github.com/ionic-team/ionic-framework/issues/27930', | ||
| }); | ||
|
|
||
| const button = page.locator('#btn-false'); | ||
| await expect(button).toHaveJSProperty('disabled', false); | ||
| await expect(button).not.toHaveAttribute('disabled', /.*/); | ||
| // The inner native button must be interactive. | ||
| await expect(button.locator('button')).not.toBeDisabled(); | ||
| }); | ||
|
|
||
| test('disabled={true} should render a disabled, non-interactive button', async ({ page }) => { | ||
| const button = page.locator('#btn-true'); | ||
| await expect(button).toHaveJSProperty('disabled', true); | ||
| await expect(button).toHaveAttribute('disabled', ''); | ||
| await expect(button.locator('button')).toBeDisabled(); | ||
| }); | ||
|
|
||
| test('aria-expanded={false} should be preserved (meaningful, not a boolean attribute)', async ({ page }) => { | ||
| // ion-button relocates aria-* from the host onto its inner native button, so | ||
| // the wrapper must NOT strip aria-expanded="false" (unlike disabled="false"): | ||
| // it has to survive on the host long enough to be inherited here. | ||
| const nativeButton = page.locator('#btn-aria button'); | ||
| await expect(nativeButton).toHaveAttribute('aria-expanded', 'false'); | ||
| }); | ||
|
|
||
| test('toggling disabled false -> true should disable the button', async ({ page }) => { | ||
| const button = page.locator('#btn-toggle'); | ||
| await expect(button).not.toHaveAttribute('disabled', /.*/); | ||
|
|
||
| await page.locator('#btn-do-toggle').click(); | ||
|
|
||
| await expect(button).toHaveAttribute('disabled', ''); | ||
| await expect(button).toHaveJSProperty('disabled', true); | ||
| }); | ||
| }); |
104 changes: 104 additions & 0 deletions
104
packages/react/src/components/__tests__/createRoutingComponent.spec.tsx
This file contains hidden or 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,104 @@ | ||
| // Jest can't import Ionic's ESM-only custom elements; stub the values the | ||
| // wrapper's util chain reaches. | ||
| jest.mock('@ionic/core/components', () => ({ | ||
| getPlatforms: () => [], | ||
| isPlatform: () => false, | ||
| componentOnReady: (_el: HTMLElement, cb: () => void) => cb(), | ||
| })); | ||
|
|
||
| import { act, render } from '@testing-library/react'; | ||
|
|
||
| import { createRoutingComponent } from '../createRoutingComponent'; | ||
|
|
||
| /** | ||
| * Routing-wrapped components (ion-button, ion-card, ion-fab-button, | ||
| * ion-item-option, ion-breadcrumb, ion-router-link) go through | ||
| * `createRoutingComponent`, which is not covered by the @lit/react runtime that | ||
| * fixes the generated components on v9. `disabled={false}` must not leave a | ||
| * stray `disabled="false"` on the host, since presence of an HTML boolean | ||
| * attribute means "true" to assistive tech. | ||
| */ | ||
| const RoutingEl = createRoutingComponent<any, any>('fake-routing-el'); | ||
|
|
||
| describe('createRoutingComponent boolean attributes', () => { | ||
| it('should not leave a disabled="false" attribute when disabled={false}', () => { | ||
| const { container } = render(<RoutingEl disabled={false}>x</RoutingEl>); | ||
| const el = container.querySelector('fake-routing-el')!; | ||
|
|
||
| expect(el.hasAttribute('disabled')).toBe(false); | ||
| }); | ||
|
|
||
| it('should preserve aria-* attributes set to false (meaningful, not boolean attributes)', () => { | ||
| const { container } = render(<RoutingEl aria-expanded={false}>x</RoutingEl>); | ||
| const el = container.querySelector('fake-routing-el')!; | ||
|
|
||
| // aria-expanded="false" means "collapsed", distinct from the attribute being absent. | ||
| expect(el.getAttribute('aria-expanded')).toBe('false'); | ||
| }); | ||
|
|
||
| it('should preserve enumerated attributes set to false (e.g. draggable)', () => { | ||
| const { container } = render(<RoutingEl draggable={false}>x</RoutingEl>); | ||
| const el = container.querySelector('fake-routing-el')!; | ||
|
|
||
| // draggable="false" explicitly disables dragging, distinct from absence. | ||
| expect(el.getAttribute('draggable')).toBe('false'); | ||
| }); | ||
|
|
||
| it('should preserve camelCased enumerated attributes set to false (e.g. spellCheck)', () => { | ||
| const { container } = render(<RoutingEl spellCheck={false}>x</RoutingEl>); | ||
| const el = container.querySelector('fake-routing-el')!; | ||
|
|
||
| // The wrapper dash-cases React prop names, so spellCheck renders as | ||
| // spell-check; spell-check="false" is meaningful and must survive. | ||
| expect(el.getAttribute('spell-check')).toBe('false'); | ||
| }); | ||
|
|
||
| it('should keep the disabled attribute when disabled={true}', () => { | ||
| const { container } = render(<RoutingEl disabled={true}>x</RoutingEl>); | ||
| const el = container.querySelector('fake-routing-el')!; | ||
|
|
||
| expect(el.hasAttribute('disabled')).toBe(true); | ||
| }); | ||
|
|
||
| it('should disable the element when toggling disabled false -> true', () => { | ||
| const { container, rerender } = render(<RoutingEl disabled={false}>x</RoutingEl>); | ||
| const el = container.querySelector('fake-routing-el')!; | ||
| expect(el.hasAttribute('disabled')).toBe(false); | ||
|
|
||
| act(() => { | ||
| rerender(<RoutingEl disabled={true}>x</RoutingEl>); | ||
| }); | ||
|
|
||
| expect(el.hasAttribute('disabled')).toBe(true); | ||
| }); | ||
|
|
||
| it('should drop the attribute when toggling disabled true -> false', () => { | ||
| const { container, rerender } = render(<RoutingEl disabled={true}>x</RoutingEl>); | ||
| const el = container.querySelector('fake-routing-el')!; | ||
|
|
||
| act(() => { | ||
| rerender(<RoutingEl disabled={false}>x</RoutingEl>); | ||
| }); | ||
|
|
||
| expect(el.hasAttribute('disabled')).toBe(false); | ||
| }); | ||
|
|
||
| it('should not re-add disabled="false" after an unrelated re-render', () => { | ||
| const { container, rerender } = render( | ||
| <RoutingEl disabled={false} title="a"> | ||
| x | ||
| </RoutingEl> | ||
| ); | ||
| const el = container.querySelector('fake-routing-el')!; | ||
|
|
||
| act(() => { | ||
| rerender( | ||
| <RoutingEl disabled={false} title="b"> | ||
| x | ||
| </RoutingEl> | ||
| ); | ||
| }); | ||
|
|
||
| expect(el.hasAttribute('disabled')).toBe(false); | ||
| }); | ||
| }); |
This file contains hidden or 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
46 changes: 46 additions & 0 deletions
46
packages/react/src/components/react-component-lib/__tests__/createComponent.spec.tsx
This file contains hidden or 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,46 @@ | ||
| import { act, render } from '@testing-library/react'; | ||
|
|
||
| import { createReactComponent } from '../createComponent'; | ||
|
|
||
| /** | ||
| * Components built with createReactComponent (e.g. IonBackButton, IonTabButton | ||
| * via inner-proxies) render attributes directly and sync props through | ||
| * attachProps, so they must get the same `disabled="false"` stripping as | ||
| * routing-wrapped components. Presence of an HTML boolean attribute means "true" | ||
| * to assistive tech, so a false-valued boolean must not leave a stray attribute. | ||
| */ | ||
| const ReactEl = createReactComponent<any, any>('fake-react-el'); | ||
|
|
||
| describe('createReactComponent boolean attributes', () => { | ||
| it('should not leave a disabled="false" attribute when disabled={false}', () => { | ||
| const { container } = render(<ReactEl disabled={false}>x</ReactEl>); | ||
| const el = container.querySelector('fake-react-el')!; | ||
|
|
||
| expect(el.hasAttribute('disabled')).toBe(false); | ||
| }); | ||
|
|
||
| it('should preserve aria-* attributes set to false', () => { | ||
| const { container } = render(<ReactEl aria-expanded={false}>x</ReactEl>); | ||
| const el = container.querySelector('fake-react-el')!; | ||
|
|
||
| expect(el.getAttribute('aria-expanded')).toBe('false'); | ||
| }); | ||
|
|
||
| it('should keep the disabled attribute when disabled={true}', () => { | ||
| const { container } = render(<ReactEl disabled={true}>x</ReactEl>); | ||
| const el = container.querySelector('fake-react-el')!; | ||
|
|
||
| expect(el.hasAttribute('disabled')).toBe(true); | ||
| }); | ||
|
|
||
| it('should drop the attribute when toggling disabled true -> false', () => { | ||
| const { container, rerender } = render(<ReactEl disabled={true}>x</ReactEl>); | ||
| const el = container.querySelector('fake-react-el')!; | ||
|
|
||
| act(() => { | ||
| rerender(<ReactEl disabled={false}>x</ReactEl>); | ||
| }); | ||
|
|
||
| expect(el.hasAttribute('disabled')).toBe(false); | ||
| }); | ||
| }); |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can we add this to the landing page? My concern is new pages might get forgotten over time if we don't put them there.
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.
Done! 96b696d
I was hesitant about having this in the router at all, but I think it's fine ultimately. Maybe someday it should evolve into a more general use page, or we just convert the normal react tests to use playwright too for better tests like what I needed here