Skip to content

Commit 26556ea

Browse files
authored
Merge pull request #199 from JJJ/jjj/option-labels
Options: Show label-only native options
2 parents b7c6e05 + 6632d11 commit 26556ea

28 files changed

Lines changed: 506 additions & 37 deletions

‎AGENTS.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
- Before a feature PR is ready to merge, update the relevant page in `docs/`. Document new options and defaults in `docs/options.html`; add a working example to every applicable jQuery, Prototype, Vanilla, and React demo page when the behavior benefits from trying it. Edit `docs/index.template.html` for shared jQuery/Prototype demo content and run `npm run build` to generate `docs/index.html` and `docs/index.proto.html`; do not edit those generated pages directly.
1515
- Update the matching GitHub wiki page for new options, behavior, or workflows. Add or refresh a screenshot when appearance or interaction is central to the change. Keep wiki examples consistent with the live site.
16+
- For every Harvest issue or pull request investigated, fixed, covered, or declined, update the source triage record and the public [Harvest issue review](https://github.com/JJJ/chosen/wiki/Harvest-Issue-Review) in the same effort. Record the upstream link, disposition, reason, and fork PR when there is one. Refresh the wiki after the PR merges so its status and links describe the shipped result.
1617
- Record user-facing changes in `CHANGELOG.md`. Add a developer note when generated markup, selectors, roles, or attributes change, even for opt-in behavior.
1718
- Check links and examples, and verify that the generated assets served by the site match `dist/`.
1819

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ This project is a continuation and modernization of the original [harvesthq/chos
5454
- Add `open_on_label_click` to let associated labels consistently focus or open both single and multiple controls while preserving the existing defaults when omitted.
5555

5656
### Fixed
57+
- Show and search an option's `label` attribute when it has no text in the jQuery, Prototype, and Vanilla editions. React already accepts the equivalent option `label` field (harvesthq/chosen#3112).
5758
- Match the classic single-select keyboard shortcuts in Vanilla and React: Enter opens a closed list, Up at the first result closes it when a choice is selected, and Backspace or Delete clears an eligible closed selection (harvesthq/chosen#994).
5859
- Clear a previous controlled React selection from the native select's reset defaults when switching to an uncontrolled control.
5960
- Keep demo action-button spacing off selected-choice remove buttons, and align the Vanilla and React chip borders and remove icons with classic Chosen.

‎coffee/lib/select-parser.coffee‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,18 @@ class SelectParser
2929

3030
add_option: (option, group_position, group_disabled) ->
3131
if option.nodeName.toUpperCase() is "OPTION"
32-
if option.text != "" or option.value != ""
32+
option_text = if option.text != "" then option.text else option.getAttribute('label') or ""
33+
if option_text != "" or option.value != ""
3334
if group_position?
3435
@parsed[group_position].children += 1
3536
@parsed.push
3637
options_index: @options_index
3738
option_element: option
3839
value: option.value
39-
text: option.text
40+
text: option_text
4041
search_text: option.getAttribute('data-search-text') || ''
4142
always_visible: option.hasAttribute('data-chosen-always-visible')
42-
html: this.escape_html(option.text).replace(/^\s+|\s+$/g, '')
43+
html: this.escape_html(option_text).replace(/^\s+|\s+$/g, '')
4344
title: option.title if option.title
4445
selected: option.selected
4546
disabled: if group_disabled is true then group_disabled else option.disabled

‎dist/js/chosen.jquery.js‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -494,19 +494,21 @@ var ChosenCore = (function() {
494494
}
495495

496496
add_option(option, group_position, group_disabled) {
497+
var option_text;
497498
if (option.nodeName.toUpperCase() === "OPTION") {
498-
if (option.text !== "" || option.value !== "") {
499+
option_text = option.text !== "" ? option.text : option.getAttribute('label') || "";
500+
if (option_text !== "" || option.value !== "") {
499501
if (group_position != null) {
500502
this.parsed[group_position].children += 1;
501503
}
502504
this.parsed.push({
503505
options_index: this.options_index,
504506
option_element: option,
505507
value: option.value,
506-
text: option.text,
508+
text: option_text,
507509
search_text: option.getAttribute('data-search-text') || '',
508510
always_visible: option.hasAttribute('data-chosen-always-visible'),
509-
html: this.escape_html(option.text).replace(/^\s+|\s+$/g, ''),
511+
html: this.escape_html(option_text).replace(/^\s+|\s+$/g, ''),
510512
title: option.title ? option.title : void 0,
511513
selected: option.selected,
512514
disabled: group_disabled === true ? group_disabled : option.disabled,

‎dist/js/chosen.jquery.min.js‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dist/js/chosen.proto.js‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -484,19 +484,21 @@ var ChosenCore = (function() {
484484
}
485485

486486
add_option(option, group_position, group_disabled) {
487+
var option_text;
487488
if (option.nodeName.toUpperCase() === "OPTION") {
488-
if (option.text !== "" || option.value !== "") {
489+
option_text = option.text !== "" ? option.text : option.getAttribute('label') || "";
490+
if (option_text !== "" || option.value !== "") {
489491
if (group_position != null) {
490492
this.parsed[group_position].children += 1;
491493
}
492494
this.parsed.push({
493495
options_index: this.options_index,
494496
option_element: option,
495497
value: option.value,
496-
text: option.text,
498+
text: option_text,
497499
search_text: option.getAttribute('data-search-text') || '',
498500
always_visible: option.hasAttribute('data-chosen-always-visible'),
499-
html: this.escape_html(option.text).replace(/^\s+|\s+$/g, ''),
501+
html: this.escape_html(option_text).replace(/^\s+|\s+$/g, ''),
500502
title: option.title ? option.title : void 0,
501503
selected: option.selected,
502504
disabled: group_disabled === true ? group_disabled : option.disabled,

‎dist/js/chosen.proto.min.js‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dist/native/chosen.native.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ var ChosenNative = (() => {
398398
className: child.className,
399399
options: Array.from(child.children, (option) => ({
400400
value: option.value,
401-
label: option.text,
401+
label: option.text || option.getAttribute("label") || "",
402402
selected: option.selected,
403403
disabled: option.disabled,
404404
hidden: option.hidden,
@@ -412,7 +412,7 @@ var ChosenNative = (() => {
412412
} else if (child.tagName === "OPTION") {
413413
entries.push({
414414
value: child.value,
415-
label: child.text,
415+
label: child.text || child.getAttribute("label") || "",
416416
selected: child.selected,
417417
disabled: child.disabled,
418418
hidden: child.hidden,

‎dist/native/index.cjs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ function optionTree(select, copyDataAttributes = false) {
398398
className: child.className,
399399
options: Array.from(child.children, (option) => ({
400400
value: option.value,
401-
label: option.text,
401+
label: option.text || option.getAttribute("label") || "",
402402
selected: option.selected,
403403
disabled: option.disabled,
404404
hidden: option.hidden,
@@ -412,7 +412,7 @@ function optionTree(select, copyDataAttributes = false) {
412412
} else if (child.tagName === "OPTION") {
413413
entries.push({
414414
value: child.value,
415-
label: child.text,
415+
label: child.text || child.getAttribute("label") || "",
416416
selected: child.selected,
417417
disabled: child.disabled,
418418
hidden: child.hidden,

‎dist/native/index.mjs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ function optionTree(select, copyDataAttributes = false) {
372372
className: child.className,
373373
options: Array.from(child.children, (option) => ({
374374
value: option.value,
375-
label: option.text,
375+
label: option.text || option.getAttribute("label") || "",
376376
selected: option.selected,
377377
disabled: option.disabled,
378378
hidden: option.hidden,
@@ -386,7 +386,7 @@ function optionTree(select, copyDataAttributes = false) {
386386
} else if (child.tagName === "OPTION") {
387387
entries.push({
388388
value: child.value,
389-
label: child.text,
389+
label: child.text || child.getAttribute("label") || "",
390390
selected: child.selected,
391391
disabled: child.disabled,
392392
hidden: child.hidden,

0 commit comments

Comments
 (0)