From c24d39a9ba84d2c33174de85fbca5e4b6781ee1d Mon Sep 17 00:00:00 2001 From: lz405 <179817187+lz405@users.noreply.github.com> Date: Thu, 24 Oct 2024 12:21:56 +0000 Subject: [PATCH 1/2] fix(react): add relevant storybook component to demostrate the onClick event add a button under a radio option to show the onClick event works --- .../react/src/stories/ic-radio-group.stories.mdx | 11 ++++++----- .../ic-radio-group/ic-radio-group.stories.mdx | 14 ++++++++------ 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/packages/react/src/stories/ic-radio-group.stories.mdx b/packages/react/src/stories/ic-radio-group.stories.mdx index 6f7626017e..2e8e373ea7 100644 --- a/packages/react/src/stories/ic-radio-group.stories.mdx +++ b/packages/react/src/stories/ic-radio-group.stories.mdx @@ -136,13 +136,14 @@ import { useState, useEffect, useRef } from "react"; - + onClick={()=>alert("Button clicked!")} + > + Click me for an alert! + diff --git a/packages/web-components/src/components/ic-radio-group/ic-radio-group.stories.mdx b/packages/web-components/src/components/ic-radio-group/ic-radio-group.stories.mdx index d796e89767..c37ca3c2d0 100644 --- a/packages/web-components/src/components/ic-radio-group/ic-radio-group.stories.mdx +++ b/packages/web-components/src/components/ic-radio-group/ic-radio-group.stories.mdx @@ -152,6 +152,12 @@ import readme from "./readme.md"; function handleIcChange(ev) { console.log(ev.detail.selectedOption); } + function handleClick() { + alert("Button clicked!"); + } + document + .querySelector("ic-button") + .addEventListener("click", handleClick); document .querySelector("ic-radio-group") .addEventListener("icChange", handleIcChange); @@ -186,13 +192,9 @@ import readme from "./readme.md"; - + Click me! ` From 05f24831cd9f6e2cf8ed283bb6196a10d31c2586 Mon Sep 17 00:00:00 2001 From: lz405 <179817187+lz405@users.noreply.github.com> Date: Tue, 22 Oct 2024 09:26:15 +0000 Subject: [PATCH 2/2] feat(web-components): cherry-pick cherry-pick --- .../test/basic/ic-radio-group.spec.ts | 4 +- .../ic-radio-option/ic-radio-option.css | 1 + .../ic-radio-option/ic-radio-option.tsx | 58 ++++++++++++------- 3 files changed, 41 insertions(+), 22 deletions(-) diff --git a/packages/web-components/src/components/ic-radio-group/test/basic/ic-radio-group.spec.ts b/packages/web-components/src/components/ic-radio-group/test/basic/ic-radio-group.spec.ts index 515729a652..19b666969f 100644 --- a/packages/web-components/src/components/ic-radio-group/test/basic/ic-radio-group.spec.ts +++ b/packages/web-components/src/components/ic-radio-group/test/basic/ic-radio-group.spec.ts @@ -227,7 +227,7 @@ describe("ic-radio-group", () => { const callbackFn = jest.fn(); page.doc.addEventListener("icCheck", callbackFn); - page.rootInstance.textfieldValueHandler({ + page.rootInstance.additionalFieldValueHandler({ detail: { value: "value" }, stopImmediatePropagation: jest.fn(), }); @@ -246,7 +246,7 @@ describe("ic-radio-group", () => { const callbackFn = jest.fn(); page.doc.addEventListener("icCheck", callbackFn); - page.rootInstance.textfieldValueHandler({ + page.rootInstance.additionalFieldValueHandler({ detail: { value: "" }, stopImmediatePropagation: jest.fn(), }); diff --git a/packages/web-components/src/components/ic-radio-option/ic-radio-option.css b/packages/web-components/src/components/ic-radio-option/ic-radio-option.css index 7a3a120737..28c5f67371 100644 --- a/packages/web-components/src/components/ic-radio-option/ic-radio-option.css +++ b/packages/web-components/src/components/ic-radio-option/ic-radio-option.css @@ -19,6 +19,7 @@ /* Focus states */ .container input:focus + span.checkmark, +.container:active input:focus + span.checkmark, :host(:focus) .container input:checked + span.checkmark { box-shadow: var(--ic-border-focus); } diff --git a/packages/web-components/src/components/ic-radio-option/ic-radio-option.tsx b/packages/web-components/src/components/ic-radio-option/ic-radio-option.tsx index 9fafbd835b..31af759b54 100644 --- a/packages/web-components/src/components/ic-radio-option/ic-radio-option.tsx +++ b/packages/web-components/src/components/ic-radio-option/ic-radio-option.tsx @@ -22,7 +22,6 @@ import { } from "../../utils/helpers"; const ADDITIONAL_FIELD = "additional-field"; -const TEXT_FIELD_SELECTOR = "ic-text-field"; /** * @slot additional-field - Content to displayed alongside a radio option. @@ -141,8 +140,7 @@ export class RadioOption { componentWillLoad(): void { if (isSlotUsed(this.el, ADDITIONAL_FIELD)) { this.hasAdditionalField = true; - const textField = this.el.querySelector(TEXT_FIELD_SELECTOR); - if (textField) textField.hiddenInput = false; + this.getAdditionalField(); } this.defaultRadioValue = this.value; @@ -163,8 +161,7 @@ export class RadioOption { const hasSlot = isSlotUsed(this.el, ADDITIONAL_FIELD); if (hasSlot && !this.hasAdditionalField) { this.hasAdditionalField = true; - const textField = this.el.querySelector(TEXT_FIELD_SELECTOR); - if (textField) textField.hiddenInput = false; + this.getAdditionalField(); } else if (!hasSlot && this.hasAdditionalField) { this.hasAdditionalField = false; } @@ -172,17 +169,17 @@ export class RadioOption { componentDidRender(): void { if (this.additionalFieldDisplay === "static") { - const textfield = this.el.querySelector(TEXT_FIELD_SELECTOR); + const additionalField = this.getAdditionalField(); if (!this.selected || this.disabled) { - textfield?.setAttribute("disabled", ""); + additionalField?.setAttribute("disabled", ""); } else { - textfield?.removeAttribute("disabled"); + additionalField?.removeAttribute("disabled"); } } } @Listen("icChange") - textfieldValueHandler(event: CustomEvent<{ value: string }>): void { + additionalFieldValueHandler(event: CustomEvent<{ value: string }>): void { if (this.selected) { this.value = event.detail.value || this.defaultRadioValue; this.icCheck.emit({ @@ -212,17 +209,28 @@ export class RadioOption { this.radioElement.tabIndex = value; } - private handleClick = () => { - if (!this.disabled) { + private getAdditionalField(): HTMLIcTextFieldElement { + const additionalField = this.el.querySelector( + '[slot="additional-field"]' + ) as HTMLIcTextFieldElement; + if (additionalField) additionalField.hiddenInput = false; + return additionalField; + } + + private handleClick = (event: MouseEvent) => { + const clickedAdditionalField = (event.target as Element).matches( + ".dynamic-container, .dynamic-container *" + ); + + if (!this.disabled && !clickedAdditionalField) { + event.stopPropagation(); if (this.skipFocus === false) { this.radioElement.focus(); } this.skipFocus = false; if (this.hasAdditionalField) { - this.value = - this.el.querySelector(TEXT_FIELD_SELECTOR).value || - this.defaultRadioValue; + this.value = this.getAdditionalField().value || this.defaultRadioValue; } this.icCheck.emit({ @@ -235,8 +243,15 @@ export class RadioOption { } }; - private swallowClick = (event: MouseEvent) => { - event.stopPropagation(); + private handleKeyDown = (event: KeyboardEvent) => { + const preventPropagationElements = ["IC-DATE-INPUT", "IC-DATE-PICKER"]; + + if ( + this.getAdditionalField() == document.activeElement && + preventPropagationElements.includes(this.getAdditionalField().nodeName) + ) { + event.stopPropagation(); + } }; private handleFormReset = (): void => { @@ -257,19 +272,23 @@ export class RadioOption { formtarget, groupLabel, handleClick, + handleKeyDown, hasAdditionalField, label, name, selected, - swallowClick, value, } = this; const id = `ic-radio-option-${isPropDefined(label) || value}-${groupLabel}`; return ( - -
+ +