Class: NitroKit::Input

Inherits:
Component
  • Object
show all
Defined in:
app/components/nitro_kit/input.rb

Constant Summary collapse

TYPES =
%i[
  button checkbox color date datetime-local email file hidden month number password radio
  range search tel text time url week
].freeze
OWNED_ATTRIBUTES =
%i[
  type id name value placeholder disabled readonly required autocomplete min max step
  minlength maxlength multiple accept pattern inputmode checked
].freeze

Constants inherited from Component

Component::ADDITIVE_DATA_ATTRIBUTES, Component::COMPONENT_OWNED_DATA_ATTRIBUTES, Component::FORBIDDEN_ATTRIBUTES, Component::RESERVED_DATA_ATTRIBUTES

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type: :text, id: nil, name: nil, value: nil, placeholder: nil, disabled: false, readonly: false, required: false, autocomplete: nil, min: nil, max: nil, step: nil, minlength: nil, maxlength: nil, multiple: false, accept: nil, pattern: nil, inputmode: nil, checked: nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ Input

Returns a new instance of Input.



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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/components/nitro_kit/input.rb', line 14

def initialize(
  type: :text,
  id: nil,
  name: nil,
  value: nil,
  placeholder: nil,
  disabled: false,
  readonly: false,
  required: false,
  autocomplete: nil,
  min: nil,
  max: nil,
  step: nil,
  minlength: nil,
  maxlength: nil,
  multiple: false,
  accept: nil,
  pattern: nil,
  inputmode: nil,
  checked: nil,
  html: {},
  aria: {},
  data: {},
  desperately_need_a_class: nil
)
  normalized_type = type.to_s.tr("_", "-").to_sym
  @type = validate_choice!(:type, normalized_type, TYPES)
  disabled = validate_boolean!(:disabled, disabled)
  readonly = validate_boolean!(:readonly, readonly)
  required = validate_boolean!(:required, required)
  multiple = validate_boolean!(:multiple, multiple)
  checked = validate_boolean!(:checked, checked, allow_nil: true)
  minlength = validate_non_negative_integer!(:minlength, minlength)
  maxlength = validate_non_negative_integer!(:maxlength, maxlength)
  if minlength && maxlength && minlength > maxlength
    raise ArgumentError, "minlength cannot exceed maxlength"
  end
  if @type == :file && !value.nil?
    raise ArgumentError, "file inputs never carry a value; omit value:"
  end
  reject_owned_attributes!(html)

  super(
    component: :input,
    attributes: {
      type: @type,
      id:,
      name:,
      value:,
      placeholder:,
      disabled:,
      readonly:,
      required:,
      autocomplete:,
      min:,
      max:,
      step:,
      minlength:,
      maxlength:,
      multiple:,
      accept:,
      pattern:,
      inputmode:,
      checked:
    }.compact,
    html:,
    aria:,
    data:,
    desperately_need_a_class:
  )
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



86
87
88
# File 'app/components/nitro_kit/input.rb', line 86

def type
  @type
end

Instance Method Details

#view_templateObject



88
89
90
# File 'app/components/nitro_kit/input.rb', line 88

def view_template
  input(**root_attributes)
end