Skip to content

Commit 500b8d9

Browse files
authored
Add Progress component (#218)
1 parent 8be3d4f commit 500b8d9

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

lib/ruby_ui/progress/progress.rb

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
module RubyUI
4+
class Progress < Base
5+
def initialize(value: 0, **attrs)
6+
@value = value.to_f.clamp(0, 100)
7+
8+
super(**attrs)
9+
end
10+
11+
def view_template
12+
div(**attrs) do
13+
div(**indicator_attrs)
14+
end
15+
end
16+
17+
private
18+
19+
def default_attrs
20+
{
21+
role: "progressbar",
22+
aria_valuenow: @value,
23+
aria_valuemin: 0,
24+
aria_valuemax: 100,
25+
aria_valuetext: "#{@value}%",
26+
class: "relative h-2 overflow-hidden rounded-full bg-primary/20 [&>*]:bg-primary"
27+
}
28+
end
29+
30+
def indicator_attrs
31+
{
32+
class: "h-full w-full flex-1",
33+
style: "transform: translateX(-#{100 - @value}%)"
34+
}
35+
end
36+
end
37+
end

test/ruby_ui/progress_test.rb

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
5+
class RubyUI::ProgressTest < ComponentTest
6+
def test_render
7+
output = phlex do
8+
RubyUI::Progress(value: 50)
9+
end
10+
11+
assert_match(/aria-valuemin="0"/, output)
12+
assert_match(/aria-valuemax="100"/, output)
13+
assert_match(/aria-valuenow="50.0"/, output)
14+
assert_match(/aria-valuetext="50.0%"/, output)
15+
assert_match(/translateX\(-50.0%\)/, output)
16+
end
17+
end

0 commit comments

Comments
 (0)