Class: ShadcnPhlexcomponents::FormDateRangePicker

Inherits:
Base
  • Object
show all
Includes:
FormHelpers
Defined in:
lib/shadcn_phlexcomponents/components/form/form_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 included from FormHelpers

#aria_attributes, #default_checked, #default_error, #default_value, #describedby, #hint, #hint_attributes, #label, #label_and_hint_container_attributes, #label_attributes, #render_error, #render_hint

Methods inherited from Base

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

Constructor Details

#initialize(method, end_method, model: false, object_name: nil, value: nil, name: nil, id: nil, label: nil, error: nil, hint: nil, **attributes) ⇒ FormDateRangePicker

Returns a new instance of FormDateRangePicker.



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

def initialize(
  method,
  end_method,
  model: false,
  object_name: nil,
  value: nil,
  name: nil,
  id: nil,
  label: nil,
  error: nil,
  hint: nil,
  **attributes
)
  @method = method
  @end_method = end_method
  @model = model
  @object_name = object_name

  @value = [
    default_value(value ? value[0] : nil, method),
    default_value(value ? value[1] : nil, end_method),
  ]
  @error = [
    default_error(error ? error[0] : nil, method),
    default_error(error ? error[1] : nil, end_method),
  ].compact

  @name = name
  @id = id
  @label = label
  @hint = hint
  @aria_id = "form-field-#{SecureRandom.hex(5)}"
  super(**attributes)
end

Instance Method Details

#render_labelObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/shadcn_phlexcomponents/components/form/form_date_range_picker.rb', line 42

def render_label(&)
  # It's currently not possible to separate the content of the yield in Phlex.
  # So we use Javascript to remove the duplicated hint or label.
  if @yield_label && @yield_hint
    div(data: { remove_hint: true }, &)
  elsif @yield_label
    yield
  elsif @label
    attrs = label_attributes(use_label_styles: false)
    Label(**attrs) { @label }
  elsif @label != false
    attrs = label_attributes(use_label_styles: true)
    rails_label(@object_name, [@method, @end_method].to_sentence, nil, **attrs)
  end
end

#view_templateObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/shadcn_phlexcomponents/components/form/form_date_range_picker.rb', line 58

def view_template(&)
  vanish(&)

  @id ||= field_id(@object_name, @method)
  @name ||=
    [
      field_name(@object_name, @method),
      field_name(@object_name, @end_method),
    ]

  FormField(data: label_and_hint_container_attributes) do
    render_label(&)
    DateRangePicker(
      id: @id,
      name: @name,
      value: @value,
      aria: aria_attributes,
      **@attributes,
    )
    render_hint(&)
    render_error
  end
end