forked from primer/view_components
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfieldset.rb
More file actions
66 lines (53 loc) · 2.16 KB
/
Copy pathfieldset.rb
File metadata and controls
66 lines (53 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# frozen_string_literal: true
module Primer
module OpenProject
# A low-level component for building fieldsets with unopinionated styling.
#
# This component is not designed to be used directly, but rather a primitive for
# authors of other components and form controls.
class Fieldset < Primer::Component
status :open_project
attr_reader :legend_text
renders_one :legend, ->(**system_arguments) {
LegendComponent.new(visually_hide_legend: @visually_hide_legend, **system_arguments)
}
# @param legend_text [String] A legend should be short and concise. The String will also be read by assistive technology.
# @param visually_hide_legend [Boolean] Controls if the legend is visible. If `true`, screen reader only text will be added.
# @param system_arguments [Hash] <%= link_to_system_arguments_docs %>
def initialize(legend_text: nil, visually_hide_legend: false, **system_arguments)
@legend_text = legend_text
@visually_hide_legend = visually_hide_legend
@system_arguments = deny_tag_argument(**system_arguments)
@system_arguments[:tag] = :fieldset
deny_aria_key(
:label,
"instead of `aria-label`, include `legend_text` and set `visually_hide_legend` to `true` on the component initializer.",
**@system_arguments
)
end
def render?
content? && (legend_text.present? || legend?)
end
class LegendComponent < Primer::Component
status :open_project
attr_reader :text
def initialize(text: nil, visually_hide_legend: false, **system_arguments)
@text = text
@system_arguments = deny_tag_argument(**system_arguments)
@system_arguments[:tag] = :legend
@system_arguments[:classes] = class_names(
@system_arguments[:classes],
{ "sr-only" => visually_hide_legend }
)
end
def call
render(Primer::BaseComponent.new(**@system_arguments)) { legend_content }
end
private
def legend_content
@legend_content ||= content || text
end
end
end
end
end