Skip to content

add aria accessibility #56

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

Open
wants to merge 1 commit into
base: feature/button-summary
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions projects/angular-ui/src/lib/radio/radio-group.component.html
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
<fieldset
role="radiogroup"
class="bao-radio-button-group"
[id]="id"
[attr.aria-describedby]="ariaDescribedby"
>
<fieldset role="radiogroup" class="bao-radio-button-group" [id]="id">
<ng-content select="bao-label, [bao-label]"></ng-content>
<ng-content select="bao-guiding-text, [bao-guiding-text]"></ng-content>

<ng-content></ng-content>
<div
class="bao-radio-button-group-description"
[attr.id]="ariaDescribedby"
#container
(cdkObserveContent)="onContentChange()"
>
<ng-content
select="bao-error, [bao-error], bao-guiding-text, [bao-guiding-text]"
></ng-content>
<ng-content select="bao-error, [bao-error]"></ng-content>
</div>
</fieldset>
10 changes: 7 additions & 3 deletions projects/angular-ui/src/lib/radio/radio-group.component.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@
@import '../core/grid';

.bao-radio-button-group {
.bao-label {
legend.bao-label {
margin-bottom: 0.5rem;
}
.bao-error,
.bao-guiding-text {

.bao-error{
margin-top: 0.5rem;
}

.bao-guiding-text {
margin-bottom: 0.5rem;
}

.bao-radio-button-label-list {
border-top: 1px solid $neutral-stroke;
padding: 0.5rem 1em 1rem 3.5rem;
Expand Down
98 changes: 85 additions & 13 deletions projects/angular-ui/src/lib/radio/radio-group.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import {
forwardRef,
InjectionToken,
Input,
OnInit,
Output,
QueryList,
ViewChild,
ViewEncapsulation
} from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
Expand Down Expand Up @@ -51,7 +51,7 @@ let radioGroupNextUniqueId = 0;
]
})
export class BaoRadioButtonGroupComponent
implements AfterContentInit, ControlValueAccessor, AfterViewInit
implements AfterContentInit, ControlValueAccessor, AfterViewInit, OnInit
{
@ContentChildren(forwardRef(() => BaoRadioButtonComponent), {
descendants: true
Expand All @@ -71,6 +71,23 @@ export class BaoRadioButtonGroupComponent
*/
@Input() public id: string = this._uniqueId;

/**
* The aria-describebdy-text id for web accessibility
* only when we have de guidance text
*/
public ariaDescribedbyGuidingText?: string;

/**
* The aria-labelledby id for web accessibility
*/
public ariaLabelledby?: string;

/**
* The aria-describebdy-error id for web accessibility
* only when error section appears
*/
public ariaDescribedbyError?: string;

/**
* Define the name property of all radio buttons. Default : null
*/
Expand Down Expand Up @@ -146,17 +163,25 @@ export class BaoRadioButtonGroupComponent
*/
public ariaDescribedby: string | null = null;

@ViewChild('container', { static: false })
private staticContainer: ElementRef;
constructor(private cdr: ChangeDetectorRef, private elementRef: ElementRef) {}

ngOnInit(): void {
this.ariaDescribedbyError = `${this.id}-ariadescribedby-error`;
this.ariaDescribedbyGuidingText = `${this.id}-ariadescribedby-guiding-text`;
this.ariaLabelledby = `${this.id}-arialabelledby`;
}

constructor(private cdr: ChangeDetectorRef) {}
get nativeElement(): HTMLElement {
return this.elementRef.nativeElement;
}

public ngAfterContentInit() {
this._isInitialized = true;
}

public ngAfterViewInit() {
this.setAriaDescribedByToDescription();
this.setAriaDescribedLgendsGuidingText();
this.cdr.detectChanges();
}

Expand Down Expand Up @@ -264,20 +289,67 @@ export class BaoRadioButtonGroupComponent
public onModelChange: (value: any) => void = () => undefined;

/**
* Set the aria-describedby property to bao-guiding-text if available
* Set the aria-describedby property to bao-errors if available
*/
private setAriaDescribedByToDescription() {
const children = Array.from(this.staticContainer.nativeElement.children);
if (children.length === 0) {
this.showAriaDescribedBy(false);
return;
const fieldSet = this.elementNode('FIELDSET');

if (fieldSet) {
const baoError = this.elementNode('DIV', fieldSet);
this.setAriaAttribute(
baoError,
this.ariaDescribedbyError,
fieldSet,
'aria-describedby'
);
}
}

/**
* Set the aria-describedby property to bao-guiding-text and legend if available
*/
private setAriaDescribedLgendsGuidingText() {
const fieldSet = this.elementNode('FIELDSET');

if (fieldSet) {
const baoLabel = this.elementNode('LEGEND', fieldSet);
const baoGuidingText = this.elementNode('BAO-GUIDING-TEXT', fieldSet);

this.setAriaAttribute(
baoLabel,
this.ariaLabelledby,
fieldSet,
'aria-labelledby'
);
this.setAriaAttribute(
baoGuidingText,
this.ariaDescribedbyGuidingText,
fieldSet,
'aria-describedby'
);
}
}

this.showAriaDescribedBy(true);
private setAriaAttribute(
nodeElement: Node,
id: string,
ariaElment: Node,
ariaType: string
): void {
if (nodeElement) {
(nodeElement as HTMLElement).setAttribute('id', id);
(ariaElment as HTMLElement).setAttribute(ariaType, id);
}
}

private showAriaDescribedBy(value: boolean) {
this.ariaDescribedby = value ? `${this.id}-ariadescribedby` : null;
private elementNode(name: string, nativeElt?: Node): Node {
const childNodes = nativeElt
? Array.from(nativeElt.childNodes)
: Array.from(this.nativeElement.childNodes);
const element = childNodes.find(x => x.nodeName === name);
console.log(name);
console.log(childNodes);
return element;
}

private onTouch: () => any = () => undefined;
Expand Down
4 changes: 2 additions & 2 deletions projects/angular-ui/src/lib/radio/radio.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ let radioNextUniqueId = 0;
'[class.bao-radio-button-disabled]': 'disabled',
'[class.bao-radio-button-card]': 'brandBorder',
'[class.bao-radio-button-hidden-label]': 'hiddenLabel',
'[class.bao-radio-button-label-list]': 'horizontalBorder',
'[class.bao-radio-button-label-list]': 'horizontalStyle',
'[class.bao-displaymode-compact]': 'displayMode === "compact"'
}
})
Expand Down Expand Up @@ -86,7 +86,7 @@ export class BaoRadioButtonComponent
/**
* horizontal border
*/
@Input() public horizontalBorder = false;
@Input() public horizontalStyle = false;

/**
* custom display mode compact, responsive
Expand Down
8 changes: 4 additions & 4 deletions projects/storybook-angular/src/stories/Radio/Radio.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export const RadioWithDescAndActionButton: Story = args => ({
props: args,
template: `
<bao-radio-button-group id="RadioWithDescAndHiddenLabel" name="RadioWithDescAndHiddenLabel" >
<bao-radio-button id="ID200" name="name200" value="example1" horizontalBorder="true" >
<bao-radio-button id="ID200" name="name200" value="example1" horizontalStyle="true" >
<bao-label>Radio button avec action button</bao-label>
<bao-radio-action-button>
<button
Expand Down Expand Up @@ -185,7 +185,7 @@ export const RadioWithDescAndActionButton: Story = args => ({
</bao-radio-action-button>
<bao-radio-button-description>Est est et dolores dolore sed justo ipsum et sit.</bao-radio-button-description>
</bao-radio-button>
<bao-radio-button id="ID201" name="name200" value="example1" horizontalBorder="true" >
<bao-radio-button id="ID201" name="name200" value="example1" horizontalStyle="true" >
<bao-label >Radio button avec action button</bao-label>
<bao-radio-action-button>
<button
Expand Down Expand Up @@ -226,7 +226,7 @@ export const RadioWithDescAndActionButtonCompact: Story = args => ({
props: args,
template: `
<bao-radio-button-group id="RadioWithDescAndHiddenLabel" name="RadioWithDescAndHiddenLabel" >
<bao-radio-button id="ID203" name="name203" value="example1" horizontalBorder="true" displayMode="compact" >
<bao-radio-button id="ID203" name="name203" value="example1" horizontalStyle="true" displayMode="compact" >
<bao-label>Radio button avec action button</bao-label>
<bao-radio-action-button>
<button
Expand Down Expand Up @@ -254,7 +254,7 @@ export const RadioWithDescAndActionButtonCompact: Story = args => ({
</bao-radio-action-button>
<bao-radio-button-description>Est est et dolores dolore sed justo ipsum et sit.</bao-radio-button-description>
</bao-radio-button>
<bao-radio-button id="ID204" name="name203" value="example1" horizontalBorder="true" displayMode="compact" >
<bao-radio-button id="ID204" name="name203" value="example1" horizontalStyle="true" displayMode="compact" >
<bao-label >Radio button avec action button</bao-label>
<bao-radio-action-button>
<button
Expand Down