Class: Keystone::Ui::FormFieldComponent
- Inherits:
-
ViewComponent::Base
- Object
- ViewComponent::Base
- Keystone::Ui::FormFieldComponent
- 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
- #checkbox? ⇒ Boolean
- #error_messages ⇒ Object
- #errors? ⇒ Boolean
- #hint? ⇒ Boolean
- #hint_text ⇒ Object
-
#initialize(attribute:, label: nil, type: :text, required: false, hint: nil, placeholder: nil, min: nil, max: nil, step: nil, value: nil, options: [], errors: []) ⇒ FormFieldComponent
constructor
A new instance of FormFieldComponent.
- #input_options ⇒ Object
- #label_text ⇒ Object
- #required? ⇒ Boolean
- #select? ⇒ Boolean
- #select_options ⇒ Object
- #textarea? ⇒ Boolean
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 = @errors = Array(errors) end |
Instance Method Details
#checkbox? ⇒ Boolean
57 58 59 |
# File 'app/components/keystone/ui/form_field_component.rb', line 57 def checkbox? @type == :checkbox end |
#error_messages ⇒ Object
49 50 51 |
# File 'app/components/keystone/ui/form_field_component.rb', line 49 def @errors end |
#errors? ⇒ Boolean
45 46 47 |
# File 'app/components/keystone/ui/form_field_component.rb', line 45 def errors? @errors.any? end |
#hint? ⇒ Boolean
37 38 39 |
# File 'app/components/keystone/ui/form_field_component.rb', line 37 def hint? !@hint.nil? end |
#hint_text ⇒ Object
41 42 43 |
# File 'app/components/keystone/ui/form_field_component.rb', line 41 def hint_text @hint end |
#input_options ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'app/components/keystone/ui/form_field_component.rb', line 69 def = { name: @attribute.to_s, class: Keystone::Ui::InputComponent::BASE_CLASSES } unless textarea? [:type] = Keystone::Ui::InputComponent::TYPE_MAP.fetch(@type) end [:placeholder] = @placeholder unless @placeholder.nil? [:value] = @value unless @value.nil? [:min] = @min unless @min.nil? [:max] = @max unless @max.nil? [:step] = @step unless @step.nil? end |
#label_text ⇒ Object
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
33 34 35 |
# File 'app/components/keystone/ui/form_field_component.rb', line 33 def required? @required end |
#select? ⇒ Boolean
61 62 63 |
# File 'app/components/keystone/ui/form_field_component.rb', line 61 def select? @type == :select end |
#select_options ⇒ Object
65 66 67 |
# File 'app/components/keystone/ui/form_field_component.rb', line 65 def @options end |
#textarea? ⇒ Boolean
53 54 55 |
# File 'app/components/keystone/ui/form_field_component.rb', line 53 def textarea? @type == :textarea end |