Class: IronAdmin::Form::TextareaComponent

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

Overview

Renders a textarea field.

Examples:

Basic textarea

render IronAdmin::Form::TextareaComponent.new(
  name: "record[description]",
  value: @record.description,
  rows: 6
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, value: nil, rows: 4, placeholder: nil, disabled: false, readonly: false, has_error: false, field: nil, current_user: nil) ⇒ TextareaComponent

Returns a new instance of TextareaComponent.

Parameters:

  • name (String)

    Textarea name

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

    Current value

  • rows (Integer) (defaults to: 4)

    Number of rows (default: 4)

  • 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



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

def initialize(name:, value: nil, rows: 4, placeholder: nil,
               disabled: false, readonly: false, has_error: false,
               field: nil, current_user: nil)
  @name = name
  @value = value
  @rows = rows
  @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 textarea has error state.

Returns:

  • (Boolean)

    Whether textarea has error state



32
33
34
# File 'app/components/iron_admin/form/textarea_component.rb', line 32

def has_error
  @has_error
end

#nameString (readonly)

Returns Textarea name attribute.

Returns:

  • (String)

    Textarea name attribute



17
18
19
# File 'app/components/iron_admin/form/textarea_component.rb', line 17

def name
  @name
end

#placeholderString? (readonly)

Returns Placeholder text.

Returns:

  • (String, nil)

    Placeholder text



26
27
28
# File 'app/components/iron_admin/form/textarea_component.rb', line 26

def placeholder
  @placeholder
end

#readonlyBoolean (readonly)

Returns Whether textarea is read-only.

Returns:

  • (Boolean)

    Whether textarea is read-only



29
30
31
# File 'app/components/iron_admin/form/textarea_component.rb', line 29

def readonly
  @readonly
end

#rowsInteger (readonly)

Returns Number of rows.

Returns:

  • (Integer)

    Number of rows



23
24
25
# File 'app/components/iron_admin/form/textarea_component.rb', line 23

def rows
  @rows
end

#valueString? (readonly)

Returns Current value.

Returns:

  • (String, nil)

    Current value



20
21
22
# File 'app/components/iron_admin/form/textarea_component.rb', line 20

def value
  @value
end

Instance Method Details

#callString

Renders the textarea.

Returns:

  • (String)

    HTML content



69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/components/iron_admin/form/textarea_component.rb', line 69

def call
  tag.textarea(
    value,
    name: name,
    id: name,
    rows: rows,
    placeholder: placeholder,
    disabled: effectively_disabled?,
    readonly: readonly,
    class: textarea_classes
  )
end

#textarea_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 textarea element.

Returns:

  • (String)

    CSS classes for textarea element



59
60
61
62
63
64
65
# File 'app/components/iron_admin/form/textarea_component.rb', line 59

def textarea_classes
  base = "block w-full border px-3 py-2 text-sm shadow-sm outline-none transition duration-150 ease-in-out " \
         "resize-y #{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