Class: Keystone::Ui::FormFieldComponent

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/keystone/ui/form_field_component.rb

Constant Summary collapse

WRAPPER_CLASSES =
"space-y-1"
LABEL_CLASSES =
"block text-sm font-medium text-gray-700 dark:text-gray-300"
REQUIRED_CLASSES =
"text-red-500 ml-0.5"
HINT_CLASSES =
"mt-1 text-sm text-gray-500 dark:text-gray-400"
ERROR_CLASSES =
"mt-1 text-sm text-red-600 dark:text-red-400"
CHECKBOX_CLASSES =
"rounded border-gray-300 text-accent-600 focus:ring-accent-500 dark:border-zinc-600 dark:bg-zinc-900"
CHECKBOX_WRAPPER_CLASSES =
"flex items-center gap-2"

Instance Method Summary collapse

Constructor Details

#initialize(attribute:, label: nil, type: :text, required: false, hint: nil, placeholder: nil, min: nil, max: nil, step: nil, value: nil, options: [], errors: []) ⇒ FormFieldComponent

Returns a new instance of FormFieldComponent.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/components/keystone/ui/form_field_component.rb', line 14

def initialize(attribute:, label: nil, type: :text, required: false, hint: nil, placeholder: nil, min: nil, max: nil, step: nil, value: nil, options: [], errors: [])
  @attribute = attribute
  @label = label
  @type = type
  @required = required
  @hint = hint
  @placeholder = placeholder
  @min = min
  @max = max
  @step = step
  @value = value
  @options = options
  @errors = Array(errors)
end

Instance Method Details

#checkbox?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'app/components/keystone/ui/form_field_component.rb', line 57

def checkbox?
  @type == :checkbox
end

#error_messagesObject



49
50
51
# File 'app/components/keystone/ui/form_field_component.rb', line 49

def error_messages
  @errors
end

#errors?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'app/components/keystone/ui/form_field_component.rb', line 45

def errors?
  @errors.any?
end

#hint?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'app/components/keystone/ui/form_field_component.rb', line 37

def hint?
  !@hint.nil?
end

#hint_textObject



41
42
43
# File 'app/components/keystone/ui/form_field_component.rb', line 41

def hint_text
  @hint
end

#input_optionsObject



69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/components/keystone/ui/form_field_component.rb', line 69

def input_options
  options = { name: @attribute.to_s, class: Keystone::Ui::InputComponent::BASE_CLASSES }
  unless textarea?
    options[:type] = Keystone::Ui::InputComponent::TYPE_MAP.fetch(@type)
  end
  options[:placeholder] = @placeholder unless @placeholder.nil?
  options[:value] = @value unless @value.nil?
  options[:min] = @min unless @min.nil?
  options[:max] = @max unless @max.nil?
  options[:step] = @step unless @step.nil?
  options
end

#label_textObject



29
30
31
# File 'app/components/keystone/ui/form_field_component.rb', line 29

def label_text
  @label || @attribute.to_s.tr("_", " ").capitalize
end

#required?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'app/components/keystone/ui/form_field_component.rb', line 33

def required?
  @required
end

#select?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'app/components/keystone/ui/form_field_component.rb', line 61

def select?
  @type == :select
end

#select_optionsObject



65
66
67
# File 'app/components/keystone/ui/form_field_component.rb', line 65

def select_options
  @options
end

#textarea?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'app/components/keystone/ui/form_field_component.rb', line 53

def textarea?
  @type == :textarea
end