Class: IronAdmin::Form::TextInputComponent

Inherits:
ViewComponent::Base
  • Object
show all
Includes:
Concerns::FormInputBehavior
Defined in:
app/components/iron_admin/form/text_input_component.rb

Overview

Renders a text input field.

Examples:

Basic text input

render IronAdmin::Form::TextInputComponent.new(name: "record[name]", value: @record.name)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, value: nil, type: :text, placeholder: nil, disabled: false, readonly: false, has_error: false, field: nil, current_user: nil) ⇒ TextInputComponent

Returns a new instance of TextInputComponent.

Parameters:

  • name (String)

    Input name

  • value (String, nil) (defaults to: nil)

    Current value

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

    Input type (default: :text)

  • placeholder (String, nil) (defaults to: nil)

    Placeholder

  • disabled (Boolean) (defaults to: false)

    Disabled state

  • readonly (Boolean) (defaults to: false)

    Read-only state

  • has_error (Boolean) (defaults to: false)

    Error state

  • field (IronAdmin::Field, nil) (defaults to: nil)

    Field config

  • current_user (Object, nil) (defaults to: nil)

    Current user for visibility checks



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/components/iron_admin/form/text_input_component.rb', line 39

def initialize(name:, value: nil, type: :text, placeholder: nil,
               disabled: false, readonly: false, has_error: false,
               field: nil, current_user: nil)
  @name = name
  @value = value
  @type = type
  @placeholder = placeholder || name.to_s.humanize
  @disabled = disabled
  @readonly = readonly
  @has_error = has_error
  @field = field
  @current_user = current_user
end

Instance Attribute Details

#has_errorBoolean (readonly)

Returns Whether input has error state.

Returns:

  • (Boolean)

    Whether input has error state



28
29
30
# File 'app/components/iron_admin/form/text_input_component.rb', line 28

def has_error
  @has_error
end

#nameString (readonly)

Returns Input name attribute.

Returns:

  • (String)

    Input name attribute



13
14
15
# File 'app/components/iron_admin/form/text_input_component.rb', line 13

def name
  @name
end

#placeholderString? (readonly)

Returns Placeholder text.

Returns:

  • (String, nil)

    Placeholder text



22
23
24
# File 'app/components/iron_admin/form/text_input_component.rb', line 22

def placeholder
  @placeholder
end

#readonlyBoolean (readonly)

Returns Whether input is read-only.

Returns:

  • (Boolean)

    Whether input is read-only



25
26
27
# File 'app/components/iron_admin/form/text_input_component.rb', line 25

def readonly
  @readonly
end

#typeSymbol (readonly)

Returns Input type (:text, :email, :password, etc.).

Returns:

  • (Symbol)

    Input type (:text, :email, :password, etc.)



19
20
21
# File 'app/components/iron_admin/form/text_input_component.rb', line 19

def type
  @type
end

#valueString? (readonly)

Returns Current value.

Returns:

  • (String, nil)

    Current value



16
17
18
# File 'app/components/iron_admin/form/text_input_component.rb', line 16

def value
  @value
end

Instance Method Details

#callString

Renders the text input.

Returns:

  • (String)

    HTML content



65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/components/iron_admin/form/text_input_component.rb', line 65

def call
  tag.input(
    type: type,
    name: name,
    id: name,
    value: value,
    placeholder: placeholder,
    disabled: effectively_disabled?,
    readonly: readonly,
    class: input_classes
  )
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 text input field.

Returns:

  • (String)

    CSS classes for text input field



55
56
57
58
59
60
61
# File 'app/components/iron_admin/form/text_input_component.rb', line 55

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 effectively_disabled? || readonly
  base
end