Class: NitroKit::FormBuilder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- NitroKit::FormBuilder
- Defined in:
- app/components/nitro_kit/form_builder.rb
Constant Summary collapse
- FIELD_TYPES =
{ color_field: :color, date_field: :date, datetime_field: :datetime_local, datetime_local_field: :datetime_local, email_field: :email, file_field: :file, hidden_field: :hidden, month_field: :month, number_field: :number, password_field: :password, phone_field: :tel, range_field: :range, search_field: :search, telephone_field: :tel, text_area: :textarea, text_field: :text, time_field: :time, url_field: :url, week_field: :week }.freeze
- CONTROL_OPTIONS =
%i[ id name value placeholder disabled readonly required autocomplete checked multiple accept min max step minlength maxlength rows cols wrap pattern inputmode ].freeze
- UNSUPPORTED_HELPERS =
{ label: "form.field(:attribute, label: \"...\")", collection_select: "form.field(:attribute, as: :select, options: ...)", grouped_collection_select: "form.field(:attribute, as: :select, option_tags: ...)", collection_radio_buttons: "form.field(:attribute, as: :radio_group, options: ...)", collection_check_boxes: "NitroKit::CheckboxGroup through form.field(:attribute, as: :checkbox)", date_select: "form.field(:attribute, as: :date)", time_zone_select: "form.field(:attribute, as: :select, options: ...)" }.freeze
Instance Method Summary collapse
- #button(value = nil, options = {}, **attributes, &block) ⇒ Object
- #check_box(field_name, options = {}, checked_value = "1", unchecked_value = "0", **attributes) ⇒ Object (also: #checkbox)
- #dropzone(field_name, id: nil, label: I18n.t("nitro_kit.dropzone.label"), description: nil, presentation: :input, direct_upload: true, multiple: false, accept: nil, max_files: 1, max_bytes: nil, disabled: false, required: false, html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ Object
- #field(field_name, as: :string, label: nil, errors: nil, html: {}, aria: {}, data: {}, control_html: {}, control_aria: {}, control_data: {}, wrapper_html: {}, wrapper_aria: {}, wrapper_data: {}, **attributes, &block) ⇒ Object
- #fieldset(**attributes, &block) ⇒ Object
- #group(**attributes, &block) ⇒ Object
- #hidden_field(field_name, options = {}, **attributes) ⇒ Object
- #radio_button(field_name, tag_value = "1", options = {}, **attributes) ⇒ Object
- #select(field_name, choices = nil, options = {}, html_options = {}, &block) ⇒ Object
- #submit(value = nil, options = {}, **attributes, &block) ⇒ Object
Instance Method Details
#button(value = nil, options = {}, **attributes, &block) ⇒ Object
234 235 236 237 238 239 240 |
# File 'app/components/nitro_kit/form_builder.rb', line 234 def (value = nil, = {}, **attributes, &block) = .symbolize_keys.merge(attributes) value = I18n.t("nitro_kit.form.submit") if value.nil? && !block [:type] = :submit unless .key?(:type) @template.render(Button.new(value, **), &block) end |
#check_box(field_name, options = {}, checked_value = "1", unchecked_value = "0", **attributes) ⇒ Object Also known as: checkbox
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'app/components/nitro_kit/form_builder.rb', line 200 def check_box( field_name, = {}, checked_value = "1", unchecked_value = "0", **attributes ) = .merge(attributes).symbolize_keys include_hidden = .delete(:include_hidden) { true } = () [:control_aria] = accessible_control_aria(field_name, [:control_aria]) field( field_name, as: :checkbox, label: false, checked_value:, unchecked_value:, include_hidden:, ** ) end |
#dropzone(field_name, id: nil, label: I18n.t("nitro_kit.dropzone.label"), description: nil, presentation: :input, direct_upload: true, multiple: false, accept: nil, max_files: 1, max_bytes: nil, disabled: false, required: false, html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'app/components/nitro_kit/form_builder.rb', line 114 def dropzone( field_name, id: nil, label: I18n.t("nitro_kit.dropzone.label"), description: nil, presentation: :input, direct_upload: true, multiple: false, accept: nil, max_files: 1, max_bytes: nil, disabled: false, required: false, html: {}, aria: {}, data: {}, desperately_need_a_class: nil ) self.multipart = true @template.render( Dropzone.new( id: id || field_id(field_name), name: self.field_name(field_name, multiple:), label:, description:, presentation:, direct_upload:, multiple:, accept:, max_files:, max_bytes:, disabled:, required:, html:, aria:, data:, desperately_need_a_class: ) ) end |
#field(field_name, as: :string, label: nil, errors: nil, html: {}, aria: {}, data: {}, control_html: {}, control_aria: {}, control_data: {}, wrapper_html: {}, wrapper_aria: {}, wrapper_data: {}, **attributes, &block) ⇒ Object
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 |
# File 'app/components/nitro_kit/form_builder.rb', line 51 def field( field_name, as: :string, label: nil, errors: nil, html: {}, aria: {}, data: {}, control_html: {}, control_aria: {}, control_data: {}, wrapper_html: {}, wrapper_aria: {}, wrapper_data: {}, **attributes, &block ) as = validate_as!(as) label = default_label_for(field_name) if label.nil? errors ||= errors_for(field_name) control_html = merge_control_boundary(:html, control_html, html) control_aria = merge_control_boundary(:aria, control_aria, aria) control_data = merge_control_boundary(:data, control_data, data) if as == :rich_text return rich_text_field( field_name, label:, errors:, control_html:, control_aria:, control_data:, html: wrapper_html, aria: wrapper_aria, data: wrapper_data, **attributes ) end self.multipart = true if as == :file @template.render( NitroKit::Field.new( self, field_name, as:, label:, errors:, html: wrapper_html, aria: wrapper_aria, data: wrapper_data, control_html:, control_aria:, control_data:, **attributes ), &block ) end |
#fieldset(**attributes, &block) ⇒ Object
47 48 49 |
# File 'app/components/nitro_kit/form_builder.rb', line 47 def fieldset(**attributes, &block) @template.render(NitroKit::Fieldset.new(**attributes), &block) end |
#group(**attributes, &block) ⇒ Object
110 111 112 |
# File 'app/components/nitro_kit/form_builder.rb', line 110 def group(**attributes, &block) @template.render(NitroKit::FieldGroup.new(**attributes), &block) end |
#hidden_field(field_name, options = {}, **attributes) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'app/components/nitro_kit/form_builder.rb', line 168 def hidden_field(field_name, = {}, **attributes) = .merge(attributes).symbolize_keys value = .key?(:value) ? .delete(:value) : value_for(field_name) @template.render( Input.new( type: :hidden, id: .delete(:id) || field_id(field_name), name: .delete(:name) || self.field_name(field_name), value:, disabled: .delete(:disabled) { false }, autocomplete: .delete(:autocomplete), data: .delete(:data) || {}, aria: .delete(:aria) || {}, html: ) ) end |
#radio_button(field_name, tag_value = "1", options = {}, **attributes) ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'app/components/nitro_kit/form_builder.rb', line 187 def (field_name, tag_value = "1", = {}, **attributes) = (.merge(attributes)) [:control_aria] = accessible_control_aria(field_name, [:control_aria]) field( field_name, as: :radio_button, label: false, options: [ [ tag_value, tag_value ] ], ** ) end |
#select(field_name, choices = nil, options = {}, html_options = {}, &block) ⇒ Object
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'app/components/nitro_kit/form_builder.rb', line 242 def select(field_name, choices = nil, = {}, = {}, &block) = .symbolize_keys = @template.capture(&block) if block ||= choices if choices.is_a?(ActiveSupport::SafeBuffer) prompt = [:prompt] == true ? I18n.t("helpers.select.prompt", default: "Please select") : [:prompt] selected = .key?(:selected) ? { value: [:selected] } : {} field( field_name, as: :select, options: ? nil : choices, option_tags:, include_blank: [:include_blank], prompt:, label: false, **selected, **() ) end |
#submit(value = nil, options = {}, **attributes, &block) ⇒ Object
225 226 227 228 229 230 231 232 |
# File 'app/components/nitro_kit/form_builder.rb', line 225 def submit(value = nil, = {}, **attributes, &block) = .symbolize_keys.merge(attributes) value = I18n.t("nitro_kit.form.submit") if value.nil? && !block [:name] = "commit" unless .key?(:name) [:value] = value if value && [:name] == "commit" && !.key?(:value) @template.render(Button.new(value, variant: :primary, type: :submit, **), &block) end |