1
+ <% checkboxes = [
2
+ { name: 'Coffee', id: 'coffee', checked: false },
3
+ { name: 'Ice Cream', id: 'ice-cream', checked: false },
4
+ { name: 'Chocolate', id: 'chocolate', checked: true }
5
+ ] %>
6
+
7
+ <%= pb_rails("flex", props: { justify: "end", margin_bottom: "sm" }) do %>
8
+ <%= pb_rails("flex", props: { justify: "end", margin_bottom: "sm" }) do %>
9
+ <%= pb_rails("button", props: { text: "Delete", id: "delete-button" }) %>
10
+ <% end %>
11
+ <% end %>
12
+
13
+ <%= pb_rails("table", props: { size: "sm" }) do %>
14
+ <%= pb_rails("table/table_head") do %>
15
+ <%= pb_rails("table/table_row") do %>
16
+ <%= pb_rails("table/table_header") do %>
17
+ <%= pb_rails("checkbox", props: {
18
+ checked: true,
19
+ value: "checkbox-value",
20
+ name: "main-checkbox-selectable",
21
+ indeterminate: true,
22
+ id: "checkbox-selectable"
23
+ }) %>
24
+ <% end %>
25
+ <%= pb_rails("table/table_header", props: { text: "Column 1" }) %>
26
+ <%= pb_rails("table/table_header", props: { text: "Column 2" }) %>
27
+ <%= pb_rails("table/table_header", props: { text: "Column 3" }) %>
28
+ <%= pb_rails("table/table_header", props: { text: "Column 4" }) %>
29
+ <%= pb_rails("table/table_header", props: { text: "Column 5" }) %>
30
+ <% end %>
31
+ <% end %>
32
+ <%= pb_rails("table/table_body") do %>
33
+ <% checkboxes.each_with_index do |checkbox, index| %>
34
+ <%= pb_rails("table/table_row") do %>
35
+ <%= pb_rails("table/table_cell") do %>
36
+ <%= pb_rails("checkbox", props: { checked: checkbox[:checked], id: "#{checkbox[:id]}-selectable-checkbox", name: "#{checkbox[:id]}-selectable-checkbox", on_change: "updateCheckboxes(#{index})", value: "check-box value" }) %>
37
+ <% end %>
38
+ <%= pb_rails("table/table_cell") do %>
39
+ <%= pb_rails("image", props: { alt: "picture of a misty forest", size: "xs", url: "https://unsplash.it/500/400/?image=634" }) %>
40
+ <% end %>
41
+ <%= pb_rails("table/table_cell", props: { text: "Value 2" }) %>
42
+ <%= pb_rails("table/table_cell", props: { text: "Value 3" }) %>
43
+ <%= pb_rails("table/table_cell", props: { text: "Value 4" }) %>
44
+ <%= pb_rails("table/table_cell", props: { text: "Value 5" }) %>
45
+ <% end %>
46
+ <% end %>
47
+ <% end %>
48
+ <% end %>
49
+
50
+ < script >
51
+ document . addEventListener ( 'DOMContentLoaded' , function ( ) {
52
+ const mainCheckboxWrapper = document . getElementById ( 'checkbox-selectable' ) ;
53
+ const mainCheckbox = document . getElementsByName ( "main-checkbox-selectable" ) [ 0 ] ;
54
+ const childCheckboxes = document . querySelectorAll ( 'input[type="checkbox"][id$="selectable-checkbox"]' ) ;
55
+ const deleteButton = document . getElementById ( 'delete-button' ) ;
56
+
57
+ const updateDeleteButton = ( ) => {
58
+ const anyChecked = Array . from ( childCheckboxes ) . some ( checkbox => checkbox . checked ) ;
59
+ deleteButton . style . display = anyChecked ? 'block' : 'none' ;
60
+ } ;
61
+
62
+ const updateMainCheckbox = ( ) => {
63
+ // Count the number of checked child checkboxes
64
+ const checkedCount = Array . from ( childCheckboxes ) . filter ( cb => cb . checked ) . length ;
65
+ // Determine if the main checkbox should be in an indeterminate state
66
+ const indeterminate = checkedCount > 0 && checkedCount < childCheckboxes . length ;
67
+
68
+ // Set the main checkbox states
69
+ mainCheckbox . indeterminate = indeterminate ;
70
+ mainCheckbox . checked = checkedCount > 0 ;
71
+
72
+ // Determine the icon class to add and remove based on the number of checked checkboxes
73
+ const iconClassToAdd = checkedCount === 0 ? 'pb_checkbox_checkmark' : 'pb_checkbox_indeterminate' ;
74
+ const iconClassToRemove = checkedCount === 0 ? 'pb_checkbox_indeterminate' : 'pb_checkbox_checkmark' ;
75
+
76
+ // Add and remove the icon class to the main checkbox wrapper
77
+ mainCheckboxWrapper . querySelector ( '[data-pb-checkbox-icon-span]' ) . classList . add ( iconClassToAdd ) ;
78
+ mainCheckboxWrapper . querySelector ( '[data-pb-checkbox-icon-span]' ) . classList . remove ( iconClassToRemove ) ;
79
+
80
+ // Toggle the visibility of the checkbox icon based on the indeterminate state
81
+ mainCheckboxWrapper . getElementsByClassName ( "indeterminate_icon" ) [ 0 ] . classList . toggle ( 'hidden' , ! indeterminate ) ;
82
+ mainCheckboxWrapper . getElementsByClassName ( "check_icon" ) [ 0 ] . classList . toggle ( 'hidden' , indeterminate ) ;
83
+
84
+ updateDeleteButton ( ) ;
85
+ } ;
86
+
87
+ mainCheckbox . addEventListener ( 'change' , function ( ) {
88
+ childCheckboxes . forEach ( cb => cb . checked = this . checked ) ;
89
+ updateMainCheckbox ( ) ;
90
+ } ) ;
91
+
92
+ childCheckboxes . forEach ( cb => {
93
+ cb . addEventListener ( 'change' , updateMainCheckbox ) ;
94
+ } ) ;
95
+ } ) ;
96
+ </ script >
0 commit comments