Skip to content

Commit

Permalink
feat: allow labels to be hidden but accessible by screen readers (#2920)
Browse files Browse the repository at this point in the history
Signed-off-by: Akshat Patel <[email protected]>
  • Loading branch information
Akshat55 authored Jun 13, 2024
1 parent 9135eeb commit 9b00fdc
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/combobox/combobox.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ import { Observable } from "rxjs";
[for]="id"
[id]="labelId"
class="cds--label"
[ngClass]="{'cds--label--disabled': disabled}">
[ngClass]="{
'cds--label--disabled': disabled,
'cds--visually-hidden': hideLabel
}">
<ng-container *ngIf="!isTemplate(label)">{{label}}</ng-container>
<ng-template *ngIf="isTemplate(label)" [ngTemplateOutlet]="label"></ng-template>
</label>
Expand Down Expand Up @@ -303,6 +306,10 @@ export class ComboBox implements OnChanges, AfterViewInit, AfterContentInit, OnD
* Label for the combobox.
*/
@Input() label: string | TemplateRef<any>;
/**
* Hide label while keeping it accessible for screen readers
*/
@Input() hideLabel = false;
/**
* Sets the optional helper text.
*/
Expand Down
3 changes: 3 additions & 0 deletions src/combobox/combobox.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default {
],
args: {
label: "Label",
hideLabel: false,
helperText: "Optional helper text",
disabled: false,
invalid: false,
Expand Down Expand Up @@ -111,6 +112,7 @@ const Template = (args) => ({
[warn]="warn"
[warnText]="warnText"
[label]="label"
[hideLabel]="hideLabel"
[helperText]="helperText"
[items]="items"
[theme]="theme"
Expand Down Expand Up @@ -180,6 +182,7 @@ const MultiTemplate = (args) => ({
[invalid]="invalid"
[invalidText]="invalidText"
[label]="label"
[hideLabel]="hideLabel"
[warn]="warn"
[disabled]="disabled"
[size]="size"
Expand Down
9 changes: 8 additions & 1 deletion src/dropdown/dropdown.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ import { hasScrollableParents } from "carbon-components-angular/utils";
*ngIf="label && !skeleton"
[for]="id"
class="cds--label"
[ngClass]="{'cds--label--disabled': disabled}">
[ngClass]="{
'cds--label--disabled': disabled,
'cds--visually-hidden': hideLabel
}">
<ng-container *ngIf="!isTemplate(label)">{{label}}</ng-container>
<ng-template *ngIf="isTemplate(label)" [ngTemplateOutlet]="label"></ng-template>
</label>
Expand Down Expand Up @@ -183,6 +186,10 @@ export class Dropdown implements OnInit, AfterContentInit, AfterViewInit, OnDest
* Label for the dropdown.
*/
@Input() label: string | TemplateRef<any>;
/**
* Hide label while keeping it accessible for screen readers
*/
@Input() hideLabel = false;
/**
* Sets the optional helper text.
*/
Expand Down
6 changes: 6 additions & 0 deletions src/dropdown/dropdown.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default {
],
args: {
label: "Label",
hideLabel: false,
helperText: "Optional helper text",
disabled: false,
invalid: false,
Expand Down Expand Up @@ -88,6 +89,7 @@ const Template = (args) => ({
template: `
<cds-dropdown
[label]="label"
[hideLabel]="hideLabel"
[skeleton]="skeleton"
[helperText]="helperText"
[size]="size"
Expand Down Expand Up @@ -115,6 +117,7 @@ const MultiTemplate = (args) => ({
[selectionFeedback]="selectionFeedback"
[(ngModel)]="model"
[label]="label"
[hideLabel]="hideLabel"
[helperText]="helperText"
[size]="size"
[dropUp]="dropUp"
Expand Down Expand Up @@ -144,6 +147,7 @@ const ReactiveTemplate = (args) => ({
-->
<app-reactive-forms
[label]="label"
[hideLabel]="hideLabel"
[helperText]="helperText"
[invalid]="invalid"
[invalidText]="invalidText"
Expand All @@ -161,6 +165,8 @@ const NgTemplate = (args) => ({
props: args,
template: `
<cds-dropdown
[label]="label"
[hideLabel]="hideLabel"
[theme]="theme"
placeholder="Select"
[displayValue]="dropdownRenderer"
Expand Down
2 changes: 2 additions & 0 deletions src/dropdown/stories/app-reactive-forms.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
<div style="width: 300px">
<cds-dropdown
[label]="label"
[hideLabel]="hideLabel"
[helperText]="helperText"
[invalid]="invalid"
[invalidText]="invalidText"
Expand All @@ -41,6 +42,7 @@ export class ReactiveFormsStory implements OnInit {

@Input() items = [];
@Input() label = "";
@Input() hideLabel = false;
@Input() helperText = "";
@Input() invalid = false;
@Input() invalidText = "";
Expand Down
2 changes: 1 addition & 1 deletion src/input/password.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { Tooltip } from "../tooltip";
})
class TestPasswordInputComponent {}

fdescribe("Password", () => {
describe("Password", () => {
let fixture: ComponentFixture<TestPasswordInputComponent>;
let component: TestPasswordInputComponent;
let passwordEl: DebugElement;
Expand Down

0 comments on commit 9b00fdc

Please sign in to comment.