Skip to content

Commit 428f768

Browse files
committed
fix(nested-level-input): include only items that belong to parent
When using collection option
1 parent 90d9de5 commit 428f768

File tree

3 files changed

+38
-21
lines changed

3 files changed

+38
-21
lines changed

app/inputs/nested_level_input.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def load_collection_data
4848

4949
collection_options = collection_to_select_options do |item, option|
5050
if @options[:parent_attribute].present?
51-
option[@options[:parent_id_attribute]] = item.send(@options[:parent_id_attribute])
51+
option[@options[:parent_attribute]] = item.send(@options[:parent_id_attribute])
5252
end
5353
end
5454

app/javascript/activeadmin_addons/inputs/slim-select-nested.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ function collectionSearch(search, args, collection) {
2525
return Promise.reject('Search term too short');
2626
}
2727

28-
const data = JSON.parse(collection).map(
28+
const data = JSON.parse(collection).filter(
29+
(item) => (!args.parent || item[args.parent] === Number(args.parentId)) &&
30+
String(item.text).toLowerCase().includes(search.toLowerCase()),
31+
).map(
2932
(item) => ({ value: String(item.id), text: item.text }),
30-
).filter(
31-
(item) => String(item.text).toLowerCase().includes(search.toLowerCase()));
33+
);
3234

3335
return Promise.resolve(data);
3436
}

spec/features/inputs/nested_select_input_spec.rb

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -99,33 +99,48 @@
9999
end
100100
end
101101

102-
context "updating medium level" do
103-
before do
104-
on_input_ctx("invoice_region") do
105-
open_slimselect_options
106-
slimselect_search_input.set("Antof")
102+
context "when updating medium level (that uses collection option)" do
103+
context 'when searching for region that does not belong to selected country' do
104+
before do
105+
on_input_ctx("invoice_region") do
106+
open_slimselect_options
107+
slimselect_search_input.set("Cuy")
108+
end
107109
end
108-
end
109110

110-
it "shows results based on entered text", js: true do
111-
expect_slimselect_result_text_to_eq(1, "Antofagasta")
111+
it "does not show results", js: true do
112+
expect_slimselect_no_result
113+
end
112114
end
113115

114-
context "after click option" do
116+
context 'when searching for region that belongs to selected country' do
115117
before do
116-
on_input_ctx("invoice_region") { click_slimselect_option("Antofagasta") }
118+
on_input_ctx("invoice_region") do
119+
open_slimselect_options
120+
slimselect_search_input.set("Antof")
121+
end
117122
end
118123

119-
it "sets value", js: true do
120-
on_input_ctx("invoice_region") { expect_slimselect_selection("Antofagasta") }
124+
it "shows results based on entered text", js: true do
125+
expect_slimselect_result_text_to_eq(1, "Antofagasta")
121126
end
122127

123-
it "preserves parent value", js: true do
124-
on_input_ctx("invoice_country") { expect_slimselect_selection("Chile") }
125-
end
128+
context "when clicking option" do
129+
before do
130+
on_input_ctx("invoice_region") { click_slimselect_option("Antofagasta") }
131+
end
126132

127-
it "resets children values", js: true do
128-
on_input_ctx("invoice_city") { expect_slimselect_empty_selection }
133+
it "sets value", js: true do
134+
on_input_ctx("invoice_region") { expect_slimselect_selection("Antofagasta") }
135+
end
136+
137+
it "preserves parent value", js: true do
138+
on_input_ctx("invoice_country") { expect_slimselect_selection("Chile") }
139+
end
140+
141+
it "resets children values", js: true do
142+
on_input_ctx("invoice_city") { expect_slimselect_empty_selection }
143+
end
129144
end
130145
end
131146
end

0 commit comments

Comments
 (0)