Skip to content

Commit f83f5f0

Browse files
authored
Merge pull request #1289 from Choices-js/option-label-attribute
Support `<option>` label attribute
2 parents bf2cfb9 + 7dc8681 commit f83f5f0

File tree

4 files changed

+10
-4
lines changed

4 files changed

+10
-4
lines changed

public/test/select-multiple/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ <h2>Select multiple inputs</h2>
6767
multiple
6868
>
6969
<option value="Choice 1"><!-- html comment -->Choice 1</option>
70-
<option value="Choice 2">Choice 2</option>
70+
<option value="Choice 2" label="Choice 2" />
7171
<option value="Find me">Choice 3</option>
7272
<option value="Choice 4">Choice 4</option>
7373
</select>

public/test/select-one/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ <h2>Select one inputs</h2>
6262
<button class="enable push-bottom">Enable</button>
6363
<select class="form-control" name="choices-basic" id="choices-basic">
6464
<option value="Choice 1"><!-- html comment -->Choice 1</option>
65-
<option value="Choice 2">Choice 2</option>
65+
<option value="Choice 2" label="Choice 2" />
6666
<option value="Find me">Choice 3</option>
6767
<option value="Choice 4">Choice 4</option>
6868
</select>

src/scripts/components/wrapped-select.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ export default class WrappedSelect extends WrappedElement<HTMLSelectElement> {
8181
score: 0,
8282
rank: 0,
8383
value: option.value,
84-
label: option.innerText, // HTML options do not support most html tags, but innerHtml will extract html comments...
84+
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/option
85+
// This attribute is text for the label indicating the meaning of the option. If the `label` attribute isn't defined, its value is that of the element text content (ie `innerText`).
86+
label: option.label,
8587
element: option,
8688
active: true,
8789
// this returns true if nothing is selected on initial load, which will break placeholder support

test/scripts/components/wrapped-select.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ describe('components/wrappedSelect', () => {
2020
option.innerHTML = 'Placeholder label';
2121
} else {
2222
option.value = `Value ${i}`;
23-
option.innerHTML = `Label ${i}`;
23+
if (i % 2 === 0) {
24+
option.innerHTML = `Label ${i}`;
25+
} else {
26+
option.label = `Label ${i}`;
27+
}
2428
}
2529

2630
if (i === 1) {

0 commit comments

Comments
 (0)