Class: LightningUiKit::DatePickerComponent

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, selected: nil, form: nil, placeholder: "Pick a date", format: "%B %-d, %Y", **options) ⇒ DatePickerComponent

Returns a new instance of DatePickerComponent.



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

def initialize(name:, selected: nil, form: nil, placeholder: "Pick a date", format: "%B %-d, %Y", **options)
  @name = name
  @selected = selected
  @form = form
  @placeholder = placeholder
  @format = format
  @options = options
end

Instance Attribute Details

#placeholderObject (readonly)

Returns the value of attribute placeholder.



11
12
13
# File 'app/components/lightning_ui_kit/date_picker_component.rb', line 11

def placeholder
  @placeholder
end

Instance Method Details

#dataObject



47
48
49
50
51
52
# File 'app/components/lightning_ui_kit/date_picker_component.rb', line 47

def data
  {
    controller: "lui-date-picker",
    action: "keydown.esc@window->lui-date-picker#close click@window->lui-date-picker#closeOnOutside"
  }.merge(@options[:data] || {})
end

#input_nameObject



32
33
34
35
36
# File 'app/components/lightning_ui_kit/date_picker_component.rb', line 32

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

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

#label_textObject



23
24
25
26
27
28
29
30
# File 'app/components/lightning_ui_kit/date_picker_component.rb', line 23

def label_text
  return placeholder unless selected?

  date = @selected.respond_to?(:strftime) ? @selected : Date.parse(@selected.to_s)
  date.strftime(@format)
rescue ArgumentError, TypeError
  selected_iso
end

#panel_classesObject



43
44
45
# File 'app/components/lightning_ui_kit/date_picker_component.rb', line 43

def panel_classes
  "lui:hidden lui:absolute lui:left-0 lui:top-full lui:z-50 lui:mt-1.5"
end

#selected?Boolean

Returns:

  • (Boolean)


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

def selected?
  !(@selected.nil? || @selected == "")
end

#selected_isoObject



17
18
19
20
21
# File 'app/components/lightning_ui_kit/date_picker_component.rb', line 17

def selected_iso
  return nil unless selected?

  @selected.respond_to?(:strftime) ? @selected.strftime("%Y-%m-%d") : @selected.to_s
end

#trigger_classesObject



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

def trigger_classes
  muted = selected? ? "" : " lui:text-foreground-muted"
  "lui:inline-flex lui:h-9 lui:w-56 lui:items-center lui:gap-2 lui:rounded-md lui:border lui:border-border lui:bg-surface-input lui:px-3 lui:text-left lui:text-sm lui:text-foreground lui:transition-colors lui:hover:bg-surface-hover lui:focus:outline-focus lui:cursor-pointer#{muted}"
end