Class: HakumiComponents::Segmented::Component
- Inherits:
-
BaseComponent
- Object
- ViewComponent::Base
- BaseComponent
- HakumiComponents::Segmented::Component
show all
- Extended by:
- T::Sig
- Defined in:
- app/components/hakumi_components/segmented/component.rb
Constant Summary
BaseComponent::ControllerOptions, BaseComponent::DateInput, BaseComponent::DateLikeValue, BaseComponent::DimensionInput, BaseComponent::HtmlPayloadInput, BaseComponent::I18nOptionValue, BaseComponent::PresenceArray, BaseComponent::PresenceScalar, BaseComponent::PresenceValue, BaseComponent::RawHtmlInput, BaseComponent::SIZES, BaseComponent::SizeValue, BaseComponent::SymbolInput
Class Method Summary
collapse
Instance Method Summary
collapse
-
#initialize(options: [], value: nil, default_value: nil, disabled: false, block: false, vertical: false, round: false, name: nil, size: :default, aria_label: nil, **html_options) ⇒ Component
constructor
A new instance of Component.
-
#normalized_options ⇒ Object
-
#option_classes(option) ⇒ Object
-
#option_data_attributes(option) ⇒ Object
-
#option_disabled?(option) ⇒ Boolean
-
#option_icon(option) ⇒ Object
-
#option_id(index) ⇒ Object
-
#option_input_attributes(option, index) ⇒ Object
-
#option_label(option) ⇒ Object
-
#option_selected?(option) ⇒ Boolean
-
#wrapper_attributes ⇒ Object
#append_data_token, boolean_html_param, #build_inline_style, cast_boolean, #cast_boolean, #class_names, #component_classes, #data_attributes_from, #dimension_to_css, #ensure_dom_id!, float_html_param, #generate_id, #html_classes, html_param, html_primitive_param, #html_style, #i18n_scope, integer_html_param, #merge_attributes, #render_value, #size_to_pixels, #stimulus_attrs, string_html_param, string_or_symbol_array_html_param, symbol_html_param, #t_default, #translate_with_default, #validate_inclusion!, #validate_required!, #value_present?
Constructor Details
#initialize(options: [], value: nil, default_value: nil, disabled: false, block: false, vertical: false, round: false, name: nil, size: :default, aria_label: nil, **html_options) ⇒ Component
Returns a new instance of Component.
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'app/components/hakumi_components/segmented/component.rb', line 53
def initialize(
options: [],
value: nil,
default_value: nil,
disabled: false,
block: false,
vertical: false,
round: false,
name: nil,
size: :default,
aria_label: nil,
**html_options
)
@options = T.let(HakumiComponents::Segmented::Option.coerce_all(options), T::Array[HakumiComponents::Segmented::Option])
@value = T.let(normalize_value(value, default_value), T.nilable(String))
@value = T.let(@options.first&.string_value, T.nilable(String)) if @value.nil? && @options.any?
@disabled = T.let(disabled, T::Boolean)
@block = T.let(block, T::Boolean)
@vertical = T.let(vertical, T::Boolean)
@round = T.let(round, T::Boolean)
@name = T.let(name.presence || generate_id("segmented"), String)
@size = T.let(size, Symbol)
@aria_label = T.let(aria_label, T.nilable(String))
@html_options = T.let(html_options, Types::HtmlAttributes)
validate_props!
end
|
Class Method Details
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'app/components/hakumi_components/segmented/component.rb', line 14
def self.(params)
options = html_param(params, :options)
options = html_param(params, :options_json) if options.nil?
normalized_options = if options.is_a?(String) || options.is_a?(Array)
HakumiComponents::Segmented::Option.coerce_all(options)
else
nil
end
{
options: normalized_options,
value: html_param(params, :value),
default_value: html_param(params, :default_value),
disabled: boolean_html_param(html_param(params, :disabled)),
block: boolean_html_param(html_param(params, :block)),
vertical: boolean_html_param(html_param(params, :vertical)),
round: boolean_html_param(html_param(params, :round)),
name: string_html_param(html_param(params, :name)),
size: symbol_html_param(html_param(params, :size)),
aria_label: string_html_param(html_param(params, :aria_label))
}.compact
end
|
Instance Method Details
#normalized_options ⇒ Object
96
97
98
|
# File 'app/components/hakumi_components/segmented/component.rb', line 96
def normalized_options
@options
end
|
#option_classes(option) ⇒ Object
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'app/components/hakumi_components/segmented/component.rb', line 101
def option_classes(option)
class_names(
"segmented-item",
{
selected: option_selected?(option),
disabled: option_disabled?(option)
},
[
option.class_name,
("hakumi-segmented-item-icon-only" if option.icon_only?)
]
)
end
|
#option_data_attributes(option) ⇒ Object
116
117
118
119
120
121
122
123
|
# File 'app/components/hakumi_components/segmented/component.rb', line 116
def option_data_attributes(option)
{
"hakumi--segmented-target": "item",
value: option.string_value,
label: option.label.is_a?(String) ? option.label : nil,
disabled: option_disabled?(option) ? "true" : nil
}.compact
end
|
#option_disabled?(option) ⇒ Boolean
151
152
153
|
# File 'app/components/hakumi_components/segmented/component.rb', line 151
def option_disabled?(option)
@disabled || option.disabled
end
|
#option_icon(option) ⇒ Object
171
172
173
174
175
176
177
178
179
180
181
182
|
# File 'app/components/hakumi_components/segmented/component.rb', line 171
def option_icon(option)
icon = option.icon
return nil if icon.nil?
if icon.is_a?(String) || icon.is_a?(Symbol)
render HakumiComponents::Icon::Component.new(name: icon, color: "currentColor")
elsif icon.respond_to?(:render_in)
render(icon)
else
nil
end
end
|
#option_id(index) ⇒ Object
156
157
158
|
# File 'app/components/hakumi_components/segmented/component.rb', line 156
def option_id(index)
"#{@name}_#{index}"
end
|
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
# File 'app/components/hakumi_components/segmented/component.rb', line 126
def option_input_attributes(option, index)
attrs = {
id: option_id(index),
class: "hakumi-segmented-input",
disabled: option_disabled?(option),
data: {
"hakumi--segmented-target": "input",
action: "change->hakumi--segmented#select"
}
}
aria_label = option_aria_label(option)
attrs[:"aria-label"] = aria_label if aria_label
attrs
end
|
#option_label(option) ⇒ Object
161
162
163
164
165
166
167
168
|
# File 'app/components/hakumi_components/segmented/component.rb', line 161
def option_label(option)
label = option.label
return nil if label.nil?
return label if label.is_a?(String)
return render(label) if label.respond_to?(:render_in)
label.to_s
end
|
#option_selected?(option) ⇒ Boolean
144
145
146
147
148
|
# File 'app/components/hakumi_components/segmented/component.rb', line 144
def option_selected?(option)
return false if @value.nil?
option.string_value == @value.to_s
end
|
#wrapper_attributes ⇒ Object
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'app/components/hakumi_components/segmented/component.rb', line 82
def wrapper_attributes
attrs = {
class: wrapper_classes,
role: "radiogroup",
"aria-disabled": @disabled ? "true" : nil,
"aria-orientation": @vertical ? "vertical" : nil,
"aria-label": @aria_label,
data: data_attributes
}
merge_attributes(attrs, @html_options.except(:class))
end
|