Skip to content

Commit e1cc6e4

Browse files
authored
Merge pull request #322 from platanus/default-select-tags-support
Default select tags support
2 parents 386d4fa + 15c6db7 commit e1cc6e4

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class ActiveAdmin::Inputs::SelectInput < Formtastic::Inputs::SelectInput
2+
def input_html_options
3+
super.merge(data: { tags: @options[:tags].present? })
4+
end
5+
6+
def raw_collection
7+
field_value = object.send(method)
8+
9+
@options[:tags].present? && field_value.present? ? (super.to_a << field_value).uniq : super
10+
end
11+
end

docs/select2_default.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ Now, if you want to enable [select2](http://ivaynberg.github.io/select2/) for a
2525
```
2626
f.input :created_at, input_html: { class: "select2" }
2727
```
28+
29+
### Options
30+
31+
* `tags`: **(optional)** boolean option, by **default** it's `false`. If `true`, it allows dynamic option creation [as described here](https://select2.org/tagging). It will also add the input's initial value to the select options if it's not in the supplied collection. Note that, unlike the `tags_input`, this does not allow multiple values. Only available for form inputs, not filters.

spec/features/inputs/select2_spec.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,60 @@
2020
expect(page).not_to have_selector("select.default-select")
2121
expect(page).not_to have_selector("select.select2")
2222
end
23+
24+
context "with tags: true option" do
25+
let(:invoice) { create_invoice }
26+
let(:selection) { '#444' }
27+
28+
before do
29+
register_form(Invoice) do |f|
30+
f.input :number, as: :select, collection: ["#111", "#222", "#333"], tags: true
31+
end
32+
end
33+
34+
context 'when entering option not in collection' do
35+
before { visit edit_admin_invoice_path(invoice) }
36+
37+
it "adds new option", js: true do
38+
expect_select2_options_count_to_eq(4)
39+
fill_select2_input(selection)
40+
expect_select2_options_count_to_eq(5)
41+
end
42+
end
43+
44+
context 'when initial value is not in inputs collection' do
45+
before do
46+
invoice.update!(number: selection)
47+
visit edit_admin_invoice_path(invoice)
48+
end
49+
50+
it "includes initial value as option", js: true do
51+
expect_select2_options_count_to_eq(5)
52+
end
53+
end
54+
end
55+
56+
context "with tags: false option" do
57+
let(:selection) { '#444' }
58+
59+
before do
60+
register_form(Invoice) do |f|
61+
f.input :number, as: :select, collection: ["#111", "#222", "#333"], tags: false
62+
end
63+
end
64+
65+
context 'when entering option not in collection' do
66+
let(:selection) { '#444' }
67+
68+
before { visit edit_admin_invoice_path(create_invoice) }
69+
70+
it "doesn't add new option", js: true do
71+
expect_select2_options_count_to_eq(4)
72+
fill_select2_input(selection)
73+
expect_select2_options_count_to_eq(4)
74+
end
75+
end
76+
end
2377
end
2478

2579
context "when default config is select 2 and select control has default-select class" do

0 commit comments

Comments
 (0)