Skip to content

Commit 537b7c0

Browse files
authored
Add toggle all checkbox to combobox (#221)
1 parent 0d88340 commit 537b7c0

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

lib/ruby_ui/combobox/combobox_controller.js

+17
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export default class extends Controller {
99

1010
static targets = [
1111
"input",
12+
"toggleAll",
1213
"popover",
1314
"item",
1415
"emptyState",
@@ -33,12 +34,22 @@ export default class extends Controller {
3334
if (e.target.type == "radio") {
3435
this.closePopover()
3536
}
37+
38+
if (this.hasToggleAllTarget && !e.target.checked) {
39+
this.toggleAllTarget.checked = false
40+
}
3641
}
3742

3843
inputContent(input) {
3944
return input.dataset.text || input.parentElement.innerText
4045
}
4146

47+
toggleAllItems() {
48+
const isChecked = this.toggleAllTarget.checked
49+
this.inputTargets.forEach(input => input.checked = isChecked)
50+
this.updateTriggerContent()
51+
}
52+
4253
updateTriggerContent() {
4354
const checkedInputs = this.inputTargets.filter(input => input.checked)
4455

@@ -73,6 +84,12 @@ export default class extends Controller {
7384
}
7485

7586
const filterTerm = this.searchInputTarget.value.toLowerCase()
87+
88+
if (this.hasToggleAllTarget) {
89+
if (filterTerm) this.toggleAllTarget.parentElement.classList.add("hidden")
90+
else this.toggleAllTarget.parentElement.classList.remove("hidden")
91+
}
92+
7693
let resultCount = 0
7794

7895
this.selectedItemIndex = null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
module RubyUI
4+
class ComboboxToggleAllCheckbox < Base
5+
def view_template
6+
input(type: "checkbox", **attrs)
7+
end
8+
9+
private
10+
11+
def default_attrs
12+
{
13+
class: [
14+
"peer h-4 w-4 shrink-0 rounded-sm border border-primary ring-offset-background accent-primary",
15+
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",
16+
"disabled:cursor-not-allowed disabled:opacity-50"
17+
],
18+
data: {
19+
ruby_ui__combobox_target: "toggleAll",
20+
action: "change->ruby-ui--combobox#toggleAllItems"
21+
}
22+
}
23+
end
24+
end
25+
end

0 commit comments

Comments
 (0)