-
Notifications
You must be signed in to change notification settings - Fork 13.5k
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
feat(checkbox): add helperText and errorText properties #30140
base: feature-8.5
Are you sure you want to change the base?
Changes from all commits
2aaf9e4
315642c
7e86b42
b3aeac1
e4531ea
804ebbd
a09d2da
b67d888
e72fe10
bfc9617
0f03052
4794ed7
444e213
54da960
662d1ff
bfc8cf7
d61c38d
45141d5
e08d608
4ece469
e32256f
39e6d9f
7996747
5f86dd9
d771da6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -148,46 +148,53 @@ input { | |||||||
opacity: 0; | ||||||||
} | ||||||||
|
||||||||
// Justify Content | ||||||||
// --------------------------------------------- | ||||||||
// Checkbox Bottom Content | ||||||||
// ---------------------------------------------------------------- | ||||||||
|
||||||||
.checkbox-bottom { | ||||||||
@include padding(4px, null, null, null); | ||||||||
|
||||||||
display: flex; | ||||||||
|
||||||||
:host(.checkbox-justify-space-between) .checkbox-wrapper { | ||||||||
justify-content: space-between; | ||||||||
} | ||||||||
|
||||||||
:host(.checkbox-justify-start) .checkbox-wrapper { | ||||||||
justify-content: start; | ||||||||
font-size: dynamic-font(12px); | ||||||||
|
||||||||
white-space: normal; | ||||||||
} | ||||||||
|
||||||||
:host(.checkbox-justify-end) .checkbox-wrapper { | ||||||||
justify-content: end; | ||||||||
:host(.checkbox-label-placement-stacked) .checkbox-bottom { | ||||||||
font-size: dynamic-font(16px); | ||||||||
} | ||||||||
|
||||||||
// Align Items | ||||||||
// --------------------------------------------- | ||||||||
// Checkbox Hint Text | ||||||||
// ---------------------------------------------------------------- | ||||||||
|
||||||||
:host(.checkbox-alignment-start) .checkbox-wrapper { | ||||||||
align-items: start; | ||||||||
} | ||||||||
/** | ||||||||
* Error text should only be shown when .ion-invalid is | ||||||||
* present on the checkbox. Otherwise the helper text should | ||||||||
* be shown. | ||||||||
*/ | ||||||||
.checkbox-bottom .error-text { | ||||||||
display: none; | ||||||||
|
||||||||
:host(.checkbox-alignment-center) .checkbox-wrapper { | ||||||||
align-items: center; | ||||||||
color: ion-color(danger, base); | ||||||||
} | ||||||||
|
||||||||
// Justify Content & Align Items | ||||||||
// --------------------------------------------- | ||||||||
.checkbox-bottom .helper-text { | ||||||||
display: block; | ||||||||
|
||||||||
// The checkbox should be displayed as block when either justify | ||||||||
// or alignment is set; otherwise, these properties will have no | ||||||||
// visible effect. | ||||||||
:host(.checkbox-justify-space-between), | ||||||||
:host(.checkbox-justify-start), | ||||||||
:host(.checkbox-justify-end), | ||||||||
:host(.checkbox-alignment-start), | ||||||||
:host(.checkbox-alignment-center) { | ||||||||
color: $text-color-step-300; | ||||||||
} | ||||||||
|
||||||||
:host(.ion-touched.ion-invalid) .checkbox-bottom .error-text { | ||||||||
display: block; | ||||||||
} | ||||||||
|
||||||||
:host(.ion-touched.ion-invalid) .checkbox-bottom .helper-text { | ||||||||
display: none; | ||||||||
} | ||||||||
|
||||||||
// Label Placement - Start | ||||||||
// ---------------------------------------------------------------- | ||||||||
|
||||||||
|
@@ -217,6 +224,8 @@ input { | |||||||
*/ | ||||||||
:host(.checkbox-label-placement-end) .checkbox-wrapper { | ||||||||
flex-direction: row-reverse; | ||||||||
|
||||||||
justify-content: start; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might cause unwanted changes in some use cases, but this matches what is expected in the Figma design. Here is how it looks before and after this change with
Because this changes the default behavior in an item, maybe we want to change this later and just recommend they set the helper.movThere was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @brandyscarney which Figma design are you referring to? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @thetaPC I will send you a link There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After some review, I agree with these changes. It also seems like a fix so it's a win win. |
||||||||
} | ||||||||
|
||||||||
/** | ||||||||
|
@@ -260,6 +269,8 @@ input { | |||||||
*/ | ||||||||
:host(.checkbox-label-placement-stacked) .checkbox-wrapper { | ||||||||
flex-direction: column; | ||||||||
|
||||||||
text-align: center; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This text alignment change is necessary because without it the label is left aligned and when you change between helper/error text it jumps: helper-stacked.movAfter my changes: helper-stacked-fixed.movThis issue does not exist without helper/error text. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This makes sense to me. Everything is centered already so the supporting text should follow that. |
||||||||
} | ||||||||
|
||||||||
:host(.checkbox-label-placement-stacked) .label-text-wrapper { | ||||||||
|
@@ -287,6 +298,46 @@ input { | |||||||
@include transform-origin(center, top); | ||||||||
} | ||||||||
|
||||||||
// Justify Content | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't make changes to any of this code I just moved it lower so it would take priority over the |
||||||||
// --------------------------------------------- | ||||||||
|
||||||||
:host(.checkbox-justify-space-between) .checkbox-wrapper { | ||||||||
justify-content: space-between; | ||||||||
} | ||||||||
|
||||||||
:host(.checkbox-justify-start) .checkbox-wrapper { | ||||||||
justify-content: start; | ||||||||
} | ||||||||
|
||||||||
:host(.checkbox-justify-end) .checkbox-wrapper { | ||||||||
justify-content: end; | ||||||||
} | ||||||||
|
||||||||
// Align Items | ||||||||
// --------------------------------------------- | ||||||||
|
||||||||
:host(.checkbox-alignment-start) .checkbox-wrapper { | ||||||||
align-items: start; | ||||||||
} | ||||||||
|
||||||||
:host(.checkbox-alignment-center) .checkbox-wrapper { | ||||||||
align-items: center; | ||||||||
} | ||||||||
|
||||||||
// Justify Content & Align Items | ||||||||
// --------------------------------------------- | ||||||||
|
||||||||
// The checkbox should be displayed as block when either justify | ||||||||
// or alignment is set; otherwise, these properties will have no | ||||||||
// visible effect. | ||||||||
:host(.checkbox-justify-space-between), | ||||||||
:host(.checkbox-justify-start), | ||||||||
:host(.checkbox-justify-end), | ||||||||
:host(.checkbox-alignment-start), | ||||||||
:host(.checkbox-alignment-center) { | ||||||||
display: block; | ||||||||
} | ||||||||
|
||||||||
// Checked / Indeterminate Checkbox | ||||||||
// --------------------------------------------- | ||||||||
|
||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,9 @@ import type { CheckboxChangeEventDetail } from './checkbox-interface'; | |
* @part container - The container for the checkbox mark. | ||
* @part label - The label text describing the checkbox. | ||
* @part mark - The checkmark used to indicate the checked state. | ||
* @part supporting-text - Supporting text displayed beneath the checkbox label. | ||
* @part helper-text - Supporting text displayed beneath the checkbox label when the checkbox is valid. | ||
* @part error-text - Supporting text displayed beneath the checkbox label when the checkbox is invalid and touched. | ||
*/ | ||
@Component({ | ||
tag: 'ion-checkbox', | ||
|
@@ -28,6 +31,8 @@ import type { CheckboxChangeEventDetail } from './checkbox-interface'; | |
}) | ||
export class Checkbox implements ComponentInterface { | ||
private inputId = `ion-cb-${checkboxIds++}`; | ||
private helperTextId = `${this.inputId}-helper-text`; | ||
private errorTextId = `${this.inputId}-error-text`; | ||
private focusEl?: HTMLElement; | ||
private inheritedAttributes: Attributes = {}; | ||
|
||
|
@@ -60,6 +65,16 @@ export class Checkbox implements ComponentInterface { | |
*/ | ||
@Prop() disabled = false; | ||
|
||
/** | ||
* Text that is placed under the checkbox label and displayed when an error is detected. | ||
*/ | ||
@Prop() errorText?: string; | ||
|
||
/** | ||
* Text that is placed under the checkbox label and displayed when no error is detected. | ||
*/ | ||
@Prop() helperText?: string; | ||
|
||
/** | ||
* The value of the checkbox does not mean if it's checked or not, use the `checked` | ||
* property for that. | ||
|
@@ -167,6 +182,48 @@ export class Checkbox implements ComponentInterface { | |
this.toggleChecked(ev); | ||
}; | ||
|
||
private getHintTextID(): string | undefined { | ||
const { el, helperText, errorText, helperTextId, errorTextId } = this; | ||
|
||
if (el.classList.contains('ion-touched') && el.classList.contains('ion-invalid') && errorText) { | ||
return errorTextId; | ||
} | ||
|
||
if (helperText) { | ||
return helperTextId; | ||
} | ||
|
||
return undefined; | ||
} | ||
|
||
/** | ||
* Responsible for rendering helper text and error text. | ||
* This element should only be rendered if hint text is set. | ||
*/ | ||
private renderHintText() { | ||
const { helperText, errorText, helperTextId, errorTextId } = this; | ||
|
||
/** | ||
* undefined and empty string values should | ||
* be treated as not having helper/error text. | ||
*/ | ||
const hasHintText = !!helperText || !!errorText; | ||
if (!hasHintText) { | ||
return; | ||
} | ||
|
||
return ( | ||
<div class="checkbox-bottom"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The class |
||
<div id={helperTextId} class="helper-text" part="supporting-text helper-text"> | ||
{helperText} | ||
</div> | ||
<div id={errorTextId} class="error-text" part="supporting-text error-text"> | ||
{errorText} | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
render() { | ||
const { | ||
color, | ||
|
@@ -218,6 +275,8 @@ export class Checkbox implements ComponentInterface { | |
onFocus={() => this.onFocus()} | ||
onBlur={() => this.onBlur()} | ||
ref={(focusEl) => (this.focusEl = focusEl)} | ||
aria-describedby={this.getHintTextID()} | ||
aria-invalid={this.getHintTextID() === this.errorTextId} | ||
{...inheritedAttributes} | ||
/> | ||
<div | ||
|
@@ -228,6 +287,7 @@ export class Checkbox implements ComponentInterface { | |
part="label" | ||
> | ||
<slot></slot> | ||
{this.renderHintText()} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The text (helper or error) is being read twice by VoiceOver on Safari, Firefox, and Chrome. After some quick debugging, it seems that this happening because the text is inside of label. Screen.Recording.2025-01-30.at.12.26.40.PM.movThere was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed that the focus with VoiceOver does not highlight correctly. This is happening on the |
||
</div> | ||
<div class="native-wrapper"> | ||
<svg class="checkbox-icon" viewBox="0 0 24 24" part="container"> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added this because without it the text is really small.