Skip to content

Commit

Permalink
Highlight search terms in RemoteSelect
Browse files Browse the repository at this point in the history
Makes it more obvious why a entry is in the list and complies
to the default highlighting that select2 does if there is no
html template rendering function.
  • Loading branch information
tvdeyen committed May 28, 2024
1 parent 98293af commit 01bd3e9
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 7 deletions.
5 changes: 3 additions & 2 deletions app/javascript/alchemy_admin/components/attachment_select.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ class AttachmentSelect extends RemoteSelect {
/**
* html template for each list entry
* @param {object} page
* @param {string} term
* @returns {string}
* @private
*/
_renderListEntry(attachment) {
_renderListEntry(attachment, term) {
return `
<div class="attachment-select--attachment">
<alchemy-icon name="${attachment.icon_css_class}"></alchemy-icon>
<span class="attachment-select--attachment-name">${attachment.name}</span>
<span class="attachment-select--attachment-name">${this._hightlightTerm(attachment.name, term)}</span>
</div>
`
}
Expand Down
5 changes: 3 additions & 2 deletions app/javascript/alchemy_admin/components/node_select.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ class NodeSelect extends RemoteSelect {
/**
* html template for each list entry
* @param {object} node
* @param {string} term
* @returns {string}
* @private
*/
_renderListEntry(node) {
_renderListEntry(node, term) {
const ancestors = node.ancestors.map((a) => a.name)
const seperator = `<alchemy-icon name="arrow-right-s"></alchemy-icon>`

Expand All @@ -33,7 +34,7 @@ class NodeSelect extends RemoteSelect {
${ancestors.length > 0 ? ancestors.join(seperator) + seperator : ""}
</span>
<span class="node-select--node-name">
${node.name}
${this._hightlightTerm(node.name, term)}
</span>
</div>
<div class="node-select--node-url">
Expand Down
5 changes: 3 additions & 2 deletions app/javascript/alchemy_admin/components/page_select.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,16 @@ class PageSelect extends RemoteSelect {
/**
* html template for each list entry
* @param {object} page
* @param {string} term
* @returns {string}
* @private
*/
_renderListEntry(page) {
_renderListEntry(page, term) {
return `
<div class="page-select--page">
<div class="page-select--top">
<alchemy-icon name="file-3"></alchemy-icon>
<span class="page-select--page-name">${page.name}</span>
<span class="page-select--page-name">${this._hightlightTerm(page.name, term)}</span>
<span class="page-select--site-name">${page.site.name}</span>
</div>
<div class="page-select--bottom">
Expand Down
15 changes: 14 additions & 1 deletion app/javascript/alchemy_admin/components/remote_select.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ export class RemoteSelect extends AlchemyHTMLElement {
},
ajax: this.ajaxConfig,
formatSelection: (item) => this._renderResult(item),
formatResult: (item) => this._renderListEntry(item)
formatResult: (item, _el, query) =>
this._renderListEntry(item, query.term)
}
}

Expand Down Expand Up @@ -126,6 +127,7 @@ export class RemoteSelect extends AlchemyHTMLElement {
/**
* html template for each list entry
* @param {object} item
* @param {string} term
* @returns {string}
* @private
*/
Expand All @@ -134,4 +136,15 @@ export class RemoteSelect extends AlchemyHTMLElement {
"You need to define a _renderListEntry function on your sub class!"
)
}

/**
* hightlighted search term
* @param {string} name
* @param {string} term
* @returns {string}
* @private
*/
_hightlightTerm(name, term) {
return name.replace(new RegExp(term, "gi"), (match) => `<em>${match}</em>`)
}
}
1 change: 1 addition & 0 deletions vendor/assets/stylesheets/alchemy_admin/select2.scss
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ html[dir="rtl"] .select2-results {
.select2-results li em {
background: #feffde;
font-style: normal;
text-decoration: underline;
}

.select2-results .select2-highlighted em {
Expand Down

0 comments on commit 01bd3e9

Please sign in to comment.