-
Notifications
You must be signed in to change notification settings - Fork 22
/
submit.rb
71 lines (58 loc) · 1.77 KB
/
submit.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
module GOVUKDesignSystemFormBuilder
module Elements
class Submit < Base
using PrefixableArray
def initialize(builder, text, warning:, secondary:, classes:, prevent_double_click:, validate:, disabled:, &block)
super(builder, nil, nil)
fail ArgumentError, 'buttons can be warning or secondary' if warning && secondary
@text = text
@prevent_double_click = prevent_double_click
@warning = warning
@secondary = secondary
@classes = classes
@validate = validate
@disabled = disabled
@block_content = capture { block.call } if block_given?
end
def html
safe_join([submit, @block_content])
end
private
def submit
@builder.submit(@text, class: classes, **options)
end
def classes
%w(button)
.prefix(brand)
.push(warning_class, secondary_class, disabled_class, padding_class, custom_classes)
.flatten
.compact
end
def options
{
formnovalidate: !@validate,
disabled: @disabled,
data: {
module: %(#{brand}-button),
'prevent-double-click': @prevent_double_click
}.select { |_k, v| v.present? }
}
end
def warning_class
%(#{brand}-button--warning) if @warning
end
def secondary_class
%(#{brand}-button--secondary) if @secondary
end
def padding_class
%(#{brand}-!-margin-right-1) if @block_content
end
def disabled_class
%(#{brand}-button--disabled) if @disabled
end
def custom_classes
Array.wrap(@classes)
end
end
end
end