Class: ShadcnPhlexcomponents::DatePicker

Inherits:
Base
  • Object
show all
Defined in:
lib/shadcn_phlexcomponents/components/date_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, id: nil, value: nil, format: "DD/MM/YYYY", select_only: false, placeholder: nil, disabled: false, options: {}, mask: true, **attributes) ⇒ DatePicker

Returns a new instance of DatePicker.



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
# File 'lib/shadcn_phlexcomponents/components/date_picker.rb', line 14

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

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

  @value = value&.utc&.iso8601
  @format = format
  @select_only = select_only
  @placeholder = placeholder
  @disabled = disabled
  @mask = mask
  @aria_id = "date-picker-#{SecureRandom.hex(5)}"
  @options = options
  super(**attributes)
end

Instance Method Details

#default_attributesObject



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/shadcn_phlexcomponents/components/date_picker.rb', line 53

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

#view_templateObject



65
66
67
68
69
70
71
72
73
74
75
76
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
# File 'lib/shadcn_phlexcomponents/components/date_picker.rb', line 65

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

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

    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-picker",
        aria_id: @aria_id,
      )
    else
      DatePickerInputContainer(disabled: @disabled, stimulus_controller_name: "date-picker") do
        DatePickerInput(id: @id, placeholder: @placeholder, format: @format, disabled: @disabled, stimulus_controller_name: "date-picker", aria_id: @aria_id)

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

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