@@ -15,9 +15,9 @@ import {
15
15
forwardRef ,
16
16
InjectionToken ,
17
17
Input ,
18
+ OnInit ,
18
19
Output ,
19
20
QueryList ,
20
- ViewChild ,
21
21
ViewEncapsulation
22
22
} from '@angular/core' ;
23
23
import { ControlValueAccessor , NG_VALUE_ACCESSOR } from '@angular/forms' ;
@@ -51,7 +51,7 @@ let radioGroupNextUniqueId = 0;
51
51
]
52
52
} )
53
53
export class BaoRadioButtonGroupComponent
54
- implements AfterContentInit , ControlValueAccessor , AfterViewInit
54
+ implements AfterContentInit , ControlValueAccessor , AfterViewInit , OnInit
55
55
{
56
56
@ContentChildren ( forwardRef ( ( ) => BaoRadioButtonComponent ) , {
57
57
descendants : true
@@ -71,6 +71,23 @@ export class BaoRadioButtonGroupComponent
71
71
*/
72
72
@Input ( ) public id : string = this . _uniqueId ;
73
73
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
+
74
91
/**
75
92
* Define the name property of all radio buttons. Default : null
76
93
*/
@@ -146,17 +163,25 @@ export class BaoRadioButtonGroupComponent
146
163
*/
147
164
public ariaDescribedby : string | null = null ;
148
165
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
+ }
151
173
152
- constructor ( private cdr : ChangeDetectorRef ) { }
174
+ get nativeElement ( ) : HTMLElement {
175
+ return this . elementRef . nativeElement ;
176
+ }
153
177
154
178
public ngAfterContentInit ( ) {
155
179
this . _isInitialized = true ;
156
180
}
157
181
158
182
public ngAfterViewInit ( ) {
159
183
this . setAriaDescribedByToDescription ( ) ;
184
+ this . setAriaDescribedLgendsGuidingText ( ) ;
160
185
this . cdr . detectChanges ( ) ;
161
186
}
162
187
@@ -264,20 +289,67 @@ export class BaoRadioButtonGroupComponent
264
289
public onModelChange : ( value : any ) => void = ( ) => undefined ;
265
290
266
291
/**
267
- * Set the aria-describedby property to bao-guiding-text if available
292
+ * Set the aria-describedby property to bao-errors if available
268
293
*/
269
294
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
+ ) ;
274
330
}
331
+ }
275
332
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
+ }
277
343
}
278
344
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 ;
281
353
}
282
354
283
355
private onTouch : ( ) => any = ( ) => undefined ;
0 commit comments