diff --git a/core/src/components/checkbox/checkbox.tsx b/core/src/components/checkbox/checkbox.tsx index 6d0906d9e22..361f897e30f 100644 --- a/core/src/components/checkbox/checkbox.tsx +++ b/core/src/components/checkbox/checkbox.tsx @@ -1,8 +1,8 @@ import type { ComponentInterface, EventEmitter } from '@stencil/core'; -import { Build, Component, Element, Event, Host, Method, Prop, State, forceUpdate, h } from '@stencil/core'; +import { Build, Component, Element, Event, Host, Listen, Method, Prop, State, forceUpdate, h } from '@stencil/core'; import { checkInvalidState, createItemMultipleInputsObserver } from '@utils/forms'; import type { Attributes } from '@utils/helpers'; -import { inheritAriaAttributes, renderHiddenInput } from '@utils/helpers'; +import { inheritAriaAttributes, raf, renderHiddenInput } from '@utils/helpers'; import { createColorClasses, hostContext } from '@utils/theme'; import { config } from '../../global/config'; @@ -11,6 +11,13 @@ import type { Color, Theme } from '../../interface'; import type { CheckboxChangeEventDetail } from './checkbox-interface'; +/** + * Slotted label content that handles its own click, so clicking it + * should not toggle the checkbox. + */ +const INTERACTIVE_LABEL_CONTENT = + 'a[href], button, details, embed, iframe, select, textarea, input:not([type="hidden"]), audio[controls], video[controls], [contenteditable]:not([contenteditable="false"]), .ion-activatable'; + /** * @virtualProp {"ios" | "md"} mode - The mode determines the platform behaviors of the component. * @virtualProp {"ios" | "md" | "ionic"} theme - The theme determines the visual appearance of the component. @@ -42,6 +49,16 @@ export class Checkbox implements ComponentInterface { private inheritedAttributes: Attributes = {}; private validationObserver?: MutationObserver; private itemFocusObserver?: MutationObserver; + private nativeInput?: HTMLInputElement; + + /** + * Set when a click starts on the slotted label content, and cleared when + * that click reaches the label text wrapper. If it is still set when the + * label forwards its click to the native input, the slotted content either + * handled the click itself or stopped its propagation, so the checkbox + * must not toggle. + */ + private skipLabelActivation = false; @Element() el!: HTMLIonCheckboxElement; @@ -290,12 +307,70 @@ export class Checkbox implements ComponentInterface { this.toggleChecked(ev); }; + /** + * Prevents toggling the checkbox when slotted label content handles its + * own click: either it is interactive content, such as a button or link, + * or its click handler stops propagation, such as an icon that opens a + * help popover. + * + * Clicking anything inside of the `