Class: ShadcnPhlexcomponents::Form

Inherits:
Base
  • Object
show all
Includes:
Phlex::Rails::Helpers::FormWith
Defined in:
lib/shadcn_phlexcomponents/components/form.rb

Constant Summary

Constants inherited from Base

Base::SANITIZER_ALLOWED_ATTRIBUTES, Base::SANITIZER_ALLOWED_TAGS, Base::TAILWIND_MERGER

Instance Method Summary collapse

Methods inherited from Base

#before_template, #convert_collection_hash_to_struct, #default_attributes, #find_as_child, #icon, #item_disabled?, #merge_default_attributes, #merged_as_child_attributes, #nokogiri_attributes_to_hash, #overlay, #sanitize_as_child

Constructor Details

#initialize(model: false, loading: false, **options) ⇒ Form

Returns a new instance of Form.



22
23
24
25
26
27
28
29
30
31
# File 'lib/shadcn_phlexcomponents/components/form.rb', line 22

def initialize(
  model: false,
  loading: false,
  **options
)
  @model = model
  @loading = loading
  @options = options
  @object_name = model ? model.to_model.model_name.param_key : nil
end

Instance Method Details

#checkbox(method = nil, **attributes) ⇒ Object



41
42
43
# File 'lib/shadcn_phlexcomponents/components/form.rb', line 41

def checkbox(method = nil, **attributes, &)
  FormCheckbox(method, model: @model, object_name: @object_name, **attributes, &)
end

#checkbox_group(method = nil, collection = [], value_method:, text_method:, **attributes) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/shadcn_phlexcomponents/components/form.rb', line 49

def checkbox_group(method = nil, collection = [], value_method:, text_method:, **attributes, &)
  FormCheckboxGroup(
    method,
    model: @model,
    object_name: @object_name,
    collection: collection,
    value_method: value_method,
    text_method: text_method,
    **attributes,
    &
  )
end

#combobox(method = nil, collection = [], value_method:, text_method:, **attributes) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/shadcn_phlexcomponents/components/form.rb', line 88

def combobox(method = nil, collection = [], value_method:, text_method:, **attributes, &)
  FormCombobox(
    method,
    model: @model,
    object_name: @object_name,
    collection: collection,
    value_method: value_method,
    text_method: text_method,
    **attributes,
    &
  )
end

#date_picker(method = nil, **attributes) ⇒ Object



101
102
103
# File 'lib/shadcn_phlexcomponents/components/form.rb', line 101

def date_picker(method = nil, **attributes, &)
  FormDatePicker(method, model: @model, object_name: @object_name, **attributes, &)
end

#date_range_picker(method = nil, end_method = nil, **attributes) ⇒ Object



105
106
107
# File 'lib/shadcn_phlexcomponents/components/form.rb', line 105

def date_range_picker(method = nil, end_method = nil, **attributes, &)
  FormDateRangePicker(method, end_method, model: @model, object_name: @object_name, **attributes, &)
end

#input(method = nil, **attributes) ⇒ Object



33
34
35
# File 'lib/shadcn_phlexcomponents/components/form.rb', line 33

def input(method = nil, **attributes, &)
  FormInput(method, model: @model, object_name: @object_name, **attributes, &)
end

#radio_group(method = nil, collection = [], value_method:, text_method:, **attributes) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/shadcn_phlexcomponents/components/form.rb', line 62

def radio_group(method = nil, collection = [], value_method:, text_method:, **attributes, &)
  FormRadioGroup(
    method,
    model: @model,
    object_name: @object_name,
    collection: collection,
    value_method: value_method,
    text_method: text_method,
    **attributes,
    &
  )
end

#select(method = nil, collection = [], value_method:, text_method:, **attributes) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/shadcn_phlexcomponents/components/form.rb', line 75

def select(method = nil, collection = [], value_method:, text_method:, **attributes, &)
  FormSelect(
    method,
    model: @model,
    object_name: @object_name,
    collection: collection,
    value_method: value_method,
    text_method: text_method,
    **attributes,
    &
  )
end

#slider(method = nil, end_method = nil, **attributes) ⇒ Object



109
110
111
# File 'lib/shadcn_phlexcomponents/components/form.rb', line 109

def slider(method = nil, end_method = nil, **attributes, &)
  FormSlider(method, end_method, model: @model, object_name: @object_name, **attributes, &)
end

#submit(value = nil, variant: :default, **attributes) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/shadcn_phlexcomponents/components/form.rb', line 113

def submit(value = nil, variant: :default, **attributes, &)
  if @loading
    LoadingButton(variant: variant, type: :submit, **attributes) do
      if block_given?
        yield
      else
        value || submit_default_value
      end
    end
  else
    Button(variant: variant, type: :submit, **attributes) do
      if block_given?
        yield
      else
        value || submit_default_value
      end
    end
  end
end

#submit_default_valueObject



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/shadcn_phlexcomponents/components/form.rb', line 145

def submit_default_value
  object = @model.respond_to?(:to_model) ? @model.to_model : nil
  key    = if object
    object.persisted? ? :update : :create
  else
    :submit
  end

  model = if object.respond_to?(:model_name)
    object.model_name.human
  else
    @object_name.to_s.humanize
  end

  defaults = []
  # Object is a model and it is not overwritten by as and scope option.
  defaults << if object.respond_to?(:model_name) && @object_name.to_s == model.downcase
    :"helpers.submit.#{object.model_name.i18n_key}.#{key}"
  else
    :"helpers.submit.#{@object_name}.#{key}"
  end
  defaults << :"helpers.submit.#{key}"
  defaults << "#{key.to_s.humanize} #{model}"

  I18n.t(defaults.shift, model: model, default: defaults)
end

#switch(method = nil, **attributes) ⇒ Object



45
46
47
# File 'lib/shadcn_phlexcomponents/components/form.rb', line 45

def switch(method = nil, **attributes, &)
  FormSwitch(method, model: @model, object_name: @object_name, **attributes, &)
end

#textarea(method = nil, **attributes) ⇒ Object



37
38
39
# File 'lib/shadcn_phlexcomponents/components/form.rb', line 37

def textarea(method = nil, **attributes, &)
  FormTextarea(method, model: @model, object_name: @object_name, **attributes, &)
end

#view_templateObject



133
134
135
136
137
138
139
140
141
# File 'lib/shadcn_phlexcomponents/components/form.rb', line 133

def view_template(&)
  @form_class = @options[:class]
  @options[:class] = "#{@options[:class]} #{"group" if @loading}"
  # rubocop:disable Style/ExplicitBlockArgument
  form_with(model: @model, **@options) do
    yield
  end
  # rubocop:enable Style/ExplicitBlockArgument
end