Skip to content

Commit 9624264

Browse files
author
Nils Henning
committed
[FEATURE] refactor radio buttons into own components, update and refactor tests
1 parent 6da720f commit 9624264

File tree

11 files changed

+451
-328
lines changed

11 files changed

+451
-328
lines changed

.byebug_history

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
continue
2+
save_screenshot
3+
continue
4+
save_screenshot
5+
continue
26
TestModel.last
37
continue
48
TestModel.last
@@ -250,7 +254,3 @@ params
250254
self
251255
params
252256
continue
253-
save_screenshot
254-
continue
255-
save_screenshot
256-
continue

app/concepts/matestack/ui/core/form/checkbox/checkbox.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
require_relative '../utils'
2+
require_relative '../has_input_html_attributes'
23
require_relative '../has_errors'
34
module Matestack::Ui::Core::Form::Checkbox
45
class Checkbox < Matestack::Ui::Core::Component::Static
56
include Matestack::Ui::Core::Form::Utils
7+
include Matestack::Ui::Core::Form::HasInputHtmlAttributes
68
include Matestack::Ui::Core::Form::HasErrors
79

8-
html_attributes :accept, :alt, :autocomplete, :autofocus, :checked, :dirname, :disabled, :form, :formaction,
9-
:formenctype, :formmethod, :formnovalidate, :formtarget, :height, :list, :max, :maxlength, :min, :minlength,
10-
:multiple, :name, :pattern, :placeholder, :readonly, :required, :size, :src, :step, :type, :value, :width
11-
1210
requires :key
1311
optional :value, :false_value, :multiple, :init, for: { as: :input_for }, label: { as: :input_label }, options: { as: :checkbox_options }
1412

@@ -40,10 +38,15 @@ def vue_attributes
4038
ref: "input.#{attr_key}",
4139
'init-value': init_value,
4240
'v-bind:class': "{ '#{input_error_class}': #{error_key} }",
41+
'value-type': value_type,
4342
"#{v_model_type}": input_key,
4443
})
4544
end
4645

46+
def value_type
47+
item_value(checkbox_options.first).is_a?(Integer) ? Integer : nil
48+
end
49+
4750
def item_value(item)
4851
item.is_a?(Array) ? item.last : item
4952
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module Matestack::Ui::Core::Form::HasInputHtmlAttributes
2+
3+
def self.included(base)
4+
base.class_eval do
5+
html_attributes(
6+
:accept, :alt, :autocomplete, :autofocus, :checked, :dirname, :disabled, :form, :formaction,
7+
:formenctype, :formmethod, :formnovalidate, :formtarget, :height, :list, :max, :maxlength, :min, :minlength,
8+
:multiple, :name, :pattern, :placeholder, :readonly, :required, :size, :src, :step, :type, :value, :width
9+
)
10+
end
11+
end
12+
13+
end

app/concepts/matestack/ui/core/form/input/input.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
require_relative '../utils'
2+
require_relative '../has_input_html_attributes'
23
require_relative '../has_errors'
34
module Matestack::Ui::Core::Form::Input
45
class Input < Matestack::Ui::Core::Component::Static
56
include Matestack::Ui::Core::Form::Utils
7+
include Matestack::Ui::Core::Form::HasInputHtmlAttributes
68
include Matestack::Ui::Core::Form::HasErrors
79

8-
html_attributes :accept, :alt, :autocomplete, :autofocus, :checked, :dirname, :disabled, :form, :formaction,
9-
:formenctype, :formmethod, :formnovalidate, :formtarget, :height, :list, :max, :maxlength, :min, :minlength,
10-
:multiple, :name, :pattern, :placeholder, :readonly, :required, :size, :src, :step, :type, :value, :width
11-
1210
requires :key, :type
1311
optional :multiple, :init, for: { as: :input_for }, label: { as: :input_label }
1412

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
require_relative '../utils'
2+
require_relative '../has_input_html_attributes'
3+
require_relative '../has_errors'
4+
module Matestack::Ui::Core::Form::Radio
5+
class Radio < Matestack::Ui::Core::Component::Static
6+
include Matestack::Ui::Core::Form::Utils
7+
include Matestack::Ui::Core::Form::HasInputHtmlAttributes
8+
include Matestack::Ui::Core::Form::HasErrors
9+
10+
requires :key
11+
optional :value, :false_value, :multiple, :init, for: { as: :input_for }, label: { as: :input_label }, options: { as: :radio_options }
12+
13+
def response
14+
radio_options.to_a.each do |item|
15+
input html_attributes.merge(
16+
attributes: vue_attributes.merge(ref: "select.#{attr_key}"),
17+
type: :radio,
18+
id: "#{id_for_item(item_value(item))}",
19+
name: item_name(item),
20+
value: item_value(item),
21+
)
22+
label text: item_label(item), for: id_for_item(item_value(item))
23+
end
24+
render_errors
25+
end
26+
27+
def vue_attributes
28+
(options[:attributes] || {}).merge({
29+
"@change": change_event,
30+
ref: "select.#{attr_key}",
31+
'init-value': init_value,
32+
'v-bind:class': "{ '#{input_error_class}': #{error_key} }",
33+
'value-type': value_type,
34+
"#{v_model_type}": input_key,
35+
})
36+
end
37+
38+
def value_type
39+
item_value(radio_options.first).is_a?(Integer) ? Integer : nil
40+
end
41+
42+
def item_value(item)
43+
item.is_a?(Array) ? item.last : item
44+
end
45+
46+
def item_name(item)
47+
"#{attr_key}_#{item.is_a?(Array) ? item.first : item}"
48+
end
49+
50+
def item_label(item)
51+
item.is_a?(Array) ? item.first : item
52+
end
53+
54+
def checked_value
55+
value || 1
56+
end
57+
58+
def v_model_type
59+
if radio_options && item_value(radio_options.first).is_a?(Integer)
60+
'v-model.number'
61+
else
62+
'v-model'
63+
end
64+
end
65+
66+
def change_event
67+
"inputChanged('#{attr_key}')"
68+
end
69+
70+
def id_for_item(value)
71+
"#{html_attributes[:id]}_#{value}"
72+
end
73+
74+
end
75+
end

lib/matestack/ui/core/components.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ def self.require_core_component(name)
7373
require_core_component "form"
7474
require_core_component "form/checkbox"
7575
require_core_component "form/input"
76+
require_core_component "form/radio"
7677
require_core_component "form/select"
7778
require_core_component "form/submit"
7879
require_core_component "header"
@@ -186,6 +187,7 @@ def self.require_core_component(name)
186187
form: Matestack::Ui::Core::Form::Form,
187188
form_checkbox: Matestack::Ui::Core::Form::Checkbox::Checkbox,
188189
form_input: Matestack::Ui::Core::Form::Input::Input,
190+
form_radio: Matestack::Ui::Core::Form::Radio::Radio,
189191
form_select: Matestack::Ui::Core::Form::Select::Select,
190192
form_submit: Matestack::Ui::Core::Form::Submit::Submit,
191193
form_textarea: Matestack::Ui::Core::Form::Textarea::Textarea,

0 commit comments

Comments
 (0)