Skip to content

Commit 3aad267

Browse files
committed
[Feature] Add Sheet initial open state
1 parent 58243b0 commit 3aad267

3 files changed

Lines changed: 27 additions & 1 deletion

File tree

gem/lib/ruby_ui/sheet/sheet.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
module RubyUI
44
class Sheet < Base
5+
def initialize(open: false, **attrs)
6+
@open = open
7+
super(**attrs)
8+
end
9+
510
def view_template(&)
611
div(**attrs, &)
712
end
@@ -10,7 +15,10 @@ def view_template(&)
1015

1116
def default_attrs
1217
{
13-
data: {controller: "ruby-ui--sheet"}
18+
data: {
19+
controller: "ruby-ui--sheet",
20+
ruby_ui__sheet_open_value: @open.to_s
21+
}
1422
}
1523
end
1624
end

gem/lib/ruby_ui/sheet/sheet_controller.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ import { Controller } from "@hotwired/stimulus"
33
export default class extends Controller {
44
static targets = ["content"]
55

6+
static values = { open: false }
7+
8+
connect() {
9+
if (this.openValue) this.open()
10+
}
11+
612
open() {
713
document.body.insertAdjacentHTML("beforeend", this.contentTarget.innerHTML)
814
}

gem/test/ruby_ui/sheet_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,16 @@ def test_render_with_all_items
3030

3131
assert_match(/Open Sheet/, output)
3232
end
33+
34+
def test_render_closed_by_default
35+
output = phlex { RubyUI.Sheet { "content" } }
36+
37+
assert_match(/data-ruby-ui--sheet-open-value="false"/, output)
38+
end
39+
40+
def test_render_open_when_open_is_true
41+
output = phlex { RubyUI.Sheet(open: true) { "content" } }
42+
43+
assert_match(/data-ruby-ui--sheet-open-value="true"/, output)
44+
end
3345
end

0 commit comments

Comments
 (0)