Skip to content

Commit af1e38a

Browse files
authored
Add toggle all example to combobox docs (#159)
1 parent 194bfac commit af1e38a

File tree

6 files changed

+59
-7
lines changed

6 files changed

+59
-7
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ GIT
1414

1515
GIT
1616
remote: https://github.com/ruby-ui/ruby_ui.git
17-
revision: 500b8d98fbee73f15a3182feb4eca8e8e146d6b5
17+
revision: 537b7c06f57a0918e4ba07ce08c3dad3dace15a3
1818
branch: main
1919
specs:
2020
ruby_ui (1.0.0.beta1)
Lines changed: 25 additions & 0 deletions
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

app/javascript/controllers/ruby_ui/combobox_controller.js

Lines changed: 17 additions & 0 deletions
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

app/views/docs/combobox_view.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ def view_template
7676
ComboboxList do
7777
ComboboxEmptyState { "No result" }
7878
79+
ComboboxItem(class: "mt-3") do
80+
ComboboxToggleAllCheckbox(name: "all", value: "all")
81+
span { "Select all" }
82+
end
83+
7984
ComboboxListGroup label: "Fruits" do
8085
ComboboxItem do
8186
ComboboxCheckbox(name: "food", value: "apple")

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "app",
33
"private": "true",
44
"dependencies": {
5-
"@floating-ui/dom": "^1.6.12",
5+
"@floating-ui/dom": "^1.6.13",
66
"@hotwired/stimulus": "3.2.2",
77
"@hotwired/turbo-rails": "8.0.12",
88
"autoprefixer": "10.4.19",

yarn.lock

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,19 +134,24 @@
134134
dependencies:
135135
"@floating-ui/utils" "^0.2.8"
136136

137-
"@floating-ui/dom@^1.6.12":
138-
version "1.6.12"
139-
resolved "https://registry.yarnpkg.com/@floating-ui/dom/-/dom-1.6.12.tgz#6333dcb5a8ead3b2bf82f33d6bc410e95f54e556"
140-
integrity sha512-NP83c0HjokcGVEMeoStg317VD9W7eDlGK7457dMBANbKA6GJZdc7rjujdgqzTaz93jkGgc5P/jeWbaCHnMNc+w==
137+
"@floating-ui/dom@^1.6.13":
138+
version "1.6.13"
139+
resolved "https://registry.yarnpkg.com/@floating-ui/dom/-/dom-1.6.13.tgz#a8a938532aea27a95121ec16e667a7cbe8c59e34"
140+
integrity sha512-umqzocjDgNRGTuO7Q8CU32dkHkECqI8ZdMZ5Swb6QAM0t5rnlrN3lGo1hdpscRd3WS8T6DKYK4ephgIH9iRh3w==
141141
dependencies:
142142
"@floating-ui/core" "^1.6.0"
143-
"@floating-ui/utils" "^0.2.8"
143+
"@floating-ui/utils" "^0.2.9"
144144

145145
"@floating-ui/utils@^0.2.8":
146146
version "0.2.8"
147147
resolved "https://registry.npmjs.org/@floating-ui/utils/-/utils-0.2.8.tgz"
148148
integrity sha512-kym7SodPp8/wloecOpcmSnWJsK7M0E5Wg8UcFA+uO4B9s5d0ywXOEro/8HM9x0rW+TljRzul/14UYz3TleT3ig==
149149

150+
"@floating-ui/utils@^0.2.9":
151+
version "0.2.9"
152+
resolved "https://registry.yarnpkg.com/@floating-ui/utils/-/utils-0.2.9.tgz#50dea3616bc8191fb8e112283b49eaff03e78429"
153+
integrity sha512-MDWhGtE+eHw5JW7lq4qhc5yRLS11ERl1c7Z6Xd0a58DozHES6EnNNwUWbMiG4J9Cgj053Bhk8zvlhFYKVhULwg==
154+
150155
"@hotwired/[email protected]":
151156
version "3.2.2"
152157
resolved "https://registry.npmjs.org/@hotwired/stimulus/-/stimulus-3.2.2.tgz"

0 commit comments

Comments
 (0)