Class: ShadcnPhlexcomponents::DateRangePicker

Inherits:
Base
  • Object
show all
Defined in:
lib/shadcn_phlexcomponents/components/date_range_picker.rb

Constant Summary

Constants inherited from Base

Base::SANITIZER_ALLOWED_ATTRIBUTES, Base::SANITIZER_ALLOWED_TAGS, Base::TAILWIND_MERGER

Instance Method Summary collapse

Methods inherited from Base

#before_template, #convert_collection_hash_to_struct, #find_as_child, #icon, #item_disabled?, #merge_default_attributes, #merged_as_child_attributes, #nokogiri_attributes_to_hash, #overlay, #sanitize_as_child

Constructor Details

#initialize(name: nil, value: nil, id: nil, format: "DD/MM/YYYY", select_only: false, placeholder: nil, disabled: false, options: {}, mask: true, **attributes) ⇒ DateRangePicker

Returns a new instance of DateRangePicker.



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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/shadcn_phlexcomponents/components/date_range_picker.rb', line 14

def initialize(
  name: nil,
  value: nil,
  id: nil,
  format: "DD/MM/YYYY",
  select_only: false,
  placeholder: nil,
  disabled: false,
  options: {},
  mask: true,
  **attributes
)
  if name && !name.is_a?(Array)
    raise ArgumentError, "Expected an array for \"name\", got #{name.class}"
  end

  if value && !value.is_a?(Array)
    raise ArgumentError, "Expected an array for \"value\", got #{value.class}"
  end

  if value
    value = value.map do |v|
      if v.is_a?(String)
        begin
          # Use Time.zone.parse to ensure consistent timezone handling
          Time.zone ? Time.zone.parse(v) : Time.parse(v)
        rescue
          nil
        end
      else
        v
      end
    end
  end

  @name = name ? name[0] : nil
  @end_name = name ? name[1] : nil
  @value = (value ? value[0] : nil)&.utc&.iso8601
  @end_value = (value ? value[1] : nil)&.utc&.iso8601
  @id = id
  @format = format
  @select_only = select_only
  @placeholder = placeholder
  @disabled = disabled
  @mask = mask
  @aria_id = "date-range-picker-#{SecureRandom.hex(5)}"
  @options = options
  super(**attributes)
end

Instance Method Details

#default_attributes ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/shadcn_phlexcomponents/components/date_range_picker.rb', line 64

def default_attributes
  {
    data: {
      controller: "date-range-picker",
      value: @value,
      end_value: @end_value,
      format: @format,
      options: @options.to_json,
      mask: @mask.to_s,
    },
  }
end

#view_template ⇒ Object



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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/shadcn_phlexcomponents/components/date_range_picker.rb', line 77

def view_template(&)
  div(**@attributes) do
    overlay("date-range-picker")

    input(
      type: :hidden,
      name: @name,
      value: @value,
      data: { date_range_picker_target: "hiddenInput" },
    )

    input(
      type: :hidden,
      name: @end_name,
      value: @end_value,
      data: { date_range_picker_target: "endHiddenInput" },
    )

    if @select_only
      # For select_only date picker, id is passed to button so that clicking on its
      # label will trigger the popover to appear
      DatePickerTrigger(
        disabled: @disabled,
        select_only: @select_only,
        id: @id,
        placeholder: @placeholder,
        stimulus_controller_name: "date-range-picker",
        aria_id: @aria_id,
      )
    else

      DatePickerInputContainer(disabled: @disabled, stimulus_controller_name: "date-range-picker") do
        DatePickerInput(id: @id, placeholder: @placeholder, format: "#{@format} - #{@format}", disabled: @disabled, stimulus_controller_name: "date-range-picker", aria_id: @aria_id)

        DatePickerTrigger(
          disabled: @disabled,
          select_only: @select_only,
          placeholder: @placeholder,
          stimulus_controller_name: "date-range-picker",
          aria_id: @aria_id,
        )
      end
    end

    DatePickerContent(stimulus_controller_name: "date-range-picker", aria_id: @aria_id)
  end
end