Class: LightningUiKit::SliderComponent

Inherits:
BaseComponent
  • Object
show all
Defined in:
app/components/lightning_ui_kit/slider_component.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: nil, min: 0, max: 100, step: 1, value: nil, disabled: false, form: nil, **options) ⇒ SliderComponent

Returns a new instance of SliderComponent.



2
3
4
5
6
7
8
9
10
11
# File 'app/components/lightning_ui_kit/slider_component.rb', line 2

def initialize(name: nil, min: 0, max: 100, step: 1, value: nil, disabled: false, form: nil, **options)
  @name = name
  @min = min
  @max = max
  @step = step
  @value = value.nil? ? min : value
  @disabled = disabled
  @form = form
  @options = options
end

Instance Attribute Details

#disabledObject (readonly)

Returns the value of attribute disabled.



13
14
15
# File 'app/components/lightning_ui_kit/slider_component.rb', line 13

def disabled
  @disabled
end

#maxObject (readonly)

Returns the value of attribute max.



13
14
15
# File 'app/components/lightning_ui_kit/slider_component.rb', line 13

def max
  @max
end

#minObject (readonly)

Returns the value of attribute min.



13
14
15
# File 'app/components/lightning_ui_kit/slider_component.rb', line 13

def min
  @min
end

#stepObject (readonly)

Returns the value of attribute step.



13
14
15
# File 'app/components/lightning_ui_kit/slider_component.rb', line 13

def step
  @step
end

#valueObject (readonly)

Returns the value of attribute value.



13
14
15
# File 'app/components/lightning_ui_kit/slider_component.rb', line 13

def value
  @value
end

Instance Method Details

#classesObject



28
29
30
31
32
33
# File 'app/components/lightning_ui_kit/slider_component.rb', line 28

def classes
  merge_classes([
    "lui:relative lui:flex lui:h-5 lui:w-full lui:touch-none lui:select-none lui:items-center",
    @options[:class]
  ].compact.join(" "))
end

#dataObject



43
44
45
46
47
48
# File 'app/components/lightning_ui_kit/slider_component.rb', line 43

def data
  {
    controller: "lui-slider",
    action: "input->lui-slider#update"
  }.merge(@options[:data] || {})
end

#fill_styleObject



35
36
37
# File 'app/components/lightning_ui_kit/slider_component.rb', line 35

def fill_style
  "width: #{formatted_percent}%;"
end

#input_nameObject



22
23
24
25
26
# File 'app/components/lightning_ui_kit/slider_component.rb', line 22

def input_name
  return @name unless @form && @name

  "#{@form.object_name}[#{@name}]"
end

#percentObject



15
16
17
18
19
20
# File 'app/components/lightning_ui_kit/slider_component.rb', line 15

def percent
  range = (@max - @min).to_f
  return 0 if range <= 0

  (((@value - @min) / range) * 100).clamp(0, 100)
end

#thumb_styleObject



39
40
41
# File 'app/components/lightning_ui_kit/slider_component.rb', line 39

def thumb_style
  "left: #{formatted_percent}%;"
end