Skip to content

Commit 72d9a41

Browse files
rctneilPaul-Bob
andauthored
Fix Grid View so that Action is populated when item is selected. (#4595)
Co-authored-by: Paul Bob <69730720+Paul-Bob@users.noreply.github.com> Co-authored-by: Paul Bob <paul.ionut.bob@gmail.com>
1 parent 6224fe9 commit 72d9a41

3 files changed

Lines changed: 39 additions & 2 deletions

File tree

app/components/avo/view_types/grid_component.html.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
tabindex: (@reflection.present? ? nil : "-1"),
77
data: {
88
component_name: self.class.to_s.underscore,
9+
controller: "item-select-all",
10+
resource_name: @resource.model_key,
911
selected_resources_name: @resource.model_key,
1012
selected_resources: [],
1113
**(@reflection.present? ? {} : {"content-focus": ""}),

app/javascript/js/controllers/item_select_all_controller.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ export default class extends Controller {
2222
}
2323

2424
elementAttributeValueChanged(element) {
25+
if (!this.hasCheckboxTarget) return
26+
2527
// Check if anything is selected.
2628
const selectedResources = JSON.parse(element.dataset.selectedResources)
2729
// If all are selected, mark the checkbox as checked.
@@ -47,7 +49,10 @@ export default class extends Controller {
4749

4850
deselectAll() {
4951
this.itemCheckboxTargets.forEach((checkbox) => checkbox.checked && checkbox.click())
50-
this.checkboxTarget.checked = false
52+
53+
if (this.hasCheckboxTarget) {
54+
this.checkboxTarget.checked = false
55+
}
5156

5257
if (this.selectAllEnabled()) {
5358
this.selectAllOverlay(false)
@@ -73,7 +78,10 @@ export default class extends Controller {
7378
let allSelected = true
7479
// eslint-disable-next-line no-return-assign
7580
this.itemCheckboxTargets.forEach((checkbox) => allSelected = allSelected && checkbox.checked)
76-
this.checkboxTarget.checked = allSelected
81+
82+
if (this.hasCheckboxTarget) {
83+
this.checkboxTarget.checked = allSelected
84+
}
7785

7886
if (this.selectAllEnabled()) {
7987
this.selectAllOverlay(allSelected)

spec/system/avo/group_3/actions_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,33 @@
484484
click_on "Actions"
485485
expect(page.find("a", text: "Toggle post published")["data-disabled"]).to eq "true"
486486
end
487+
488+
# Regression for https://github.com/avo-hq/avo/pull/4595 — selected grid
489+
# records must reach the action (confirmation message + handle query).
490+
it "runs an action with the selected records on the products grid" do
491+
product = create :product, title: "Grid Product One"
492+
493+
visit avo.resources_products_path
494+
495+
grid_component = find(
496+
"[data-component-name=\"avo/index/grid_item_component\"]" \
497+
"[data-resource-name=\"products\"]" \
498+
"[data-record-id=\"#{product.to_param}\"]"
499+
)
500+
501+
within grid_component do
502+
grid_component.hover
503+
find("input[type=checkbox]").click
504+
end
505+
506+
open_panel_action(action_name: "Show products")
507+
508+
expect(page).to have_text "Selected products: Grid Product One"
509+
510+
run_action
511+
512+
expect(page).to have_text "Selected products: Grid Product One"
513+
end
487514
end
488515

489516
describe "record assignment" do

0 commit comments

Comments
 (0)