-
-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2748 from sascha-karnatz/add-multi-tag-component
Transform the tag selector into a web component
- Loading branch information
Showing
19 changed files
with
181 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module Alchemy | ||
module Admin | ||
class TagsAutocomplete < ViewComponent::Base | ||
delegate :alchemy, to: :helpers | ||
|
||
def initialize(additional_class: nil) | ||
@additional_class = additional_class | ||
end | ||
|
||
def call | ||
content_tag("alchemy-tags-autocomplete", content, attributes) | ||
end | ||
|
||
private | ||
|
||
def attributes | ||
{ | ||
placeholder: Alchemy.t(:search_tag), | ||
url: alchemy.autocomplete_admin_tags_path, | ||
class: @additional_class | ||
} | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
app/javascript/alchemy_admin/components/tags_autocomplete.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
class TagsAutocomplete extends HTMLElement { | ||
connectedCallback() { | ||
this.classList.add("autocomplete_tag_list") | ||
$(this.input).select2(this.select2Config) | ||
} | ||
|
||
get input() { | ||
return this.getElementsByTagName("input")[0] | ||
} | ||
|
||
get select2Config() { | ||
return { | ||
tags: true, | ||
tokenSeparators: [","], | ||
openOnEnter: false, | ||
minimumInputLength: 1, | ||
createSearchChoice: this.#createSearchChoice, | ||
ajax: { | ||
url: this.getAttribute("url"), | ||
dataType: "json", | ||
data: (term) => { | ||
return { term } | ||
}, | ||
results: (data) => { | ||
return { results: data } | ||
} | ||
}, | ||
initSelection: this.#initSelection | ||
} | ||
} | ||
|
||
#createSearchChoice(term, data) { | ||
if ( | ||
$(data).filter(function () { | ||
return this.text.localeCompare(term) === 0 | ||
}).length === 0 | ||
) { | ||
return { | ||
id: term, | ||
text: term | ||
} | ||
} | ||
} | ||
|
||
#initSelection(element, callback) { | ||
const data = [] | ||
$(element.val().split(",")).each(function () { | ||
data.push({ | ||
id: this.trim(), | ||
text: this | ||
}) | ||
}) | ||
callback(data) | ||
} | ||
} | ||
|
||
customElements.define("alchemy-tags-autocomplete", TagsAutocomplete) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
<%= alchemy_form_for [:admin, @page], class: 'edit_page' do |f| %> | ||
<%= f.input :name, autofocus: true %> | ||
<div class="input string"> | ||
<%= f.label :tag_list %> | ||
<%= render 'alchemy/admin/partials/autocomplete_tag_list', f: f %> | ||
</div> | ||
<%= render Alchemy::Admin::TagsAutocomplete.new do %> | ||
<%= f.input :tag_list, input_html: { value: f.object.tag_list.join(",") } %> | ||
<% end %> | ||
<%= f.submit Alchemy.t(:save) %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
app/views/alchemy/admin/partials/_autocomplete_tag_list.html.erb
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe Alchemy::Admin::TagsAutocomplete, type: :component do | ||
before do | ||
render | ||
end | ||
|
||
context "without parameters" do | ||
subject(:render) do | ||
render_inline(described_class.new) { "Page Select Content" } | ||
end | ||
|
||
it "should render the component and render given block content" do | ||
expect(page).to have_selector("alchemy-tags-autocomplete") | ||
expect(page).to have_text("Page Select Content") | ||
end | ||
|
||
it "should have the default placeholder" do | ||
expect(page).to have_selector("alchemy-tags-autocomplete[placeholder='Search tag']") | ||
end | ||
|
||
it "should have the default tags autocomplete - url" do | ||
expect(page).to have_selector("alchemy-tags-autocomplete[url='/admin/tags/autocomplete']") | ||
end | ||
end | ||
|
||
context "with additional class" do | ||
subject(:render) do | ||
render_inline(described_class.new(additional_class: "foooo")) | ||
end | ||
|
||
it "should have these class" do | ||
expect(page).to have_selector("alchemy-tags-autocomplete.foooo") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
spec/javascript/alchemy_admin/components/tags_autocomplete.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { renderComponent } from "./component.helper" | ||
|
||
// import jquery and append it to the window object | ||
import jQuery from "jquery" | ||
globalThis.$ = jQuery | ||
globalThis.jQuery = jQuery | ||
|
||
import "alchemy_admin/components/tags_autocomplete" | ||
import("assets/jquery_plugins/select2") | ||
|
||
describe("alchemy-tags-autocomplete", () => { | ||
/** | ||
* | ||
* @type {HTMLElement | undefined} | ||
*/ | ||
let component = undefined | ||
|
||
beforeEach(() => { | ||
const html = ` | ||
<alchemy-tags-autocomplete> | ||
<input type="text"> | ||
</alchemy-tags-autocomplete> | ||
` | ||
component = renderComponent("alchemy-tags-autocomplete", html) | ||
}) | ||
|
||
it("should render the input field", () => { | ||
expect(component.getElementsByTagName("input")[0]).toBeInstanceOf( | ||
HTMLElement | ||
) | ||
}) | ||
|
||
it("should initialize Select2", () => { | ||
expect( | ||
component.getElementsByClassName("select2-container").length | ||
).toEqual(1) | ||
}) | ||
}) |