Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit e571bfe

Browse files
committed
doc(cb-a11y): Labelling form controls: content
1 parent fd362a8 commit e571bfe

8 files changed

+92
-9
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
div.edit-box{
2+
height: auto;
3+
width: 30%;
4+
}

public/docs/_examples/cb-a11y/ts/app/form-controls/a11y-custom-control.component.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
<label [id]="uniqueId">
44
<ng-content></ng-content>
55
</label>
6-
<div role="textbox"
6+
<div #divTextBox
7+
role="textbox"
8+
aria-multiline="false"
79
[attr.aria-labelledby]="uniqueId"
8-
class="form-control"
10+
class="form-control edit-box"
11+
[innerHTML]="outerValue"
12+
(keypress)="onChange($event, divTextBox.innerHTML)"
913
contenteditable></div>
1014
</div>
1115
<!-- #enddocregion -->

public/docs/_examples/cb-a11y/ts/app/form-controls/a11y-custom-control.component.ts

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,59 @@
1-
import {Component, OnInit} from "angular2/core";
1+
import {Component, OnInit, Provider, forwardRef,} from "angular2/core";
22
import {A11yHelper} from "../services/a11y-helper.service";
3+
import {NG_VALUE_ACCESSOR, ControlValueAccessor} from "angular2/common";
4+
5+
const noop = () => {
6+
};
7+
8+
const A11Y_CUSTOM_CONTROL_VALUE_ACCESSOR = new Provider(
9+
NG_VALUE_ACCESSOR, {
10+
useExisting: forwardRef(() => A11yCustomControl),
11+
multi: true
12+
});
313

414
@Component({
515
selector: 'a11y-custom-control',
6-
templateUrl: './app/form-controls/a11y-custom-control.component.html'
16+
templateUrl: './app/form-controls/a11y-custom-control.component.html',
17+
styleUrls: ['./app/form-controls/a11y-custom-control.component.css'],
18+
providers: [A11Y_CUSTOM_CONTROL_VALUE_ACCESSOR]
719
})
8-
export class A11yCustomControl implements OnInit{
20+
export class A11yCustomControl implements OnInit, ControlValueAccessor {
21+
22+
uniqueId:string;
23+
24+
private _innerValue:any = '';
25+
outerValue:string = '';
926

10-
uniqueId: string;
27+
private _onTouchedCallback:() => void = noop;
28+
private _onChangeCallback:(_:any) => void = noop;
1129

12-
constructor(private _a11yHelper: A11yHelper){}
30+
constructor(private _a11yHelper:A11yHelper) {
31+
}
32+
33+
onChange(event:any, value:string) {
34+
if (event.keyCode == 13) {
35+
event.preventDefault();
36+
}
37+
else {
38+
this._innerValue = this._a11yHelper.removeHtmlStringBreaks(value);
39+
this._onChangeCallback(this._innerValue);
40+
}
41+
}
42+
43+
writeValue(value:any) {
44+
if (value != this._innerValue) {
45+
this._innerValue = value;
46+
this.outerValue = value;
47+
}
48+
}
49+
50+
registerOnChange(fn:any) {
51+
this._onChangeCallback = fn;
52+
}
53+
54+
registerOnTouched(fn:any) {
55+
this._onTouchedCallback = fn;
56+
}
1357

1458
ngOnInit():void {
1559
this.uniqueId = this._a11yHelper.generateUniqueIdString();

public/docs/_examples/cb-a11y/ts/app/form-controls/a11y-form-controls.component.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,14 @@ <h3>Labeling custom controls</h3>
139139
<hr>
140140
</header>
141141

142+
<a11y-input-wrapper>
143+
<span class="wrapped-label">Write in this wrapped input:</span>
144+
<input class="form-control" [(ngModel)]="inputWrappedModel">
145+
</a11y-input-wrapper>
146+
147+
{{inputDivModel}}
142148
<!-- #docregion cb-a11y-form-controls-custom-control-usage -->
143-
<a11y-custom-control>Write in this labelled div:</a11y-custom-control>
149+
<a11y-custom-control [(ngModel)]="inputDivModel">Write in this labelled div:</a11y-custom-control>
144150
<!--#enddocregion-->
145151

146152
</section>

public/docs/_examples/cb-a11y/ts/app/form-controls/a11y-form-controls.component.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {CORE_DIRECTIVES, FORM_DIRECTIVES} from "angular2/common";
33
import {A11yHelper} from "../services/a11y-helper.service";
44
import {ROUTER_DIRECTIVES} from "angular2/router";
55
import {A11yCustomControl} from "./a11y-custom-control.component";
6+
import {A11yInputWrapper} from "./a11y-input-wrapper.component";
67

78
@Component({
89
selector: 'a11y-form-controls',
@@ -11,7 +12,8 @@ import {A11yCustomControl} from "./a11y-custom-control.component";
1112
CORE_DIRECTIVES,
1213
FORM_DIRECTIVES,
1314
ROUTER_DIRECTIVES,
14-
A11yCustomControl
15+
A11yCustomControl,
16+
A11yInputWrapper
1517
]
1618
})
1719
export class A11yFormControls implements OnInit {
@@ -22,6 +24,8 @@ export class A11yFormControls implements OnInit {
2224

2325
inputModel:string;
2426
inputExplicitModel: string;
27+
inputWrappedModel: string;
28+
inputDivModel: string = '';
2529
textModel:string;
2630
selectModel: string;
2731
searchModel: string;
@@ -43,6 +47,7 @@ export class A11yFormControls implements OnInit {
4347
console.log(this.inputExplicitModel);
4448
console.log(this.searchModel);
4549
console.log(this.filterModel);
50+
console.log(this.inputWrappedModel);
4651
}
4752

4853
isChecked(item:string):boolean {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<div class="form-group">
2+
<label><ng-content select=".wrapped-label"></ng-content>
3+
<ng-content select="input"></ng-content>
4+
</label>
5+
</div>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import {Component} from "angular2/core";
2+
3+
@Component({
4+
selector: 'a11y-input-wrapper',
5+
templateUrl: './app/form-controls/a11y-input-wrapper.component.html'
6+
})
7+
export class A11yInputWrapper{
8+
9+
}

public/docs/_examples/cb-a11y/ts/app/services/a11y-helper.service.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ export class A11yHelper {
8484
isStringInArray(stringArray: Array<string>, item: string): boolean {
8585
return stringArray.indexOf(item.toString()) != -1;
8686
}
87+
88+
removeHtmlStringBreaks(inputValue: string):string{
89+
return inputValue.replace(new RegExp('<div>', 'g'), '')
90+
.replace(new RegExp('</div>', 'g'), '\n')
91+
.replace(new RegExp('<br>', 'g'), '')
92+
}
8793

8894
private randomGuidSnippet():string {
8995
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);

0 commit comments

Comments
 (0)