Class: NitroKit::Field
- Defined in:
- app/components/nitro_kit/field.rb
Constant Summary collapse
- INPUT_TYPES =
%i[ button color date datetime datetime_local email file hidden month number password range search string tel text time url week ].freeze
- TYPES =
( INPUT_TYPES + %i[select combobox textarea rich_text checkbox radio radio_button radio_group switch] ).freeze
Constants inherited from Component
Component::ADDITIVE_DATA_ATTRIBUTES, Component::COMPONENT_OWNED_DATA_ATTRIBUTES, Component::FORBIDDEN_ATTRIBUTES, Component::RESERVED_DATA_ATTRIBUTES
Instance Attribute Summary collapse
-
#as ⇒ Object
readonly
Returns the value of attribute as.
-
#field_description ⇒ Object
readonly
Returns the value of attribute field_description.
-
#field_error_messages ⇒ Object
readonly
Returns the value of attribute field_error_messages.
-
#field_label ⇒ Object
readonly
Returns the value of attribute field_label.
-
#form ⇒ Object
readonly
Returns the value of attribute form.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #control(html: {}, aria: {}, data: {}) ⇒ Object
- #description(text = nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil, &block) ⇒ Object
- #errors(error_messages = nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ Object
-
#initialize(form = nil, field_name = nil, as: :string, label: nil, description: nil, errors: nil, options: nil, option_tags: nil, include_blank: nil, prompt: nil, id: nil, name: nil, value: UNSET, placeholder: nil, disabled: false, readonly: false, required: false, autocomplete: nil, checked: nil, indeterminate: false, multiple: false, accept: nil, min: nil, max: nil, step: nil, minlength: nil, maxlength: nil, rows: nil, cols: nil, wrap: nil, pattern: nil, inputmode: nil, checked_value: "1", unchecked_value: "0", include_hidden: true, html: {}, aria: {}, data: {}, control_html: {}, control_aria: {}, control_data: {}, rich_text_content: nil, desperately_need_a_class: nil) ⇒ Field
constructor
A new instance of Field.
- #label(text = nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil, &block) ⇒ Object
- #view_template(&block) ⇒ Object
Constructor Details
#initialize(form = nil, field_name = nil, as: :string, label: nil, description: nil, errors: nil, options: nil, option_tags: nil, include_blank: nil, prompt: nil, id: nil, name: nil, value: UNSET, placeholder: nil, disabled: false, readonly: false, required: false, autocomplete: nil, checked: nil, indeterminate: false, multiple: false, accept: nil, min: nil, max: nil, step: nil, minlength: nil, maxlength: nil, rows: nil, cols: nil, wrap: nil, pattern: nil, inputmode: nil, checked_value: "1", unchecked_value: "0", include_hidden: true, html: {}, aria: {}, data: {}, control_html: {}, control_aria: {}, control_data: {}, rich_text_content: nil, desperately_need_a_class: nil) ⇒ Field
Returns a new instance of Field.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'app/components/nitro_kit/field.rb', line 15 def initialize( form = nil, field_name = nil, as: :string, label: nil, description: nil, errors: nil, options: nil, option_tags: nil, include_blank: nil, prompt: nil, id: nil, name: nil, value: UNSET, placeholder: nil, disabled: false, readonly: false, required: false, autocomplete: nil, checked: nil, indeterminate: false, multiple: false, accept: nil, min: nil, max: nil, step: nil, minlength: nil, maxlength: nil, rows: nil, cols: nil, wrap: nil, pattern: nil, inputmode: nil, checked_value: "1", unchecked_value: "0", include_hidden: true, html: {}, aria: {}, data: {}, control_html: {}, control_aria: {}, control_data: {}, rich_text_content: nil, desperately_need_a_class: nil ) @form = form @field_name = field_name&.to_s @as = validate_choice!(:as, as.to_s.tr("-", "_").to_sym, TYPES) @multiple = validate_boolean!(:multiple, multiple) @id = id || form&.field_id(field_name) || default_field_id @name = name || form&.field_name(field_name, multiple: @multiple) || default_field_name @explicit_value = value @field_label = label.nil? ? derived_label : label @field_description = description @field_error_messages = Array(errors).compact @options = @option_tags = @include_blank = include_blank @prompt = prompt @placeholder = placeholder @disabled = validate_boolean!(:disabled, disabled) @readonly = validate_boolean!(:readonly, readonly) @required = validate_boolean!(:required, required) @autocomplete = autocomplete @checked = validate_boolean!(:checked, checked, allow_nil: true) @indeterminate = validate_boolean!(:indeterminate, indeterminate) @accept = accept @min = min @max = max @step = step @minlength = minlength @maxlength = maxlength @rows = rows @cols = cols @wrap = wrap @pattern = pattern @inputmode = inputmode @checked_value = checked_value @unchecked_value = unchecked_value @include_hidden = validate_boolean!(:include_hidden, include_hidden) @control_html = control_html @control_aria = control_aria @control_data = control_data @rich_text_content = rich_text_content if @options.is_a?(ActiveSupport::SafeBuffer) raise ArgumentError, "pass captured option markup through option_tags:" end if @as != :textarea && !(@rows.nil? && @cols.nil? && @wrap.nil?) raise ArgumentError, "rows:, cols:, and wrap: require as: :textarea" end if @as == :radio_group && @field_label == false raise ArgumentError, "as: :radio_group requires a legend; pass label: with the group name" end super( component: :field, attributes: { data: { field_type: @as.to_s.tr("_", "-"), state: @field_error_messages.any? ? "invalid" : nil, required: @required ? "true" : nil, disabled: @disabled ? "true" : nil }.compact }, html:, aria:, data:, desperately_need_a_class: ) end |
Instance Attribute Details
#as ⇒ Object (readonly)
Returns the value of attribute as.
127 128 129 |
# File 'app/components/nitro_kit/field.rb', line 127 def as @as end |
#field_description ⇒ Object (readonly)
Returns the value of attribute field_description.
127 128 129 |
# File 'app/components/nitro_kit/field.rb', line 127 def field_description @field_description end |
#field_error_messages ⇒ Object (readonly)
Returns the value of attribute field_error_messages.
127 128 129 |
# File 'app/components/nitro_kit/field.rb', line 127 def @field_error_messages end |
#field_label ⇒ Object (readonly)
Returns the value of attribute field_label.
127 128 129 |
# File 'app/components/nitro_kit/field.rb', line 127 def field_label @field_label end |
#form ⇒ Object (readonly)
Returns the value of attribute form.
127 128 129 |
# File 'app/components/nitro_kit/field.rb', line 127 def form @form end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
127 128 129 |
# File 'app/components/nitro_kit/field.rb', line 127 def id @id end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
127 128 129 |
# File 'app/components/nitro_kit/field.rb', line 127 def name @name end |
Instance Method Details
#control(html: {}, aria: {}, data: {}) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'app/components/nitro_kit/field.rb', line 189 def control(html: {}, aria: {}, data: {}) case as when *INPUT_TYPES input_control(html:, aria:, data:) when :select select_control(html:, aria:, data:) when :combobox combobox_control(html:, aria:, data:) when :textarea textarea_control(html:, aria:, data:) when :rich_text rich_text_control(html:, aria:, data:) when :checkbox checkbox_control(html:, aria:, data:) when :switch switch_control(html:, aria:, data:) when :radio, :radio_button (html:, aria:, data:) when :radio_group radio_group_control(html:, aria:, data:) end end |
#description(text = nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil, &block) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'app/components/nitro_kit/field.rb', line 155 def description(text = nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil, &block) text = field_description if text.nil? return unless !text.nil? || block div( **slot_attributes( :description, attributes: { id: description_id }.compact, html:, aria:, data:, desperately_need_a_class: ) ) { text_or_block(text, &block) } end |
#errors(error_messages = nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'app/components/nitro_kit/field.rb', line 171 def errors( = nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil) = .nil? ? : Array().compact return if .empty? ul( **slot_attributes( :error, attributes: { id: errors_id, role: "alert" }.compact, html:, aria:, data:, desperately_need_a_class: ) ) do .each { || li { plain(.to_s) } } end end |
#label(text = nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil, &block) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'app/components/nitro_kit/field.rb', line 135 def label(text = nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil, &block) text = field_label if text.nil? return if text == false return unless !text.nil? || block render_in_slot( Label.new( text, for: label_target_id, id: label_element_id, html:, aria:, data:, desperately_need_a_class: ), :label, &block ) end |
#view_template(&block) ⇒ Object
129 130 131 132 133 |
# File 'app/components/nitro_kit/field.rb', line 129 def view_template(&block) div(**root_attributes) do block ? yield : default_field end end |