@@ -15,9 +15,9 @@ import {
1515 forwardRef ,
1616 InjectionToken ,
1717 Input ,
18+ OnInit ,
1819 Output ,
1920 QueryList ,
20- ViewChild ,
2121 ViewEncapsulation
2222} from '@angular/core' ;
2323import { ControlValueAccessor , NG_VALUE_ACCESSOR } from '@angular/forms' ;
@@ -51,7 +51,7 @@ let radioGroupNextUniqueId = 0;
5151 ]
5252} )
5353export class BaoRadioButtonGroupComponent
54- implements AfterContentInit , ControlValueAccessor , AfterViewInit
54+ implements AfterContentInit , ControlValueAccessor , AfterViewInit , OnInit
5555{
5656 @ContentChildren ( forwardRef ( ( ) => BaoRadioButtonComponent ) , {
5757 descendants : true
@@ -71,6 +71,23 @@ export class BaoRadioButtonGroupComponent
7171 */
7272 @Input ( ) public id : string = this . _uniqueId ;
7373
74+ /**
75+ * The aria-describebdy-text id for web accessibility
76+ * only when we have de guidance text
77+ */
78+ public ariaDescribedbyGuidingText ?: string ;
79+
80+ /**
81+ * The aria-labelledby id for web accessibility
82+ */
83+ public ariaLabelledby ?: string ;
84+
85+ /**
86+ * The aria-describebdy-error id for web accessibility
87+ * only when error section appears
88+ */
89+ public ariaDescribedbyError ?: string ;
90+
7491 /**
7592 * Define the name property of all radio buttons. Default : null
7693 */
@@ -146,17 +163,25 @@ export class BaoRadioButtonGroupComponent
146163 */
147164 public ariaDescribedby : string | null = null ;
148165
149- @ViewChild ( 'container' , { static : false } )
150- private staticContainer : ElementRef ;
166+ constructor ( private cdr : ChangeDetectorRef , private elementRef : ElementRef ) { }
167+
168+ ngOnInit ( ) : void {
169+ this . ariaDescribedbyError = `${ this . id } -ariadescribedby-error` ;
170+ this . ariaDescribedbyGuidingText = `${ this . id } -ariadescribedby-guiding-text` ;
171+ this . ariaLabelledby = `${ this . id } -arialabelledby` ;
172+ }
151173
152- constructor ( private cdr : ChangeDetectorRef ) { }
174+ get nativeElement ( ) : HTMLElement {
175+ return this . elementRef . nativeElement ;
176+ }
153177
154178 public ngAfterContentInit ( ) {
155179 this . _isInitialized = true ;
156180 }
157181
158182 public ngAfterViewInit ( ) {
159183 this . setAriaDescribedByToDescription ( ) ;
184+ this . setAriaDescribedLgendsGuidingText ( ) ;
160185 this . cdr . detectChanges ( ) ;
161186 }
162187
@@ -264,20 +289,67 @@ export class BaoRadioButtonGroupComponent
264289 public onModelChange : ( value : any ) => void = ( ) => undefined ;
265290
266291 /**
267- * Set the aria-describedby property to bao-guiding-text if available
292+ * Set the aria-describedby property to bao-errors if available
268293 */
269294 private setAriaDescribedByToDescription ( ) {
270- const children = Array . from ( this . staticContainer . nativeElement . children ) ;
271- if ( children . length === 0 ) {
272- this . showAriaDescribedBy ( false ) ;
273- return ;
295+ const fieldSet = this . elementNode ( 'FIELDSET' ) ;
296+
297+ if ( fieldSet ) {
298+ const baoError = this . elementNode ( 'DIV' , fieldSet ) ;
299+ this . setAriaAttribute (
300+ baoError ,
301+ this . ariaDescribedbyError ,
302+ fieldSet ,
303+ 'aria-describedby'
304+ ) ;
305+ }
306+ }
307+
308+ /**
309+ * Set the aria-describedby property to bao-guiding-text and legend if available
310+ */
311+ private setAriaDescribedLgendsGuidingText ( ) {
312+ const fieldSet = this . elementNode ( 'FIELDSET' ) ;
313+
314+ if ( fieldSet ) {
315+ const baoLabel = this . elementNode ( 'LEGEND' , fieldSet ) ;
316+ const baoGuidingText = this . elementNode ( 'BAO-GUIDING-TEXT' , fieldSet ) ;
317+
318+ this . setAriaAttribute (
319+ baoLabel ,
320+ this . ariaLabelledby ,
321+ fieldSet ,
322+ 'aria-labelledby'
323+ ) ;
324+ this . setAriaAttribute (
325+ baoGuidingText ,
326+ this . ariaDescribedbyGuidingText ,
327+ fieldSet ,
328+ 'aria-describedby'
329+ ) ;
274330 }
331+ }
275332
276- this . showAriaDescribedBy ( true ) ;
333+ private setAriaAttribute (
334+ nodeElement : Node ,
335+ id : string ,
336+ ariaElment : Node ,
337+ ariaType : string
338+ ) : void {
339+ if ( nodeElement ) {
340+ ( nodeElement as HTMLElement ) . setAttribute ( 'id' , id ) ;
341+ ( ariaElment as HTMLElement ) . setAttribute ( ariaType , id ) ;
342+ }
277343 }
278344
279- private showAriaDescribedBy ( value : boolean ) {
280- this . ariaDescribedby = value ? `${ this . id } -ariadescribedby` : null ;
345+ private elementNode ( name : string , nativeElt ?: Node ) : Node {
346+ const childNodes = nativeElt
347+ ? Array . from ( nativeElt . childNodes )
348+ : Array . from ( this . nativeElement . childNodes ) ;
349+ const element = childNodes . find ( x => x . nodeName === name ) ;
350+ console . log ( name ) ;
351+ console . log ( childNodes ) ;
352+ return element ;
281353 }
282354
283355 private onTouch : ( ) => any = ( ) => undefined ;
0 commit comments