Class: IronAdmin::Form::FieldWrapperComponent

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

Overview

Wraps form fields with label, error messages, and hints.

Examples:

Basic field wrapper

render IronAdmin::Form::FieldWrapperComponent.new(name: :email, errors: @record.errors[:email]) do
  render IronAdmin::Form::TextInputComponent.new(name: "record[email]")
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, label: nil, errors: [], required: false, hint: nil, span_full: false) ⇒ FieldWrapperComponent

Returns a new instance of FieldWrapperComponent.

Parameters:

  • name (Symbol, String)

    Field name

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

    Label text

  • errors (Array<String>) (defaults to: [])

    Error messages

  • required (Boolean) (defaults to: false)

    Required state

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

    Hint text

  • span_full (Boolean) (defaults to: false)

    Span full width



36
37
38
39
40
41
42
43
# File 'app/components/iron_admin/form/field_wrapper_component.rb', line 36

def initialize(name:, label: nil, errors: [], required: false, hint: nil, span_full: false)
  @name = name
  @label = label || name.to_s.humanize
  @errors = Array(errors)
  @required = required
  @hint = hint
  @span_full = span_full
end

Instance Attribute Details

#errorsArray<String> (readonly)

Returns Error messages.

Returns:

  • (Array<String>)

    Error messages



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

def errors
  @errors
end

#hintString? (readonly)

Returns Hint text.

Returns:

  • (String, nil)

    Hint text



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

def hint
  @hint
end

#labelString? (readonly)

Returns Label text.

Returns:

  • (String, nil)

    Label text



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

def label
  @label
end

#nameSymbol, String (readonly)

Returns Field name.

Returns:

  • (Symbol, String)

    Field name



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

def name
  @name
end

#requiredBoolean (readonly)

Returns Whether field is required.

Returns:

  • (Boolean)

    Whether field is required



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

def required
  @required
end

#span_fullBoolean (readonly)

Returns Whether to span full width.

Returns:

  • (Boolean)

    Whether to span full width



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

def span_full
  @span_full
end

Instance Method Details

#error_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 error container.

Returns:

  • (String)

    CSS classes for error container



71
72
73
# File 'app/components/iron_admin/form/field_wrapper_component.rb', line 71

def error_classes
  "flex items-center gap-1.5 mt-1.5"
end

#has_errors?Boolean

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 Whether there are validation errors.

Returns:

  • (Boolean)

    Whether there are validation errors



53
54
55
# File 'app/components/iron_admin/form/field_wrapper_component.rb', line 53

def has_errors?
  errors.any?
end

#label_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 label element.

Returns:

  • (String)

    CSS classes for label element



65
66
67
# File 'app/components/iron_admin/form/field_wrapper_component.rb', line 65

def label_classes
  "block text-sm font-medium #{theme.label_text} mb-1.5"
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:



47
48
49
# File 'app/components/iron_admin/form/field_wrapper_component.rb', line 47

def theme
  IronAdmin.configuration.theme
end

#wrapper_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 wrapper element.

Returns:

  • (String)

    CSS classes for wrapper element



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

def wrapper_classes
  span_full ? "md:col-span-2 2xl:col-span-3" : ""
end