Class: HakumiComponents::Rate::Component
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
Instance Method Summary
collapse
-
#character_content ⇒ Object
-
#current_value ⇒ Object
-
#initialize(name: nil, label: nil, caption: nil, value: nil, default_value: 0, count: 5, allow_half: false, allow_clear: false, disabled: false, readonly: false, tooltips: [], character: nil, required: false, standalone: true, errors: [], **html_options) ⇒ Component
constructor
A new instance of Component.
-
#input_attributes ⇒ Object
-
#rate_classes ⇒ Object
-
#rate_wrapper_attributes ⇒ Object
-
#star_classes(state) ⇒ Object
-
#star_state(index) ⇒ Object
-
#tooltip_for(index) ⇒ Object
#describedby_ids, #error?, #error_message, #form_field_caption, #form_field_contract, #form_field_errors, #form_field_html_options, #form_field_label, #form_field_name, #form_field_required, #form_field_rules, #form_field_standalone, #form_item_attributes, #form_item_classes, #input_id, #render_caption, #render_error, #render_explain, #render_label, #standalone?
#form_field_contract
#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(name: nil, label: nil, caption: nil, value: nil, default_value: 0, count: 5, allow_half: false, allow_clear: false, disabled: false, readonly: false, tooltips: [], character: nil, required: false, standalone: true, errors: [], **html_options) ⇒ Component
Returns a new instance of Component.
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
67
68
69
70
71
72
73
74
|
# File 'app/components/hakumi_components/rate/component.rb', line 31
def initialize(
name: nil,
label: nil,
caption: nil,
value: nil,
default_value: 0,
count: 5,
allow_half: false,
allow_clear: false,
disabled: false,
readonly: false,
tooltips: [],
character: nil,
required: false,
standalone: true,
errors: [],
**html_options
)
@name = T.let(name || generate_id("rate"), Types::FormFieldName)
@label = label
@caption = caption
@value = value
@default_value = default_value
@count = count
@allow_half = allow_half
@allow_clear = allow_clear
@disabled = disabled
@readonly = readonly
@tooltips = T.let(tooltips, T::Array[String])
@character = character
@required = required
@standalone = standalone
@errors = T.let(errors, Types::FormFieldErrors)
@html_options = T.let(html_options, Types::HtmlAttributes)
initialize_form_field_contract!(
name: @name,
label: @label,
caption: @caption,
errors: @errors,
standalone: @standalone,
required: @required,
html_options: @html_options
)
end
|
Instance Method Details
#character_content ⇒ Object
148
149
150
|
# File 'app/components/hakumi_components/rate/component.rb', line 148
def character_content
render_value(@character || "★")
end
|
#current_value ⇒ Object
77
78
79
80
|
# File 'app/components/hakumi_components/rate/component.rb', line 77
def current_value
base_value = @value.nil? ? @default_value : @value
base_value.to_f
end
|
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
# File 'app/components/hakumi_components/rate/component.rb', line 153
def input_attributes
{
type: "hidden",
name: form_field_name,
id: input_id,
value: current_value,
disabled: @disabled ? true : nil,
"aria-invalid": error? ? "true" : nil,
"aria-describedby": describedby_ids,
data: {
hakumi__rate_target: "input"
}
}.compact
end
|
#rate_classes ⇒ Object
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'app/components/hakumi_components/rate/component.rb', line 83
def rate_classes
class_names(
"rate",
{
disabled: @disabled,
readonly: @readonly,
"allow-clear": @allow_clear,
"has-value": current_value.positive?
},
[ html_class_name ]
)
end
|
#rate_wrapper_attributes ⇒ Object
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
# File 'app/components/hakumi_components/rate/component.rb', line 97
def rate_wrapper_attributes
base_attrs = {
class: rate_classes,
role: "slider",
"aria-valuenow": current_value,
"aria-valuemin": @allow_clear ? 0 : (@allow_half ? 0.5 : 1),
"aria-valuemax": @count,
"aria-label": @label || "Rating",
"aria-readonly": @readonly ? "true" : nil,
"aria-disabled": @disabled ? "true" : nil,
data: {
controller: "hakumi--rate",
hakumi__rate_count_value: @count,
hakumi__rate_allow_half_value: @allow_half,
hakumi__rate_allow_clear_value: @allow_clear,
hakumi__rate_disabled_value: @disabled,
hakumi__rate_readonly_value: @readonly,
hakumi__rate_value_value: current_value
}
}
merge_attributes(base_attrs, @html_options.except(:class))
end
|
#star_classes(state) ⇒ Object
131
132
133
134
135
136
137
138
139
140
|
# File 'app/components/hakumi_components/rate/component.rb', line 131
def star_classes(state)
class_names(
"rate-star",
{
full: state == :full,
half: state == :half,
zero: state == :zero
}
)
end
|
#star_state(index) ⇒ Object
122
123
124
125
126
127
128
|
# File 'app/components/hakumi_components/rate/component.rb', line 122
def star_state(index)
value = current_value
return :full if value >= index + 1
return :half if @allow_half && value > index && value < index + 1
:zero
end
|
143
144
145
|
# File 'app/components/hakumi_components/rate/component.rb', line 143
def tooltip_for(index)
@tooltips[index]
end
|