-
Notifications
You must be signed in to change notification settings - Fork 13.4k
fix(range): improve focus and blur handling for dual knobs #30482
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
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5e5efaf
fix(range): improve focus and blur handling for dual knobs
ShaneK 40db335
test(range): emit ionFocus only once per focus event
ShaneK ea1aac7
Merge branch 'main' of github.com:ionic-team/ionic-framework into FW-…
ShaneK 3f514ff
test(range): improving test of dual knob range slider for actual usag…
ShaneK 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
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,201 @@ | ||
| import { newSpecPage } from '@stencil/core/testing'; | ||
|
|
||
| import { Range } from '../../range'; | ||
|
|
||
| describe('range: dual knobs focus management', () => { | ||
| it('should properly manage initial focus with dual knobs', async () => { | ||
| const page = await newSpecPage({ | ||
| components: [Range], | ||
| html: ` | ||
| <ion-range dual-knobs="true" min="0" max="100" value='{"lower": 25, "upper": 75}' aria-label="Dual range"> | ||
| </ion-range> | ||
| `, | ||
| }); | ||
|
|
||
| const range = page.body.querySelector('ion-range'); | ||
| expect(range).not.toBeNull(); | ||
|
|
||
| await page.waitForChanges(); | ||
|
|
||
| // Get the knob elements | ||
| const knobA = range!.shadowRoot!.querySelector('.range-knob-a') as HTMLElement; | ||
| const knobB = range!.shadowRoot!.querySelector('.range-knob-b') as HTMLElement; | ||
|
|
||
| expect(knobA).not.toBeNull(); | ||
| expect(knobB).not.toBeNull(); | ||
|
|
||
| // Initially, neither knob should have the ion-focused class | ||
| expect(knobA.classList.contains('ion-focused')).toBe(false); | ||
| expect(knobB.classList.contains('ion-focused')).toBe(false); | ||
| }); | ||
|
|
||
| it('should show focus on the correct knob when focused via keyboard navigation', async () => { | ||
| const page = await newSpecPage({ | ||
| components: [Range], | ||
| html: ` | ||
| <ion-range dual-knobs="true" min="0" max="100" value='{"lower": 25, "upper": 75}' aria-label="Dual range"> | ||
| </ion-range> | ||
| `, | ||
| }); | ||
|
|
||
| const range = page.body.querySelector('ion-range'); | ||
| await page.waitForChanges(); | ||
|
|
||
| const knobA = range!.shadowRoot!.querySelector('.range-knob-a') as HTMLElement; | ||
| const knobB = range!.shadowRoot!.querySelector('.range-knob-b') as HTMLElement; | ||
|
|
||
| // Focus knob A | ||
| knobA.dispatchEvent(new Event('focus')); | ||
| await page.waitForChanges(); | ||
|
|
||
| // Only knob A should have the ion-focused class | ||
| expect(knobA.classList.contains('ion-focused')).toBe(true); | ||
| expect(knobB.classList.contains('ion-focused')).toBe(false); | ||
|
|
||
| // Focus knob B | ||
| knobB.dispatchEvent(new Event('focus')); | ||
| await page.waitForChanges(); | ||
|
|
||
| // Only knob B should have the ion-focused class | ||
| expect(knobA.classList.contains('ion-focused')).toBe(false); | ||
| expect(knobB.classList.contains('ion-focused')).toBe(true); | ||
| }); | ||
|
|
||
| it('should remove focus from all knobs when focus leaves the range', async () => { | ||
| const page = await newSpecPage({ | ||
| components: [Range], | ||
| html: ` | ||
| <ion-range dual-knobs="true" min="0" max="100" value='{"lower": 25, "upper": 75}' aria-label="Dual range"> | ||
| </ion-range> | ||
| `, | ||
| }); | ||
|
|
||
| const range = page.body.querySelector('ion-range'); | ||
| await page.waitForChanges(); | ||
|
|
||
| const knobA = range!.shadowRoot!.querySelector('.range-knob-a') as HTMLElement; | ||
| const knobB = range!.shadowRoot!.querySelector('.range-knob-b') as HTMLElement; | ||
|
|
||
| // Focus knob A | ||
| knobA.dispatchEvent(new Event('focus')); | ||
| await page.waitForChanges(); | ||
|
|
||
| expect(knobA.classList.contains('ion-focused')).toBe(true); | ||
|
|
||
| // Blur the knob (focus leaves the range) | ||
| knobA.dispatchEvent(new Event('blur')); | ||
| await page.waitForChanges(); | ||
|
|
||
| // Wait for the timeout in onKnobBlur to complete | ||
| await new Promise((resolve) => setTimeout(resolve, 10)); | ||
| await page.waitForChanges(); | ||
|
|
||
| // Neither knob should have the ion-focused class | ||
| expect(knobA.classList.contains('ion-focused')).toBe(false); | ||
| expect(knobB.classList.contains('ion-focused')).toBe(false); | ||
| }); | ||
|
|
||
| it('should emit ionFocus when any knob receives focus but only once until blur', async () => { | ||
| const page = await newSpecPage({ | ||
| components: [Range], | ||
| html: ` | ||
| <ion-range dual-knobs="true" min="0" max="100" value='{"lower": 25, "upper": 75}' aria-label="Dual range"> | ||
| </ion-range> | ||
| `, | ||
| }); | ||
|
|
||
| const range = page.body.querySelector('ion-range')!; | ||
| await page.waitForChanges(); | ||
|
|
||
| let focusEventFiredCount = 0; | ||
| range.addEventListener('ionFocus', () => { | ||
| focusEventFiredCount++; | ||
| }); | ||
|
|
||
| const knobA = range.shadowRoot!.querySelector('.range-knob-a') as HTMLElement; | ||
| const knobB = range.shadowRoot!.querySelector('.range-knob-b') as HTMLElement; | ||
|
|
||
| // Focus knob A | ||
| knobA.dispatchEvent(new Event('focus')); | ||
| knobB.dispatchEvent(new Event('focus')); | ||
| await page.waitForChanges(); | ||
|
|
||
| expect(focusEventFiredCount).toBe(1); | ||
| }); | ||
|
|
||
| it('should emit ionBlur when focus leaves the range completely', async () => { | ||
| const page = await newSpecPage({ | ||
| components: [Range], | ||
| html: ` | ||
| <ion-range dual-knobs="true" min="0" max="100" value='{"lower": 25, "upper": 75}' aria-label="Dual range"> | ||
| </ion-range> | ||
| `, | ||
| }); | ||
|
|
||
| const range = page.body.querySelector('ion-range')!; | ||
| await page.waitForChanges(); | ||
|
|
||
| let blurEventFired = false; | ||
| range.addEventListener('ionBlur', () => { | ||
| blurEventFired = true; | ||
| }); | ||
|
|
||
| const knobA = range.shadowRoot!.querySelector('.range-knob-a') as HTMLElement; | ||
|
|
||
| // Focus and then blur knob A | ||
| knobA.dispatchEvent(new Event('focus')); | ||
| await page.waitForChanges(); | ||
|
|
||
| knobA.dispatchEvent(new Event('blur')); | ||
| await page.waitForChanges(); | ||
|
|
||
| // Wait for the timeout in onKnobBlur to complete | ||
| await new Promise((resolve) => setTimeout(resolve, 10)); | ||
| await page.waitForChanges(); | ||
|
|
||
| expect(blurEventFired).toBe(true); | ||
| }); | ||
|
|
||
| it('should correctly handle Tab navigation between knobs', async () => { | ||
| const page = await newSpecPage({ | ||
| components: [Range], | ||
| html: ` | ||
| <ion-range dual-knobs="true" min="0" max="100" value='{"lower": 25, "upper": 75}' aria-label="Dual range"> | ||
| </ion-range> | ||
| `, | ||
| }); | ||
|
|
||
| const range = page.body.querySelector('ion-range')!; | ||
| await page.waitForChanges(); | ||
|
|
||
| const knobA = range.shadowRoot!.querySelector('.range-knob-a') as HTMLElement; | ||
| const knobB = range.shadowRoot!.querySelector('.range-knob-b') as HTMLElement; | ||
|
|
||
| // Simulate Tab to first knob | ||
| knobA.dispatchEvent(new Event('focus')); | ||
| await page.waitForChanges(); | ||
|
|
||
| // First knob should be focused | ||
| expect(knobA.classList.contains('ion-focused')).toBe(true); | ||
| expect(knobB.classList.contains('ion-focused')).toBe(false); | ||
|
|
||
| // Simulate Tab to second knob (blur first, focus second) | ||
| knobA.dispatchEvent(new Event('blur')); | ||
| knobB.dispatchEvent(new Event('focus')); | ||
| await page.waitForChanges(); | ||
|
|
||
| // Second knob should be focused, first should not | ||
| expect(knobA.classList.contains('ion-focused')).toBe(false); | ||
| expect(knobB.classList.contains('ion-focused')).toBe(true); | ||
|
|
||
| // Verify Arrow key navigation still works on focused knob | ||
|
|
||
| // Simulate Arrow Right key press on knob B | ||
| const keyEvent = new KeyboardEvent('keydown', { key: 'ArrowRight' }); | ||
| knobB.dispatchEvent(keyEvent); | ||
| await page.waitForChanges(); | ||
|
|
||
| // The knob that visually appears focused should be the one that responds to keyboard input | ||
| expect(knobB.classList.contains('ion-focused')).toBe(true); | ||
| }); | ||
| }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.