mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-11-07 15:07:13 +08:00
fix(labels): prevent clicking a label from triggering onClick twice on several components (#30384)
Issue number: resolves #30165 --------- <!-- Please do not submit updates to dependencies unless it fixes an issue. --> <!-- Please try to limit your pull request to one type (bugfix, feature, etc). Submit multiple pull requests if needed. --> ## What is the current behavior? <!-- Please describe the current behavior that you are modifying. --> Currently, several components will trigger their `onClick` twice if you click on their labels. ## What is the new behavior? <!-- Please describe the behavior or changes that are being added by this PR. --> After this fix, the affected components will only trigger `onClick` once per click of their labels or click directly on the element. ## Does this introduce a breaking change? - [ ] Yes - [X] No <!-- If this introduces a breaking change: 1. Describe the impact and migration path for existing applications below. 2. Update the BREAKING.md file with the breaking change. 3. Add "BREAKING CHANGE: [...]" to the commit description when merging. See https://github.com/ionic-team/ionic-framework/blob/main/docs/CONTRIBUTING.md#footer for more information. --> ## Other information <!-- Any other information that is important to this PR such as screenshots of how the component looks before and after the change. --> The affected components are: - Checkbox - Select - Textarea - Toggle - Input I also tested radio and range but could not reproduce the issue for them. Note that two of the components, checkbox and toggle, had to have special implementations for both their test and fix because of how the host component acts as the component for accessibility purposes. Current dev build: `8.5.7-dev.11746044124.147aab6c` --------- Co-authored-by: Maria Hutt <thetaPC@users.noreply.github.com> Co-authored-by: Brandy Smith <brandyscarney@users.noreply.github.com>
This commit is contained in:
@ -199,6 +199,14 @@ export class Checkbox implements ComponentInterface {
|
|||||||
this.toggleChecked(ev);
|
this.toggleChecked(ev);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stops propagation when the display label is clicked,
|
||||||
|
* otherwise, two clicks will be triggered.
|
||||||
|
*/
|
||||||
|
private onDivLabelClick = (ev: MouseEvent) => {
|
||||||
|
ev.stopPropagation();
|
||||||
|
};
|
||||||
|
|
||||||
private getHintTextID(): string | undefined {
|
private getHintTextID(): string | undefined {
|
||||||
const { el, helperText, errorText, helperTextId, errorTextId } = this;
|
const { el, helperText, errorText, helperTextId, errorTextId } = this;
|
||||||
|
|
||||||
@ -314,6 +322,7 @@ export class Checkbox implements ComponentInterface {
|
|||||||
}}
|
}}
|
||||||
part="label"
|
part="label"
|
||||||
id={this.inputLabelId}
|
id={this.inputLabelId}
|
||||||
|
onClick={this.onDivLabelClick}
|
||||||
>
|
>
|
||||||
<slot></slot>
|
<slot></slot>
|
||||||
{this.renderHintText()}
|
{this.renderHintText()}
|
||||||
|
|||||||
@ -99,4 +99,38 @@ configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, config }) =>
|
|||||||
expect(ionChange).not.toHaveReceivedEvent();
|
expect(ionChange).not.toHaveReceivedEvent();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test.describe(title('checkbox: click'), () => {
|
||||||
|
test('should trigger onclick only once when clicking the label', async ({ page }, testInfo) => {
|
||||||
|
testInfo.annotations.push({
|
||||||
|
type: 'issue',
|
||||||
|
description: 'https://github.com/ionic-team/ionic-framework/issues/30165',
|
||||||
|
});
|
||||||
|
|
||||||
|
// Create a spy function in page context
|
||||||
|
await page.setContent(`<ion-checkbox onclick="console.log('click called')">Test Checkbox</ion-checkbox>`, config);
|
||||||
|
|
||||||
|
// Track calls to the exposed function
|
||||||
|
let clickCount = 0;
|
||||||
|
page.on('console', (msg) => {
|
||||||
|
if (msg.text().includes('click called')) {
|
||||||
|
clickCount++;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const input = page.locator('div.label-text-wrapper');
|
||||||
|
|
||||||
|
// Use position to make sure we click into the label enough to trigger
|
||||||
|
// what would be the double click
|
||||||
|
await input.click({
|
||||||
|
position: {
|
||||||
|
x: 5,
|
||||||
|
y: 5,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Verify the click was triggered exactly once
|
||||||
|
expect(clickCount).toBe(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -720,6 +720,18 @@ export class Input implements ComponentInterface {
|
|||||||
return this.label !== undefined || this.labelSlot !== null;
|
return this.label !== undefined || this.labelSlot !== null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stops propagation when the label is clicked,
|
||||||
|
* otherwise, two clicks will be triggered.
|
||||||
|
*/
|
||||||
|
private onLabelClick = (ev: MouseEvent) => {
|
||||||
|
// Only stop propagation if the click was directly on the label
|
||||||
|
// and not on the input or other child elements
|
||||||
|
if (ev.target === ev.currentTarget) {
|
||||||
|
ev.stopPropagation();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Renders the border container
|
* Renders the border container
|
||||||
* when fill="outline".
|
* when fill="outline".
|
||||||
@ -815,9 +827,9 @@ export class Input implements ComponentInterface {
|
|||||||
* interactable, clicking the label would focus that instead
|
* interactable, clicking the label would focus that instead
|
||||||
* since it comes before the input in the DOM.
|
* since it comes before the input in the DOM.
|
||||||
*/}
|
*/}
|
||||||
<label class="input-wrapper" htmlFor={inputId}>
|
<label class="input-wrapper" htmlFor={inputId} onClick={this.onLabelClick}>
|
||||||
{this.renderLabelContainer()}
|
{this.renderLabelContainer()}
|
||||||
<div class="native-wrapper">
|
<div class="native-wrapper" onClick={this.onLabelClick}>
|
||||||
<slot name="start"></slot>
|
<slot name="start"></slot>
|
||||||
<input
|
<input
|
||||||
class="native-input"
|
class="native-input"
|
||||||
|
|||||||
@ -130,4 +130,81 @@ configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, screenshot, c
|
|||||||
await expect(item).toHaveScreenshot(screenshot(`input-with-clear-button-item-color`));
|
await expect(item).toHaveScreenshot(screenshot(`input-with-clear-button-item-color`));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test.describe(title('input: click'), () => {
|
||||||
|
test('should trigger onclick only once when clicking the label', async ({ page }, testInfo) => {
|
||||||
|
testInfo.annotations.push({
|
||||||
|
type: 'issue',
|
||||||
|
description: 'https://github.com/ionic-team/ionic-framework/issues/30165',
|
||||||
|
});
|
||||||
|
// Create a spy function in page context
|
||||||
|
await page.setContent(
|
||||||
|
`
|
||||||
|
<ion-input
|
||||||
|
label="Click Me"
|
||||||
|
value="Test Value"
|
||||||
|
></ion-input>
|
||||||
|
`,
|
||||||
|
config
|
||||||
|
);
|
||||||
|
|
||||||
|
// Track calls to the exposed function
|
||||||
|
const clickEvent = await page.spyOnEvent('click');
|
||||||
|
const input = page.locator('label.input-wrapper');
|
||||||
|
|
||||||
|
// Use position to make sure we click into the label enough to trigger
|
||||||
|
// what would be the double click
|
||||||
|
await input.click({
|
||||||
|
position: {
|
||||||
|
x: 5,
|
||||||
|
y: 5,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Verify the click was triggered exactly once
|
||||||
|
expect(clickEvent).toHaveReceivedEventTimes(1);
|
||||||
|
|
||||||
|
// Verify that the event target is the checkbox and not the item
|
||||||
|
const event = clickEvent.events[0];
|
||||||
|
expect((event.target as HTMLElement).tagName.toLowerCase()).toBe('ion-input');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should trigger onclick only once when clicking the wrapper', async ({ page }, testInfo) => {
|
||||||
|
testInfo.annotations.push({
|
||||||
|
type: 'issue',
|
||||||
|
description: 'https://github.com/ionic-team/ionic-framework/issues/30165',
|
||||||
|
});
|
||||||
|
// Create a spy function in page context
|
||||||
|
await page.setContent(
|
||||||
|
`
|
||||||
|
<ion-input
|
||||||
|
label="Click Me"
|
||||||
|
value="Test Value"
|
||||||
|
label-placement="floating"
|
||||||
|
></ion-input>
|
||||||
|
`,
|
||||||
|
config
|
||||||
|
);
|
||||||
|
|
||||||
|
// Track calls to the exposed function
|
||||||
|
const clickEvent = await page.spyOnEvent('click');
|
||||||
|
const input = page.locator('div.native-wrapper');
|
||||||
|
|
||||||
|
// Use position to make sure we click into the label enough to trigger
|
||||||
|
// what would be the double click
|
||||||
|
await input.click({
|
||||||
|
position: {
|
||||||
|
x: 1,
|
||||||
|
y: 1,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Verify the click was triggered exactly once
|
||||||
|
expect(clickEvent).toHaveReceivedEventTimes(1);
|
||||||
|
|
||||||
|
// Verify that the event target is the checkbox and not the item
|
||||||
|
const event = clickEvent.events[0];
|
||||||
|
expect((event.target as HTMLElement).tagName.toLowerCase()).toBe('ion-input');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -911,6 +911,18 @@ export class Select implements ComponentInterface {
|
|||||||
return this.label !== undefined || this.labelSlot !== null;
|
return this.label !== undefined || this.labelSlot !== null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stops propagation when the label is clicked,
|
||||||
|
* otherwise, two clicks will be triggered.
|
||||||
|
*/
|
||||||
|
private onLabelClick = (ev: MouseEvent) => {
|
||||||
|
// Only stop propagation if the click was directly on the label
|
||||||
|
// and not on the input or other child elements
|
||||||
|
if (ev.target === ev.currentTarget) {
|
||||||
|
ev.stopPropagation();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Renders the border container
|
* Renders the border container
|
||||||
* when fill="outline".
|
* when fill="outline".
|
||||||
@ -1173,7 +1185,7 @@ export class Select implements ComponentInterface {
|
|||||||
[`select-label-placement-${labelPlacement}`]: true,
|
[`select-label-placement-${labelPlacement}`]: true,
|
||||||
})}
|
})}
|
||||||
>
|
>
|
||||||
<label class="select-wrapper" id="select-label">
|
<label class="select-wrapper" id="select-label" onClick={this.onLabelClick}>
|
||||||
{this.renderLabelContainer()}
|
{this.renderLabelContainer()}
|
||||||
<div class="select-wrapper-inner">
|
<div class="select-wrapper-inner">
|
||||||
<slot name="start"></slot>
|
<slot name="start"></slot>
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
import { expect } from '@playwright/test';
|
import { expect } from '@playwright/test';
|
||||||
import { configs, test } from '@utils/test/playwright';
|
|
||||||
import type { E2ELocator } from '@utils/test/playwright';
|
import type { E2ELocator } from '@utils/test/playwright';
|
||||||
|
import { configs, test } from '@utils/test/playwright';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This checks that certain overlays open correctly. While the
|
* This checks that certain overlays open correctly. While the
|
||||||
@ -150,6 +150,45 @@ configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, config }) =>
|
|||||||
expect(alerts.length).toBe(1);
|
expect(alerts.length).toBe(1);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test.describe(title('select: click'), () => {
|
||||||
|
test('should trigger onclick only once when clicking the label', async ({ page }, testInfo) => {
|
||||||
|
testInfo.annotations.push({
|
||||||
|
type: 'issue',
|
||||||
|
description: 'https://github.com/ionic-team/ionic-framework/issues/30165',
|
||||||
|
});
|
||||||
|
// Create a spy function in page context
|
||||||
|
await page.setContent(
|
||||||
|
`
|
||||||
|
<ion-select aria-label="Fruit" interface="alert">
|
||||||
|
<ion-select-option value="apple">Apple</ion-select-option>
|
||||||
|
<ion-select-option value="banana">Banana</ion-select-option>
|
||||||
|
</ion-select>
|
||||||
|
`,
|
||||||
|
config
|
||||||
|
);
|
||||||
|
|
||||||
|
// Track calls to the exposed function
|
||||||
|
const clickEvent = await page.spyOnEvent('click');
|
||||||
|
const input = page.locator('label.select-wrapper');
|
||||||
|
|
||||||
|
// Use position to make sure we click into the label enough to trigger
|
||||||
|
// what would be the double click
|
||||||
|
await input.click({
|
||||||
|
position: {
|
||||||
|
x: 5,
|
||||||
|
y: 5,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Verify the click was triggered exactly once
|
||||||
|
expect(clickEvent).toHaveReceivedEventTimes(1);
|
||||||
|
|
||||||
|
// Verify that the event target is the checkbox and not the item
|
||||||
|
const event = clickEvent.events[0];
|
||||||
|
expect((event.target as HTMLElement).tagName.toLowerCase()).toBe('ion-select');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
35
core/src/components/textarea/test/basic/textarea.e2e.ts
Normal file
35
core/src/components/textarea/test/basic/textarea.e2e.ts
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import { expect } from '@playwright/test';
|
||||||
|
import { configs, test } from '@utils/test/playwright';
|
||||||
|
|
||||||
|
configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, config }) => {
|
||||||
|
test.describe(title('textarea: click'), () => {
|
||||||
|
test('should trigger onclick only once when clicking the label', async ({ page }, testInfo) => {
|
||||||
|
testInfo.annotations.push({
|
||||||
|
type: 'issue',
|
||||||
|
description: 'https://github.com/ionic-team/ionic-framework/issues/30165',
|
||||||
|
});
|
||||||
|
// Create a spy function in page context
|
||||||
|
await page.setContent(`<ion-textarea label="Textarea"></ion-textarea>`, config);
|
||||||
|
|
||||||
|
// Track calls to the exposed function
|
||||||
|
const clickEvent = await page.spyOnEvent('click');
|
||||||
|
const input = page.locator('label.textarea-wrapper');
|
||||||
|
|
||||||
|
// Use position to make sure we click into the label enough to trigger
|
||||||
|
// what would be the double click
|
||||||
|
await input.click({
|
||||||
|
position: {
|
||||||
|
x: 5,
|
||||||
|
y: 5,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Verify the click was triggered exactly once
|
||||||
|
expect(clickEvent).toHaveReceivedEventTimes(1);
|
||||||
|
|
||||||
|
// Verify that the event target is the checkbox and not the item
|
||||||
|
const event = clickEvent.events[0];
|
||||||
|
expect((event.target as HTMLElement).tagName.toLowerCase()).toBe('ion-textarea');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -572,6 +572,18 @@ export class Textarea implements ComponentInterface {
|
|||||||
return this.label !== undefined || this.labelSlot !== null;
|
return this.label !== undefined || this.labelSlot !== null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stops propagation when the label is clicked,
|
||||||
|
* otherwise, two clicks will be triggered.
|
||||||
|
*/
|
||||||
|
private onLabelClick = (ev: MouseEvent) => {
|
||||||
|
// Only stop propagation if the click was directly on the label
|
||||||
|
// and not on the input or other child elements
|
||||||
|
if (ev.target === ev.currentTarget) {
|
||||||
|
ev.stopPropagation();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Renders the border container when fill="outline".
|
* Renders the border container when fill="outline".
|
||||||
*/
|
*/
|
||||||
@ -726,7 +738,7 @@ export class Textarea implements ComponentInterface {
|
|||||||
* interactable, clicking the label would focus that instead
|
* interactable, clicking the label would focus that instead
|
||||||
* since it comes before the textarea in the DOM.
|
* since it comes before the textarea in the DOM.
|
||||||
*/}
|
*/}
|
||||||
<label class="textarea-wrapper" htmlFor={inputId}>
|
<label class="textarea-wrapper" htmlFor={inputId} onClick={this.onLabelClick}>
|
||||||
{this.renderLabelContainer()}
|
{this.renderLabelContainer()}
|
||||||
<div class="textarea-wrapper-inner">
|
<div class="textarea-wrapper-inner">
|
||||||
{/**
|
{/**
|
||||||
|
|||||||
38
core/src/components/toggle/test/basic/toggle.e2e.ts
Normal file
38
core/src/components/toggle/test/basic/toggle.e2e.ts
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
import { expect } from '@playwright/test';
|
||||||
|
import { configs, test } from '@utils/test/playwright';
|
||||||
|
|
||||||
|
configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, config }) => {
|
||||||
|
test.describe(title('toggle: click'), () => {
|
||||||
|
test('should trigger onclick only once when clicking the label', async ({ page }, testInfo) => {
|
||||||
|
testInfo.annotations.push({
|
||||||
|
type: 'issue',
|
||||||
|
description: 'https://github.com/ionic-team/ionic-framework/issues/30165',
|
||||||
|
});
|
||||||
|
|
||||||
|
// Create a spy function in page context
|
||||||
|
await page.setContent(`<ion-toggle onclick="console.log('click called')">my label</ion-toggle>`, config);
|
||||||
|
|
||||||
|
// Track calls to the exposed function
|
||||||
|
let clickCount = 0;
|
||||||
|
page.on('console', (msg) => {
|
||||||
|
if (msg.text().includes('click called')) {
|
||||||
|
clickCount++;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const input = page.locator('div.label-text-wrapper');
|
||||||
|
|
||||||
|
// Use position to make sure we click into the label enough to trigger
|
||||||
|
// what would be the double click
|
||||||
|
await input.click({
|
||||||
|
position: {
|
||||||
|
x: 5,
|
||||||
|
y: 5,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Verify the click was triggered exactly once
|
||||||
|
expect(clickCount).toBe(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -268,6 +268,14 @@ export class Toggle implements ComponentInterface {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stops propagation when the display label is clicked,
|
||||||
|
* otherwise, two clicks will be triggered.
|
||||||
|
*/
|
||||||
|
private onDivLabelClick = (ev: MouseEvent) => {
|
||||||
|
ev.stopPropagation();
|
||||||
|
};
|
||||||
|
|
||||||
private onFocus = () => {
|
private onFocus = () => {
|
||||||
this.ionFocus.emit();
|
this.ionFocus.emit();
|
||||||
};
|
};
|
||||||
@ -437,6 +445,7 @@ export class Toggle implements ComponentInterface {
|
|||||||
}}
|
}}
|
||||||
part="label"
|
part="label"
|
||||||
id={inputLabelId}
|
id={inputLabelId}
|
||||||
|
onClick={this.onDivLabelClick}
|
||||||
>
|
>
|
||||||
<slot></slot>
|
<slot></slot>
|
||||||
{this.renderHintText()}
|
{this.renderHintText()}
|
||||||
|
|||||||
Reference in New Issue
Block a user