Class: HakumiComponents::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Extended by:
T::Sig
Defined in:
app/form_builders/hakumi_components/form_builder/contracts.rb,
app/form_builders/hakumi_components/form_builder.rb

Overview

typed: strict frozen_string_literal: true

Defined Under Namespace

Modules: Contracts Classes: FieldContext, ModelBinding

Instance Method Summary collapse

Constructor Details

#initialize(object_name, object, template, options) ⇒ FormBuilder

Returns a new instance of FormBuilder.



16
17
18
19
20
21
22
# File 'app/form_builders/hakumi_components/form_builder.rb', line 16

def initialize(object_name, object, template, options)
  @object_name = T.let(object_name&.to_s, T.nilable(String))
  @object = T.let(object, T.nilable(ActiveModel::Model))
  @template = T.let(template, ActionView::Helpers::RenderingHelper)
  @model_binding = T.let(nil, T.nilable(HakumiComponents::FormBuilder::ModelBinding))
  super
end

Instance Method Details

#autocomplete_field(method, choices = nil, **options) ⇒ Object



179
180
181
182
183
184
185
186
187
# File 'app/form_builders/hakumi_components/form_builder.rb', line 179

def autocomplete_field(method, choices = nil, **options)
  render_collection_field(
    HakumiComponents::Autocomplete::Component,
    method,
    :options,
    HakumiComponents::SelectionControl::Coercion.entries(ensure_selection_entry_array(choices)),
    options
  )
end

#cascader_field(method, choices = nil, **options) ⇒ Object



196
197
198
199
200
201
202
203
204
# File 'app/form_builders/hakumi_components/form_builder.rb', line 196

def cascader_field(method, choices = nil, **options)
  render_collection_field(
    HakumiComponents::Cascader::Component,
    method,
    :options,
    choices || [],
    options
  )
end

#check_box(method, **options) ⇒ Object



133
134
135
# File 'app/form_builders/hakumi_components/form_builder.rb', line 133

def check_box(method, **options)
  render_checked_field(HakumiComponents::Switch::Component, method, options)
end

#checkbox_field(method, **options) ⇒ Object



207
208
209
# File 'app/form_builders/hakumi_components/form_builder.rb', line 207

def checkbox_field(method, **options)
  render_checked_field(HakumiComponents::Checkbox::Component, method, options)
end

#collection_radio_buttons(method, collection, value_method, text_method, **options) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/form_builders/hakumi_components/form_builder.rb', line 115

def collection_radio_buttons(method, collection, value_method, text_method, **options)
  enhance_options_with_introspection!(method, options)

  radio_options = collection.map do |item|
    HakumiComponents::Radio::Group::Option.new(
      label: item.send(text_method),
      value: item.send(value_method)
    )
  end

  render_form_field(
    HakumiComponents::Radio::Group::Component,
    method,
    options.merge(options: radio_options)
  )
end

#color_picker_field(method, **options) ⇒ Object



212
213
214
# File 'app/form_builders/hakumi_components/form_builder.rb', line 212

def color_picker_field(method, **options)
  render_simple_field(HakumiComponents::ColorPicker::Component, method, options)
end

#date_picker(method, **options) ⇒ Object



157
158
159
# File 'app/form_builders/hakumi_components/form_builder.rb', line 157

def date_picker(method, **options)
  render_simple_field(HakumiComponents::DatePicker::Component, method, options)
end

#mentions_field(method, **options) ⇒ Object



162
163
164
165
166
167
168
169
170
# File 'app/form_builders/hakumi_components/form_builder.rb', line 162

def mentions_field(method, **options)
  render_collection_field(
    HakumiComponents::Mentions::Component,
    method,
    :options,
    HakumiComponents::Mentions::Coercion.options(ensure_mentions_array(options.delete(:options))),
    options
  )
end

#number_field(method, **options) ⇒ Object



40
41
42
# File 'app/form_builders/hakumi_components/form_builder.rb', line 40

def number_field(method, **options)
  render_simple_field(HakumiComponents::InputNumber::Component, method, options)
end

#password_field(method, **options) ⇒ Object



30
31
32
# File 'app/form_builders/hakumi_components/form_builder.rb', line 30

def password_field(method, **options)
  render_simple_field(HakumiComponents::Input::Password::Component, method, options)
end

#radio_button(method, tag_value, **options) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/form_builders/hakumi_components/form_builder.rb', line 85

def radio_button(method, tag_value, **options)
  enhance_options_with_introspection!(method, options)

  id_suffix = options[:id] ? nil : tag_value.to_s
  config = field_configuration(method, options, id_suffix: id_suffix)

  option_id = options.delete(:id)
  config[:id] = option_id if option_id.is_a?(String) || option_id.is_a?(Symbol)

  checked = config[:value].to_s == tag_value.to_s
  config[:value] = tag_value
  config[:checked] = checked

  render_form_field(
    HakumiComponents::Radio::Component,
    method,
    options,
    config
  )
end

#rate_field(method, **options) ⇒ Object



138
139
140
# File 'app/form_builders/hakumi_components/form_builder.rb', line 138

def rate_field(method, **options)
  render_simple_field(HakumiComponents::Rate::Component, method, options)
end

#select(method, choices = nil, **options) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'app/form_builders/hakumi_components/form_builder.rb', line 51

def select(method, choices = nil, **options)
  render_collection_field(
    HakumiComponents::Select::Component,
    method,
    :options,
    HakumiComponents::SelectionControl::Coercion.options(ensure_flat_selection_array(choices)),
    options
  )
end

#slider_field(method, **options) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
# File 'app/form_builders/hakumi_components/form_builder.rb', line 143

def slider_field(method, **options)
  enhance_options_with_introspection!(method, options)

  config = field_configuration(method, options)

  render_form_field(
    HakumiComponents::Slider::Component,
    method,
    options,
    config
  )
end

#submit(value = nil, **options) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'app/form_builders/hakumi_components/form_builder.rb', line 243

def submit(value = nil, **options)
  value ||= submit_default_value

  button_type = options.delete(:type) || :primary
  button_size = options.delete(:size) || :default
  block = options.delete(:block) || false
  loading = options.delete(:loading) || false
  danger = options.delete(:danger) || false

  @template.render(HakumiComponents::Button::Component.new(
    type: button_type,
    size: button_size,
    html_type: :submit,
    block: block,
    loading: loading,
    danger: danger,
    **options
  )) { value }
end

#text_area(method, **options) ⇒ Object



35
36
37
# File 'app/form_builders/hakumi_components/form_builder.rb', line 35

def text_area(method, **options)
  render_simple_field(HakumiComponents::Input::TextArea::Component, method, options)
end

#text_field(method, **options) ⇒ Object



25
26
27
# File 'app/form_builders/hakumi_components/form_builder.rb', line 25

def text_field(method, **options)
  render_simple_field(HakumiComponents::Input::Component, method, options)
end

#time_picker_field(method, **options) ⇒ Object



217
218
219
# File 'app/form_builders/hakumi_components/form_builder.rb', line 217

def time_picker_field(method, **options)
  render_simple_field(HakumiComponents::TimePicker::Component, method, options)
end

#transfer_field(method, data_source = nil, **options) ⇒ Object



222
223
224
225
226
227
228
229
230
231
# File 'app/form_builders/hakumi_components/form_builder.rb', line 222

def transfer_field(method, data_source = nil, **options)
  disabled = options[:disabled] == true
  render_collection_field(
    HakumiComponents::Transfer::Component,
    method,
    :data_source,
    HakumiComponents::Transfer::Coercion.items(Array(data_source), disabled: disabled),
    options
  )
end

#tree_select(method, choices = nil, **options) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'app/form_builders/hakumi_components/form_builder.rb', line 68

def tree_select(method, choices = nil, **options)
  render_collection_field(
    HakumiComponents::TreeSelect::Component,
    method,
    :options,
    choices || [],
    options
  )
end

#upload_field(method, **options) ⇒ Object

Raises:

  • (ArgumentError)


234
235
236
237
238
239
240
# File 'app/form_builders/hakumi_components/form_builder.rb', line 234

def upload_field(method, **options)
  enhance_options_with_introspection!(method, options)

  raise ArgumentError, "action parameter is required for upload_field" unless options[:action]

  render_form_field(HakumiComponents::Upload::Component, method, options)
end