Skip to content

Commit 812fd64

Browse files
authored
Fix Hds::Form::Label argument for with boolean values (#2863)
1 parent b8ff128 commit 812fd64

File tree

5 files changed

+58
-1
lines changed

5 files changed

+58
-1
lines changed

.changeset/nine-fans-cough.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@hashicorp/design-system-components": patch
3+
---
4+
5+
`Form::Label` - Forced the `for` HTML attribute to be converted to a string

packages/components/src/components/hds/form/label/index.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Copyright (c) HashiCorp, Inc.
33
SPDX-License-Identifier: MPL-2.0
44
}}
5-
<label class={{this.classNames}} for={{@controlId}} id={{this.id}} ...attributes>
5+
<label class={{this.classNames}} for="{{@controlId}}" id={{this.id}} ...attributes>
66
{{yield}}
77
<Hds::Form::Indicator @isRequired={{@isRequired}} @isOptional={{@isOptional}} />
88
</label>

showcase/app/templates/components/form/radio.hbs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,4 +327,21 @@
327327
</SF.Item>
328328
</Shw::Flex>
329329

330+
<Shw::Text::H3>Special cases</Shw::Text::H3>
331+
332+
<Shw::Flex as |SF|>
333+
<SF.Item as |SFI|>
334+
<SFI.Label>With <code>true/false</code> as boolean values</SFI.Label>
335+
<Hds::Form::Radio::Group @name="control-booleans" as |G|>
336+
{{#let (array true false) as |bools|}}
337+
{{#each bools as |bool|}}
338+
<G.RadioField @id={{bool}} @value={{bool}} as |F|>
339+
<F.Label>{{bool}}</F.Label>
340+
</G.RadioField>
341+
{{/each}}
342+
{{/let}}
343+
</Hds::Form::Radio::Group>
344+
</SF.Item>
345+
</Shw::Flex>
346+
330347
</section>

showcase/tests/integration/components/hds/form/label/index-test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ module('Integration | Component | hds/form/label/index', function (hooks) {
6666
assert.dom('#test-form-label').hasAttribute('for', 'my-control-id');
6767
});
6868

69+
test('it renders a label with the "for" attribute even if the @controlId argument is a boolean', async function (assert) {
70+
await render(
71+
hbs`<Hds::Form::Label @controlId={{true}} id="test-form-label-true">True</Hds::Form::Label>
72+
<Hds::Form::Label @controlId={{false}} id="test-form-label-false">False</Hds::Form::Label>
73+
`
74+
);
75+
assert.dom('#test-form-label-true').hasAttribute('for', 'true');
76+
assert.dom('#test-form-label-false').hasAttribute('for', 'false');
77+
});
78+
6979
// ID
7080

7181
test('it renders a label with the correct "id" attribute if the @controlId argument is provided', async function (assert) {

showcase/tests/integration/components/hds/form/radio/group-test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,31 @@ module('Integration | Component | hds/form/radio/group', function (hooks) {
141141
);
142142
});
143143

144+
test('it automatically provides all the ID relations between the elements when dynamically rendered using boolean values', async function (assert) {
145+
await render(
146+
hbs`<Hds::Form::Radio::Group as |G|>
147+
<G.RadioField @value={{true}} @id={{true}} as |F|>
148+
<F.Label>This is the label for the 'true' value</F.Label>
149+
</G.RadioField>
150+
<G.RadioField @value={{false}} @id={{false}} as |F|>
151+
<F.Label>This is the label for the 'false' value</F.Label>
152+
</G.RadioField>
153+
</Hds::Form::Radio::Group>`
154+
);
155+
156+
const inputs = this.element.querySelectorAll('input[type="radio"]');
157+
const labels = this.element.querySelectorAll('label');
158+
159+
// the `true` value should be used for the `id` (input) and `for` (label) attributes
160+
assert.dom(inputs[0]).hasAttribute('id', 'true');
161+
assert.dom(labels[0]).hasAttribute('for', 'true');
162+
163+
// the `false` value should not be used, but the `id` (input) attribute should be generated and the `for` (label) attribute should match it
164+
const generatedId = inputs[1].id;
165+
assert.true(generatedId.startsWith('ember'));
166+
assert.dom(labels[1]).hasAttribute('for', generatedId);
167+
});
168+
144169
// NAME
145170

146171
test('it renders the defined name on all controls within a group', async function (assert) {

0 commit comments

Comments
 (0)