From 56014cf64c387bd4492270905327c570f6814a6b Mon Sep 17 00:00:00 2001 From: Liam DeBeasi Date: Thu, 14 Mar 2024 11:14:21 -0400 Subject: [PATCH] fix(range, select): prefer labels passed by developer (#29145) --- .github/COMPONENT-GUIDE.md | 124 +++++++----------- BREAKING.md | 4 +- core/src/components/range/range.tsx | 52 ++------ .../components/range/test/label/range.spec.ts | 30 +++++ core/src/components/select/select.tsx | 9 +- .../components/select/test/select.spec.tsx | 28 ++++ core/src/utils/helpers.ts | 58 -------- core/src/utils/test/aria.spec.ts | 98 -------------- 8 files changed, 122 insertions(+), 281 deletions(-) delete mode 100644 core/src/utils/test/aria.spec.ts diff --git a/.github/COMPONENT-GUIDE.md b/.github/COMPONENT-GUIDE.md index 15c74b82e4..28206dc9ab 100644 --- a/.github/COMPONENT-GUIDE.md +++ b/.github/COMPONENT-GUIDE.md @@ -439,53 +439,38 @@ render() { #### Labels -A helper function has been created to get the proper `aria-label` for the checkbox. This can be imported as `getAriaLabel` like the following: +Labels should be passed directly to the component in the form of either visible text or an `aria-label`. The visible text can be set inside of a `label` element, and the `aria-label` can be set directly on the interactive element. -```tsx -const { label, labelId, labelText } = getAriaLabel(el, inputId); -``` +In the following example the `aria-label` can be inherited from the Host using the `inheritAttributes` or `inheritAriaAttributes` utilities. This allows developers to set `aria-label` on the host element since they do not have access to inside the shadow root. -where `el` and `inputId` are the following: +> [!NOTE] +> Use `inheritAttributes` to specify which attributes should be inherited or `inheritAriaAttributes` to inherit all of the possible `aria` attributes. ```tsx -export class Checkbox implements ComponentInterface { - private inputId = `ion-cb-${checkboxIds++}`; +import { Prop } from '@stencil/core'; +import { inheritAttributes } from '@utils/helpers'; +import type { Attributes } from '@utils/helpers'; - @Element() el!: HTMLElement; - - ... -} -``` +... -This can then be added to the `Host` like the following: +private inheritedAttributes: Attributes = {}; -```tsx - -``` +@Prop() labelText?: string; -In addition to that, the checkbox input should have a label added: +componentWillLoad() { + this.inheritedAttributes = inheritAttributes(this.el, ['aria-label']); +} -```tsx - - - +render() { + return ( + + + + ) +} ``` #### Hidden Input @@ -567,57 +552,40 @@ render() { #### Labels -A helper function has been created to get the proper `aria-label` for the switch. This can be imported as `getAriaLabel` like the following: +Labels should be passed directly to the component in the form of either visible text or an `aria-label`. The visible text can be set inside of a `label` element, and the `aria-label` can be set directly on the interactive element. -```tsx -const { label, labelId, labelText } = getAriaLabel(el, inputId); -``` +In the following example the `aria-label` can be inherited from the Host using the `inheritAttributes` or `inheritAriaAttributes` utilities. This allows developers to set `aria-label` on the host element since they do not have access to inside the shadow root. -where `el` and `inputId` are the following: +> [!NOTE] +> Use `inheritAttributes` to specify which attributes should be inherited or `inheritAriaAttributes` to inherit all of the possible `aria` attributes. ```tsx -export class Toggle implements ComponentInterface { - private inputId = `ion-tg-${toggleIds++}`; - - @Element() el!: HTMLElement; +import { Prop } from '@stencil/core'; +import { inheritAttributes } from '@utils/helpers'; +import type { Attributes } from '@utils/helpers'; - ... -} -``` +... -This can then be added to the `Host` like the following: +private inheritedAttributes: Attributes = {}; -```tsx - -``` +@Prop() labelText?: string; -In addition to that, the checkbox input should have a label added: +componentWillLoad() { + this.inheritedAttributes = inheritAttributes(this.el, ['aria-label']); +} -```tsx - - - +render() { + return ( + + + + ) +} ``` - #### Hidden Input A helper function to render a hidden input has been added, it can be added in the `render`: diff --git a/BREAKING.md b/BREAKING.md index 5291db672f..27696cf2cb 100644 --- a/BREAKING.md +++ b/BREAKING.md @@ -254,11 +254,11 @@ For more information on styling toast buttons, refer to the [Toast Theming docum

Range

-- The `legacy` property and support for the legacy syntax, which involved placing an `ion-range` inside of an `ion-item` with an `ion-label`, have been removed. For more information on migrating from the legacy range syntax, refer to the [Range documentation](https://ionicframework.com/docs/api/range#migrating-from-legacy-range-syntax). +- The `legacy` property and support for the legacy syntax, which involved placing an `ion-range` inside of an `ion-item` with an `ion-label`, have been removed. Ionic will also no longer attempt to automatically associate form controls with sibling `