Skip to content
This repository was archived by the owner on Jul 30, 2018. It is now read-only.

Commit 8f2641c

Browse files
authored
FormLabelMixin: pass classes and label-specific attributes to the <label> node (#319)
* set classes and formID attribute on the label node in formLabelMixin
1 parent 515e2fc commit 8f2641c

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

src/mixins/createFormLabelMixin.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ export interface FormLabelMixinProperties {
5757
*/
5858
describedBy?: string;
5959

60+
/**
61+
* ID of a form element associated with the form field
62+
*/
63+
formId?: string;
64+
6065
/**
6166
* Indicates the value entered in the form field is invalid
6267
*/
@@ -118,7 +123,7 @@ const labelDefaults = {
118123
/**
119124
* Allowed attributes for a11y
120125
*/
121-
const allowedAttributes = ['checked', 'describedBy', 'disabled', 'invalid', 'maxLength', 'minLength', 'multiple', 'name', 'placeholder', 'readOnly', 'required', 'type', 'value'];
126+
const allowedFormFieldAttributes = ['checked', 'describedBy', 'disabled', 'invalid', 'maxLength', 'minLength', 'multiple', 'name', 'placeholder', 'readOnly', 'required', 'type', 'value'];
122127

123128
function getFormFieldA11yAttributes(instance: FormLabel) {
124129
const { properties, type } = instance;
@@ -130,7 +135,7 @@ function getFormFieldA11yAttributes(instance: FormLabel) {
130135

131136
const nodeAttributes: any = {};
132137

133-
for (const key of allowedAttributes) {
138+
for (const key of allowedFormFieldAttributes) {
134139

135140
if (attributeKeys.indexOf(key) === -1) {
136141
continue;
@@ -168,8 +173,14 @@ const createFormLabelMixin = compose<FormLabelMixin, {}>({})
168173
.aspect({
169174
after: {
170175
render(this: FormLabel, result: DNode): DNode {
176+
const labelNodeAttributes: any = {};
171177
if (isHNode(result)) {
172178
assign(result.properties, getFormFieldA11yAttributes(this));
179+
180+
// move classes to label node
181+
const { classes } = result.properties;
182+
const { formId } = this.properties;
183+
assign(labelNodeAttributes, { classes, 'form': formId });
173184
}
174185

175186
if (this.properties.label) {
@@ -194,7 +205,7 @@ const createFormLabelMixin = compose<FormLabelMixin, {}>({})
194205
children.reverse();
195206
}
196207

197-
result = v('label', children);
208+
result = v('label', labelNodeAttributes, children);
198209
}
199210

200211
return result;

tests/unit/mixins/createFormLabelMixin.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as registerSuite from 'intern!object';
22
import * as assert from 'intern/chai!assert';
33
import { VNode } from '@dojo/interfaces/vdom';
4-
import { w } from './../../../src/d';
4+
import { v, w } from './../../../src/d';
55
import createWidgetBase from '../../../src/createWidgetBase';
66
import createFormLabelMixin from '../../../src/mixins/createFormLabelMixin';
77

@@ -74,6 +74,28 @@ registerSuite({
7474
assert.strictEqual(vnode.vnodeSelector, 'div');
7575
assert.isUndefined(vnode.properties!['value']);
7676
assert.isUndefined(vnode.properties!['maxlength']);
77+
},
78+
'label properties'() {
79+
const inputWithClasses = createWidgetBase.override({
80+
render(this: any) {
81+
return v('input', { classes: this.properties.classes });
82+
}
83+
});
84+
const formField = inputWithClasses.mixin(createFormLabelMixin)({
85+
properties: {
86+
label: 'foo',
87+
value: 'bar',
88+
maxLength: 100,
89+
formId: 'baz',
90+
classes: { 'qux': true }
91+
}
92+
});
93+
let vnode = <VNode> formField.__render__();
94+
95+
assert.strictEqual(vnode.children![0].properties!['value'], 'bar');
96+
assert.strictEqual(vnode.children![0].properties!['maxlength'], '100');
97+
assert.strictEqual(vnode.properties!['form'], 'baz');
98+
assert.isTrue(vnode.properties!.classes!['qux'], 'Classes should be set on the label node');
7799
}
78100
},
79101
'type'() {

0 commit comments

Comments
 (0)