Class: Matestack::Ui::VueJs::Components::Form::Radio

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

Instance Method Summary collapse

Methods inherited from Base

#attribute_key, #attributes, #change_event, #component_attributes, #display_errors?, #error_class, #error_config, #error_key, #error_tag, #form_context, #get_from_error_config, #id, #init, #init_value, #input_error_class, #input_key, #input_label, #key, #multiple, #name, #placeholder, #render_errors, #type, #value_type, #wrapper_error_class, #wrapper_tag

Methods inherited from Vue

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

Constructor Details

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

Instance Method Details

#component_idObject



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

def component_id
  "radio-component-for-#{key}"
end

#item_id(item) ⇒ Object



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

def item_id(item)
  "#{id || key}+'_#{item_value(item)}'"
end

#item_label(item) ⇒ Object



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

def item_label(item)
  item.is_a?(Array) ? item.first : item
end

#item_name(item) ⇒ Object



64
65
66
# File 'lib/matestack/ui/vue_js/components/form/radio.rb', line 64

def item_name(item)
  "#{key}_#{item_value(item)}"
end

#item_value(item) ⇒ Object

calculated attributes



52
53
54
# File 'lib/matestack/ui/vue_js/components/form/radio.rb', line 52

def item_value(item)
  item.is_a?(Array) ? item.last : item
end

#radio_attributes(item) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/matestack/ui/vue_js/components/form/radio.rb', line 34

def radio_attributes(item)
  attributes.merge({
    ":id": item_id(item),
    name: item_name(item),
    type: :radio,
    "matestack-ui-vuejs-ref": matestack_ui_vuejs_ref("select.#{key}"),
    'value-type': value_type(item_value(radio_options.first))
  }).tap do |attrs|
    attrs[value_key(item)] = item_value(item)
  end
end

#radio_optionsObject



46
47
48
# File 'lib/matestack/ui/vue_js/components/form/radio.rb', line 46

def radio_options
  @radio_options ||= options.delete(:options)
end

#render_optionsObject



16
17
18
19
20
21
# File 'lib/matestack/ui/vue_js/components/form/radio.rb', line 16

def render_options
  radio_options.to_a.each do |item|
    input radio_attributes(item)
    label item_label(item), ":for": item_id(item)
  end
end

#responseObject



9
10
11
12
13
14
# File 'lib/matestack/ui/vue_js/components/form/radio.rb', line 9

def response
  div class: 'matestack-ui-core-form-radio' do
    render_options
    render_errors
  end
end

#v_model_typeObject



68
69
70
# File 'lib/matestack/ui/vue_js/components/form/radio.rb', line 68

def v_model_type
  item_value(radio_options.first).is_a?(Numeric) ? 'v-model.number' : 'v-model'
end

#value_key(value) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/matestack/ui/vue_js/components/form/radio.rb', line 72

def value_key(value)
  if value.is_a?(Array)
    value[1].is_a?(Numeric) ? ':value' : 'value'
  else
    value.is_a?(Numeric) ? ':value' : 'value'
  end
end

#vue_propsObject



27
28
29
30
31
32
# File 'lib/matestack/ui/vue_js/components/form/radio.rb', line 27

def vue_props
  {
    init_value: init_value,
    key: key,
  }
end