Skip to content

Commit 4e533c9

Browse files
committed
fix: Use text instead of name for element select data options
Select2's default matcher filters against the text property of data items. Commit f66a7b1 renamed text to name when adding element hints, which broke the search filtering in the element select dropdown. Renaming name back to text restores Select2's built-in matching so typing in the search field correctly filters and highlights elements. (cherry picked from commit f0fffb4)
1 parent 7a9f01f commit 4e533c9

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

app/components/alchemy/admin/element_select.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def elements_options
3030

3131
elements.sort_by(&:name).map do |element|
3232
{
33-
name: Element.display_name_for(element.name),
33+
text: Element.display_name_for(element.name),
3434
hint: element.hint,
3535
icon: element.icon_file,
3636
id: element.name

app/javascript/alchemy_admin/components/element_select.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@ import { hightlightTerm } from "alchemy_admin/components/remote_select"
22

33
const formatSelection = (option) => {
44
return `
5-
<div class="element-select-name">${option.icon} ${option.name}</div>
5+
<div class="element-select-name">
6+
${option.icon}<span>${option.text}</span>
7+
</div>
68
`
79
}
810

9-
const formatItem = (icon, name, hint) => {
11+
const formatItem = (icon, text, hint) => {
1012
const description = hint
1113
? `<div class="element-select-description">${hint}</div>`
1214
: ""
1315
return `
1416
<div class="element-select-item">
15-
${formatSelection({ icon, name })}
17+
${formatSelection({ icon, text })}
1618
${description}
1719
</div>
1820
`
@@ -34,11 +36,11 @@ class ElementSelect extends HTMLElement {
3436
formatResult: (option, _el, search) => {
3537
let text
3638

37-
if (option.id === "") return option.name
39+
if (option.id === "") return option.text
3840
if (search.term !== "") {
39-
text = hightlightTerm(option.name, search.term)
41+
text = hightlightTerm(option.text, search.term)
4042
} else {
41-
text = option.name
43+
text = option.text
4244
}
4345

4446
return formatItem(option.icon, text, option.hint)

spec/components/alchemy/admin/element_select_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
options = JSON.parse(component["options"])
3737
expect(options).to match_array([
3838
{
39-
"name" => "Headline",
39+
"text" => "Headline",
4040
"icon" => an_instance_of(String),
4141
"hint" => "Use this for headlines.",
4242
"id" => "headline"

0 commit comments

Comments
 (0)