Sha256: 1a65951ccf68f7521a4e16c8b368f82d1f10c2d3f466d5b2b8862ad47d287d28

Contents?: true

Size: 1.73 KB

Versions: 7

Compression:

Stored size: 1.73 KB

Contents

# frozen_string_literal: true

module Satis
  module DateTimePicker
    class Component < Satis::ApplicationComponent
      attr_reader :form, :attribute, :inline, :options, :clearable, :format, :time_picker, :multiple, :range

      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')

        # 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

      def week_start
        Date::DAYS_INTO_WEEK[Date.beginning_of_week] || 1
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
satis-1.0.75 app/components/satis/date_time_picker/component.rb
satis-1.0.74 app/components/satis/date_time_picker/component.rb
satis-1.0.70 app/components/satis/date_time_picker/component.rb
satis-1.0.69 app/components/satis/date_time_picker/component.rb
satis-1.0.68 app/components/satis/date_time_picker/component.rb
satis-1.0.67 app/components/satis/date_time_picker/component.rb
satis-1.0.66 app/components/satis/date_time_picker/component.rb