Method: Satis::DateTimePicker::Component#initialize

Defined in:
app/components/satis/date_time_picker/component.rb

#initialize(form:, attribute:, **options, &block) ⇒ Component

Returns a new instance of Component.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/components/satis/date_time_picker/component.rb', line 8

def initialize(form:, attribute:, **options, &block)
  super

  @form = form
  @attribute = attribute
  @options = options
  @block = block
  options[:input_html] ||= {}
  @time_picker = options.key?(:time_picker) ? options[:time_picker] : true
  @inline = options.key?(:inline) ? options[:inline] : false
  @clearable = options.key?(:clearable) ? options[:clearable] : true
  @multiple = options.key?(:multiple) ? options[:multiple] : false
  @range = options.key?(:range) ? options[:range] : false

  @format = if options.key?(:format)
    options[:format]
  else
    {weekday: "long", month: "short", year: "numeric", day: "numeric",
     hour: "numeric", minute: "numeric", hour12: false}
  end

  options[:input_html].merge!("data-satis-date-time-picker-target" => "hiddenInput", "data-action" => "change->satis-date-time-picker#hiddenInputChanged")

  # FIXME: deal with ranges and multiples
  hidden_value = options[:input_html][:value]
  hidden_value ||= @form.object.send(attribute)
  hidden_value = if hidden_value.is_a?(String)
    hidden_value&.split(" - ")&.map { |d| Time.parse(d).iso8601 }&.join(" - ")
  else
    hidden_value&.iso8601
  end

  options[:input_html][:value] = hidden_value
end