-
Notifications
You must be signed in to change notification settings - Fork 62
Add a separator component #231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module RubyUI | ||
| class Separator < Base | ||
| ORIENTATIONS = %i[horizontal vertical].freeze | ||
|
|
||
| def initialize(as: "div", orientation: :horizontal, decorative: true, **attrs) | ||
| raise ArgumentError, "Invalid orientation: #{orientation}" unless ORIENTATIONS.include?(orientation.to_sym) | ||
|
|
||
| @as = as | ||
| @orientation = orientation.to_sym | ||
| @decorative = decorative | ||
| super(**attrs) | ||
| end | ||
|
|
||
| def view_template(&) | ||
| public_send(@as, **attrs, &) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def default_attrs | ||
| { | ||
| role: (@decorative ? "none" : "separator"), | ||
| class: [ | ||
| "shrink-0 bg-border", | ||
| orientation_classes | ||
| ], | ||
| data: { | ||
| orientation: @orientation | ||
| } | ||
|
||
| } | ||
| end | ||
|
|
||
| def orientation_classes | ||
| return "h-[1px] w-full" if @orientation == :horizontal | ||
|
|
||
| "h-full w-[1px]" | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "test_helper" | ||
|
|
||
| class RubyUI::SeparatorTest < ComponentTest | ||
| def test_render | ||
| output = phlex do | ||
| RubyUI.Separator() | ||
| end | ||
|
|
||
| assert_match(/div/, output) | ||
| assert_match(/role="none"/, output) | ||
| assert_match(/data-orientation="horizontal"/, output) | ||
| end | ||
|
|
||
| def test_render_with_vertical_orientation | ||
| output = phlex do | ||
| RubyUI.Separator(orientation: :vertical) | ||
| end | ||
|
|
||
| assert_match(/data-orientation="vertical"/, output) | ||
| end | ||
|
|
||
| def test_render_with_decorative_false | ||
| output = phlex do | ||
| RubyUI.Separator(decorative: false) | ||
| end | ||
|
|
||
| assert_match(/role="separator"/, output) | ||
| end | ||
|
|
||
| def test_render_with_custom_tag | ||
| output = phlex do | ||
| RubyUI.Separator(as: "hr") | ||
| end | ||
|
|
||
| assert_match(/hr/, output) | ||
| end | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
asparameter could be a symbol, and then we could usetagphlex method here, eg.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added it as
tag(@as, **attrs, &)because, without the parentheses, the syntax highlighting was incorrect: