Class: Matestack::Ui::VueJs::Components::Form::Base

Inherits:
Vue
  • Object
show all
Defined in:
lib/matestack/ui/vue_js/components/form/base.rb

Direct Known Subclasses

Checkbox, Input, Radio, Select, Textarea

Instance Method Summary collapse

Methods inherited from Vue

#base_vue_props, #component_uid, #create_children, inherited, #initialize, #matestack_ui_vuejs_ref, #vue_component, vue_name, #vue_name, #vue_props

Constructor Details

This class inherits a constructor from Matestack::Ui::VueJs::Vue

Instance Method Details

#attribute_keyObject



81
82
83
# File 'lib/matestack/ui/vue_js/components/form/base.rb', line 81

def attribute_key
  key.to_s + "#{'[]' if ctx.multiple && ctx.type == :file}"
end

#attributesObject

calculated attributes



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/matestack/ui/vue_js/components/form/base.rb', line 66

def attributes
  (options || {}).merge({
    "matestack-ui-vuejs-ref": matestack_ui_vuejs_ref("input.#{attribute_key}"),
    ":id": id,
    type: ctx.type,
    multiple: ctx.multiple,
    placeholder: ctx.placeholder,
    'v-on:change': change_event,
    'init-value': init_value,
    'v-bind:class': "{ '#{input_error_class}': #{error_key} }",
  }).tap do |attrs|
    attrs[:"#{v_model_type}"] = input_key unless type == :file
  end
end

#change_eventObject



96
97
98
99
100
# File 'lib/matestack/ui/vue_js/components/form/base.rb', line 96

def change_event
  input_changed = "vc.inputChanged('#{attribute_key}');"
  input_changed << "vc.filesAdded('#{attribute_key}');" if type == :file
  input_changed
end

#component_attributesObject



14
15
16
# File 'lib/matestack/ui/vue_js/components/form/base.rb', line 14

def component_attributes
  super.merge("matestack-ui-vuejs-ref": "#{form_context.component_uid}-#{component_id}")
end

#component_idObject



18
19
20
# File 'lib/matestack/ui/vue_js/components/form/base.rb', line 18

def component_id
  # defined in subclass
end

#display_errors?Boolean

error rendering

Returns:

  • (Boolean)


126
127
128
129
130
131
132
# File 'lib/matestack/ui/vue_js/components/form/base.rb', line 126

def display_errors?
  if form_context.ctx.errors == false
    error_config ? true : false
  else
    error_config != false
  end
end

#error_classObject



138
139
140
# File 'lib/matestack/ui/vue_js/components/form/base.rb', line 138

def error_class
  get_from_error_config(:class) || 'error'
end

#error_configObject



40
41
42
# File 'lib/matestack/ui/vue_js/components/form/base.rb', line 40

def error_config
  ctx.errors
end

#error_keyObject



134
135
136
# File 'lib/matestack/ui/vue_js/components/form/base.rb', line 134

def error_key
  "vc.parentFormErrors['#{key}']"
end

#error_tagObject



142
143
144
145
# File 'lib/matestack/ui/vue_js/components/form/base.rb', line 142

def error_tag
  get_from_error_config(:tag) || :div
  # error_config.is_a?(Hash) && error_config.dig(:tag) || :div
end

#form_contextObject



10
11
12
# File 'lib/matestack/ui/vue_js/components/form/base.rb', line 10

def form_context
  Matestack::Ui::VueJs::Components::Form::Context.form_context
end

#get_from_error_config(*keys) ⇒ Object



162
163
164
165
166
# File 'lib/matestack/ui/vue_js/components/form/base.rb', line 162

def get_from_error_config(*keys)
  comp_error_config = error_config.dig(*keys) if error_config.is_a?(Hash)
  form_error_config = form_context.ctx.errors.dig(*keys) if form_context.ctx.errors.is_a?(Hash)
  comp_error_config || form_error_config
end

#idObject



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/matestack/ui/vue_js/components/form/base.rb', line 44

def id
  if ctx.id.present?
    "'#{ctx.id}'"
  else
    if form_context.is_nested_form?
      "'#{key}'+vc.parentNestedFormRuntimeId"
    else
      "'#{key}'"
    end
  end
end

#initObject



36
37
38
# File 'lib/matestack/ui/vue_js/components/form/base.rb', line 36

def init
  ctx.init
end

#init_valueObject



89
90
91
92
93
94
# File 'lib/matestack/ui/vue_js/components/form/base.rb', line 89

def init_value
  return init unless init.nil?
  if form_context.for_option.respond_to?(key)
    form_context.for_option.send(key)
  end
end

#input_error_classObject



147
148
149
150
# File 'lib/matestack/ui/vue_js/components/form/base.rb', line 147

def input_error_class
  get_from_error_config(:input, :class) || 'error'
  # error_config.is_a?(Hash) && error_config.dig(:input, :class) || 'error'
end

#input_keyObject



102
103
104
# File 'lib/matestack/ui/vue_js/components/form/base.rb', line 102

def input_key
  "vc.parentFormData['#{key}']"
end

#input_labelObject



32
33
34
# File 'lib/matestack/ui/vue_js/components/form/base.rb', line 32

def input_label
  ctx.label
end

#keyObject

options/settings



24
25
26
# File 'lib/matestack/ui/vue_js/components/form/base.rb', line 24

def key
  ctx.key
end

#multipleObject



56
57
58
# File 'lib/matestack/ui/vue_js/components/form/base.rb', line 56

def multiple
  ctx.multiple
end

#nameObject



85
86
87
# File 'lib/matestack/ui/vue_js/components/form/base.rb', line 85

def name
  attribute_key
end

#placeholderObject



60
61
62
# File 'lib/matestack/ui/vue_js/components/form/base.rb', line 60

def placeholder
  ctx.placeholder
end

#render_errorsObject



168
169
170
171
172
173
174
175
176
# File 'lib/matestack/ui/vue_js/components/form/base.rb', line 168

def render_errors
  if display_errors?
    Matestack::Ui::Component.new(wrapper_tag, class: wrapper_error_class, 'v-if': error_key) do
      Matestack::Ui::Component.new(error_tag, class: error_class, 'v-for': "error in #{error_key}") do
        plain vue.error
      end
    end
  end
end

#typeObject



28
29
30
# File 'lib/matestack/ui/vue_js/components/form/base.rb', line 28

def type
  ctx.type
end

#v_model_type(item = nil) ⇒ Object

set v-model.number for all numeric init values or options



107
108
109
110
111
112
113
# File 'lib/matestack/ui/vue_js/components/form/base.rb', line 107

def v_model_type(item=nil)
  if item.nil?
    (type == :number || init_value.is_a?(Numeric)) ? 'v-model.number' : 'v-model'
  else
    item.is_a?(Integer) ? 'v-model.number' : 'v-model'
  end
end

#value_type(item = nil) ⇒ Object

set value-type “Integer” for all numeric init values or options



116
117
118
119
120
121
122
# File 'lib/matestack/ui/vue_js/components/form/base.rb', line 116

def value_type(item=nil)
  if item.nil?
    (type == :number || init_value.is_a?(Numeric)) ? Integer : nil
  else
    item.is_a?(Integer)? Integer : nil
  end
end

#wrapper_error_classObject



157
158
159
160
# File 'lib/matestack/ui/vue_js/components/form/base.rb', line 157

def wrapper_error_class
  get_from_error_config(:wrapper, :class) || 'errors'
  # error_config.is_a?(Hash) && error_config.dig(:wrapper, :class) || 'errors'
end

#wrapper_tagObject



152
153
154
155
# File 'lib/matestack/ui/vue_js/components/form/base.rb', line 152

def wrapper_tag
  get_from_error_config(:wrapper, :tag) || :div
  # error_config.is_a?(Hash) && error_config.dig(:wrapper, :tag) || :div
end