Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Highlight search terms in RemoteSelect #2898

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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