Class: IronAdmin::Form::DatePickerComponent

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/iron_admin/form/date_picker_component.rb

Overview

Renders a date/time picker input.

Examples:

Date picker

render IronAdmin::Form::DatePickerComponent.new(
  name: "record[birth_date]",
  value: @record.birth_date,
  type: :date
)

Constant Summary collapse

TYPES =

Supported date/time types.

Returns:

  • (Array<Symbol>)
%i[date datetime datetime_local time].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, value: nil, type: :datetime_local, min: nil, max: nil, disabled: false, has_error: false) ⇒ DatePickerComponent

Returns a new instance of DatePickerComponent.

Parameters:

  • name (String)

    Input name

  • value (Date, DateTime, nil) (defaults to: nil)

    Current value

  • type (Symbol) (defaults to: :datetime_local)

    Picker type (default: :datetime_local)

  • min (Date, nil) (defaults to: nil)

    Minimum date

  • max (Date, nil) (defaults to: nil)

    Maximum date

  • disabled (Boolean) (defaults to: false)

    Disabled state

  • has_error (Boolean) (defaults to: false)

    Error state



46
47
48
49
50
51
52
53
54
55
# File 'app/components/iron_admin/form/date_picker_component.rb', line 46

def initialize(name:, value: nil, type: :datetime_local, min: nil, max: nil,
               disabled: false, has_error: false)
  @name = name
  @value = value
  @type = type.to_sym
  @min = min
  @max = max
  @disabled = disabled
  @has_error = has_error
end

Instance Attribute Details

#disabledBoolean (readonly)

Returns Whether input is disabled.

Returns:

  • (Boolean)

    Whether input is disabled



30
31
32
# File 'app/components/iron_admin/form/date_picker_component.rb', line 30

def disabled
  @disabled
end

#has_errorBoolean (readonly)

Returns Whether input has error state.

Returns:

  • (Boolean)

    Whether input has error state



33
34
35
# File 'app/components/iron_admin/form/date_picker_component.rb', line 33

def has_error
  @has_error
end

#maxDate? (readonly)

Returns Maximum date.

Returns:

  • (Date, nil)

    Maximum date



27
28
29
# File 'app/components/iron_admin/form/date_picker_component.rb', line 27

def max
  @max
end

#minDate? (readonly)

Returns Minimum date.

Returns:

  • (Date, nil)

    Minimum date



24
25
26
# File 'app/components/iron_admin/form/date_picker_component.rb', line 24

def min
  @min
end

#nameString (readonly)

Returns Input name attribute.

Returns:

  • (String)

    Input name attribute



15
16
17
# File 'app/components/iron_admin/form/date_picker_component.rb', line 15

def name
  @name
end

#typeSymbol (readonly)

Returns Picker type (:date, :datetime, :datetime_local, :time).

Returns:

  • (Symbol)

    Picker type (:date, :datetime, :datetime_local, :time)



21
22
23
# File 'app/components/iron_admin/form/date_picker_component.rb', line 21

def type
  @type
end

#valueDate, ... (readonly)

Returns Current value.

Returns:

  • (Date, DateTime, nil)

    Current value



18
19
20
# File 'app/components/iron_admin/form/date_picker_component.rb', line 18

def value
  @value
end

Instance Method Details

#callString

Renders the date picker input.

Returns:

  • (String)

    HTML content



102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/components/iron_admin/form/date_picker_component.rb', line 102

def call
  tag.input(
    type: input_type,
    name: name,
    id: name,
    value: formatted_value,
    min: min,
    max: max,
    disabled: disabled,
    class: input_classes
  )
end

#formatted_valueString?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Value formatted for HTML input.

Returns:

  • (String, nil)

    Value formatted for HTML input



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/components/iron_admin/form/date_picker_component.rb', line 85

def formatted_value
  return nil unless value

  case @type
  when :date
    value.respond_to?(:strftime) ? value.strftime("%Y-%m-%d") : value
  when :datetime, :datetime_local
    value.respond_to?(:strftime) ? value.strftime("%Y-%m-%dT%H:%M") : value
  when :time
    value.respond_to?(:strftime) ? value.strftime("%H:%M") : value
  else
    value
  end
end

#input_classesString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns CSS classes for date input field.

Returns:

  • (String)

    CSS classes for date input field



75
76
77
78
79
80
81
# File 'app/components/iron_admin/form/date_picker_component.rb', line 75

def input_classes
  base = "block w-full border px-3 py-2 text-sm shadow-sm outline-none transition duration-150 ease-in-out " \
         "#{theme.border_radius} #{theme.input_border} #{theme.card_bg} #{theme.body_text} #{theme.input_focus}"
  base += " !border-red-400 !focus:border-red-500 !focus:ring-red-500/20" if has_error
  base += " bg-gray-50 cursor-not-allowed" if disabled
  base
end

#input_typeString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns HTML input type attribute value.

Returns:

  • (String)

    HTML input type attribute value



65
66
67
68
69
70
71
# File 'app/components/iron_admin/form/date_picker_component.rb', line 65

def input_type
  case @type
  when :datetime_local then "datetime-local"
  when :datetime then "datetime-local"
  else @type.to_s
  end
end

#themeIronAdmin::Configuration::Theme

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Theme configuration.

Returns:



59
60
61
# File 'app/components/iron_admin/form/date_picker_component.rb', line 59

def theme
  IronAdmin.configuration.theme
end