Class: NitroKit::Field

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

Constant Summary collapse

FIELD =
[
  "flex flex-col gap-2 align-start",
  "[&:has([data-slot='error'])_[data-slot='label']]:text-destructive",
  "[&:has([data-slot='error'])_[data-slot='control']]:border-destructive"
].freeze
DESCRIPTION =
"text-sm text-muted-foreground"
ERROR =
"text-sm text-destructive"

Instance Attribute Summary collapse

Attributes inherited from Component

#attrs

Instance Method Summary collapse

Methods inherited from Component

#data_merge, merge, #merge

Constructor Details

#initialize(name, as: :string, label: nil, description: nil, errors: nil, **attrs) ⇒ Field

Returns a new instance of Field.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/components/nitro_kit/field.rb', line 12

def initialize(
  name,
  as: :string,
  label: nil,
  description: nil,
  errors: nil,
  **attrs
)
  super(**attrs)

  @name = name
  @as = as.to_sym

  @field_attrs = attrs
  @field_label = label || name.to_s.humanize
  @field_description = description
  @field_error_messages = errors
end

Instance Attribute Details

#asObject (readonly)

Returns the value of attribute as.



31
32
33
# File 'app/components/nitro_kit/field.rb', line 31

def as
  @as
end

#field_attrsObject (readonly)

Returns the value of attribute field_attrs.



31
32
33
# File 'app/components/nitro_kit/field.rb', line 31

def field_attrs
  @field_attrs
end

#field_descriptionObject (readonly)

Returns the value of attribute field_description.



31
32
33
# File 'app/components/nitro_kit/field.rb', line 31

def field_description
  @field_description
end

#field_error_messagesObject (readonly)

Returns the value of attribute field_error_messages.



31
32
33
# File 'app/components/nitro_kit/field.rb', line 31

def field_error_messages
  @field_error_messages
end

#field_labelObject (readonly)

Returns the value of attribute field_label.



31
32
33
# File 'app/components/nitro_kit/field.rb', line 31

def field_label
  @field_label
end

#nameObject (readonly)

Returns the value of attribute name.



31
32
33
# File 'app/components/nitro_kit/field.rb', line 31

def name
  @name
end

Instance Method Details

#control(**override_attrs) ⇒ Object



107
108
109
110
111
112
# File 'app/components/nitro_kit/field.rb', line 107

def control(**override_attrs)
  case as
  when :string
    input(**override_attrs)
  end
end

#description(text = nil, **attrs) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'app/components/nitro_kit/field.rb', line 67

def description(text = nil, **attrs)
  text ||= field_description

  return unless text

  div(
    data: {slot: "description"},
    class: merge(DESCRIPTION, attrs[:class])
  ) { text }
end

#errors(error_messages = nil, **attrs) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/components/nitro_kit/field.rb', line 91

def errors(error_messages = nil, **attrs)
  error_messages ||= field_error_messages

  return unless error_messages&.any?

  ul(
    **attrs,
    data: {slot: "error"},
    class: merge([ERROR, attrs[:class]])
  ) do |msg|
    error_messages.each do |msg|
      li { msg }
    end
  end
end

#input(**attrs) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'app/components/nitro_kit/field.rb', line 80

def input(**attrs)
  render(
    Input.new(
      name:,
      **field_attrs,
      **attrs,
      data: {slot: "control", **data_merge(field_attrs[:data], attrs[:data])}
    )
  )
end

#label(text = nil, **attrs) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/components/nitro_kit/field.rb', line 52

def label(text = nil, **attrs)
  text ||= field_label

  return unless text

  render(
    Label.new(
      **attrs,
      data: data_merge({slot: "label"}, attrs[:data])
    )
  ) do
    text
  end
end

#original_inputObject



78
# File 'app/components/nitro_kit/field.rb', line 78

alias :original_input :input

#original_labelObject



50
# File 'app/components/nitro_kit/field.rb', line 50

alias :original_label :label

#view_templateObject



40
41
42
43
44
45
46
47
48
# File 'app/components/nitro_kit/field.rb', line 40

def view_template
  div(**attrs, class: merge(FIELD, attrs[:class])) do
    if !block_given?
      default_field
    else
      yield
    end
  end
end