-
Notifications
You must be signed in to change notification settings - Fork 13.5k
feat(checkbox): add helperText and errorText properties #30140
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
Changes from 29 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
38ac4f9
bac7383
d8a1502
6b83b14
8dd4e91
14dd28c
45aa36d
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); | ||
brandyscarney marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
white-space: normal; | ||
} | ||
|
||
:host(.checkbox-justify-end) .checkbox-wrapper { | ||
justify-content: end; | ||
:host(.checkbox-label-placement-stacked) .checkbox-bottom { | ||
font-size: dynamic-font(16px); | ||
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 added this because without it the text is really small. 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. Same as above* :) |
||
} | ||
|
||
// 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; | ||
brandyscarney marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
|
@@ -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. | ||
|
@@ -174,6 +189,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) { | ||
BenOsodrac marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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, | ||
|
@@ -199,6 +256,8 @@ export class Checkbox implements ComponentInterface { | |
return ( | ||
<Host | ||
aria-checked={indeterminate ? 'mixed' : `${checked}`} | ||
aria-describedby={this.getHintTextID()} | ||
aria-invalid={this.getHintTextID() === this.errorTextId} | ||
class={createColorClasses(color, { | ||
[mode]: true, | ||
'in-item': hostContext('ion-item', el), | ||
|
@@ -237,6 +296,7 @@ export class Checkbox implements ComponentInterface { | |
part="label" | ||
> | ||
<slot></slot> | ||
{this.renderHintText()} | ||
brandyscarney marked this conversation as resolved.
Show resolved
Hide resolved
brandyscarney marked this conversation as resolved.
Show resolved
Hide resolved
|
||
</div> | ||
<div class="native-wrapper"> | ||
<svg class="checkbox-icon" viewBox="0 0 24 24" part="container"> | ||
|
Uh oh!
There was an error while loading. Please reload this page.