Class: HakumiComponents::Slider::Component

Inherits:
BaseComponent
  • Object
show all
Extended by:
T::Sig
Includes:
Concerns::FormField
Defined in:
app/components/hakumi_components/slider/component.rb

Constant Summary collapse

MarkScalar =
T.type_alias { T.nilable(Types::ValidationPrimitive) }
MarkHash =
T.type_alias { T::Hash[Types::HtmlKey, MarkScalar] }
MarkMap =
T.type_alias { T::Hash[Types::ValidationPrimitive, Types::ValidationPrimitive] }
MarkEntry =
T.type_alias { T.any(HakumiComponents::Slider::Mark, MarkHash, Types::ValidationPrimitive) }
NumericInput =
T.type_alias { T.any(Numeric, String) }
MarksInput =
T.type_alias do
  T.nilable(T.any(MarkMap, T::Array[MarkEntry]))
end

Constants inherited from BaseComponent

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

Methods included from Concerns::FormField

#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?

Methods included from Concerns::FormFieldInterface

#form_field_contract

Methods inherited from BaseComponent

#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: nil, min: 0, max: 100, step: 1, range: false, vertical: false, marks: nil, dots: false, tooltip_visible: nil, keyboard: true, disabled: false, allow_cross: true, handle_min: nil, handle_max: nil, standalone: true, required: false, errors: [], **html_options) ⇒ Component

Returns a new instance of Component.



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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/components/hakumi_components/slider/component.rb', line 46

def initialize(
  name: nil,
  label: nil,
  caption: nil,
  value: nil,
  default_value: nil,
  min: 0,
  max: 100,
  step: 1,
  range: false,
  vertical: false,
  marks: nil,
  dots: false,
  tooltip_visible: nil,
  keyboard: true,
  disabled: false,
  allow_cross: true,
  handle_min: nil,
  handle_max: nil,
  standalone: true,
  required: false,
  errors: [],
  **html_options
)
  validate_numeric_like!(min, "min")
  validate_numeric_like!(max, "max")
  validate_numeric_like!(step, "step")

  @name = T.let(name || generate_id("slider"), Types::FormFieldName)
  @label = T.let(label, T.nilable(String))
  @caption = T.let(caption, T.nilable(String))
  @value = T.let(value, Types::FormFieldValue)
  @default_value = T.let(default_value, Types::FormFieldValue)
  @min = T.let(min.to_s, String)
  @max = T.let(max.to_s, String)
  @step = T.let(step.to_s, String)
  @range = T.let(cast_boolean(range) ? true : false, T::Boolean)
  @vertical = T.let(cast_boolean(vertical) ? true : false, T::Boolean)
  @marks = T.let(normalize_marks(marks), T::Array[HakumiComponents::Slider::Mark])
  @dots = T.let(cast_boolean(dots) ? true : false, T::Boolean)
  @tooltip_visible = T.let(cast_boolean(tooltip_visible), T.nilable(T::Boolean))
  @keyboard = T.let(cast_boolean(keyboard) != false, T::Boolean)
  @disabled = T.let(cast_boolean(disabled) ? true : false, T::Boolean)
  @allow_cross = T.let(cast_boolean(allow_cross) != false, T::Boolean)
  @handle_min = T.let(handle_min, Types::FormFieldValue)
  @handle_max = T.let(handle_max, Types::FormFieldValue)
  @standalone = T.let(cast_boolean(standalone) ? true : false, T::Boolean)
  @required = T.let(cast_boolean(required) ? true : false, T::Boolean)
  @errors = T.let(Array(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
  )

  validate_props!
end

Instance Method Details

#current_input_value(index = 0) ⇒ Object



217
218
219
220
221
222
# File 'app/components/hakumi_components/slider/component.rb', line 217

def current_input_value(index = 0)
  values = normalized_input_values
  return values.first unless @range

  values[index]
end

#dot_valuesObject



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'app/components/hakumi_components/slider/component.rb', line 199

def dot_values
  return [] unless @dots

  min = @min.to_f
  max = @max.to_f
  step = @step.to_f
  return [] if step <= 0

  values = T.let([], T::Array[String])
  current = min
  while current <= max + (step / 1000.0)
    values << current.to_s
    current += step
  end
  values
end

#handle_countObject



154
155
156
# File 'app/components/hakumi_components/slider/component.rb', line 154

def handle_count
  @range ? 2 : 1
end

#input_id_for(index) ⇒ Object



147
148
149
150
151
# File 'app/components/hakumi_components/slider/component.rb', line 147

def input_id_for(index)
  return input_id.to_s unless @range

  "#{input_id}_#{index}"
end

#input_nameObject



139
140
141
142
143
144
# File 'app/components/hakumi_components/slider/component.rb', line 139

def input_name
  return form_field_name unless @range
  return form_field_name if form_field_name.to_s.end_with?("[]")

  "#{form_field_name}[]"
end

#mark_style(value) ⇒ Object



189
190
191
192
193
194
195
196
# File 'app/components/hakumi_components/slider/component.rb', line 189

def mark_style(value)
  percent = position_percent(value)
  if @vertical
    "bottom: #{percent}%"
  else
    "left: #{percent}%"
  end
end

#marks?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'app/components/hakumi_components/slider/component.rb', line 159

def marks?
  @marks.any?
end

#slider_disabled?Boolean

Returns:

  • (Boolean)


174
175
176
# File 'app/components/hakumi_components/slider/component.rb', line 174

def slider_disabled?
  @disabled
end

#slider_dots?Boolean

Returns:

  • (Boolean)


169
170
171
# File 'app/components/hakumi_components/slider/component.rb', line 169

def slider_dots?
  @dots
end

#slider_marksObject



164
165
166
# File 'app/components/hakumi_components/slider/component.rb', line 164

def slider_marks
  @marks
end

#slider_maxObject



184
185
186
# File 'app/components/hakumi_components/slider/component.rb', line 184

def slider_max
  @max
end

#slider_minObject



179
180
181
# File 'app/components/hakumi_components/slider/component.rb', line 179

def slider_min
  @min
end

#wrapper_attributesObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/components/hakumi_components/slider/component.rb', line 110

def wrapper_attributes
  merge_attributes(
    {
      class: component_html_class,
      data: {
        controller: "hakumi--slider",
        "hakumi--slider-min-value": @min,
        "hakumi--slider-max-value": @max,
        "hakumi--slider-step-value": @step,
        "hakumi--slider-range-value": @range,
        "hakumi--slider-vertical-value": @vertical,
        "hakumi--slider-disabled-value": @disabled,
        "hakumi--slider-keyboard-value": @keyboard,
        "hakumi--slider-allow-cross-value": @allow_cross,
        "hakumi--slider-handle-min-value": handle_bounds_value(@handle_min),
        "hakumi--slider-handle-max-value": handle_bounds_value(@handle_max),
        "hakumi--slider-tooltip-visible-value": tooltip_visible_value,
        "hakumi--slider-value-value": data_value(@value),
        "hakumi--slider-default-value-value": data_value(@default_value)
      }.compact
    },
    {
      id: @html_options[:id],
      data: @html_options[:data]
    }.compact
  )
end