klods-ruby 1.3.0 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/klods/form_builder.rb +59 -18
- data/lib/klods/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: fa5215d6f8d8d6fdce3f82098a17b2dd9fc5f4047a62bac0a1dfeeca7a2cd62f
|
|
4
|
+
data.tar.gz: d31d4138d47a2c3677abbfb83393b6fb64f1724ee6c81276d410a41c2ccfe58f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: '0516894ed0e82205daa3c7df426842938bbc30281a4c1267a998405736a0e1fcd1844e383d653c1c4236e85974d48216b40ba893ea1a6c19ed0ddd733c707aa0'
|
|
7
|
+
data.tar.gz: 7fe64308e30edcda9caf2415b08afd4c1b489e35f8bd12c5162a61996aafce0a496f258207f8d0fb2e5c232b31e52fccbc323e82e97718a11ea9686ae24148ca
|
data/lib/klods/form_builder.rb
CHANGED
|
@@ -1,13 +1,50 @@
|
|
|
1
1
|
module Klods
|
|
2
2
|
class FormBuilder < ActionView::Helpers::FormBuilder
|
|
3
|
-
# Renders a fully wired klods
|
|
4
|
-
# optional help/error text.
|
|
3
|
+
# Renders a fully wired klods input field: wrapper div, label, input, and
|
|
4
|
+
# optional help/error text. For textarea use f.klods_textarea; for select use f.klods_select.
|
|
5
5
|
#
|
|
6
6
|
# = f.klods_field :email, label: "Email", type: :email, autocomplete: "email"
|
|
7
7
|
# = f.klods_field :password, label: "Password", type: :password, required: true
|
|
8
|
-
# = f.klods_field :bio, label: "Bio", type: :textarea
|
|
9
8
|
# = f.klods_field :name, label: "Name", help: "Your display name"
|
|
10
9
|
def klods_field(method, label: nil, type: :text, help: nil, required: false, **options)
|
|
10
|
+
render_klods_wrapper(method, label: label, help: help, required: required) do |aria|
|
|
11
|
+
send(input_helper_for(type), method, class: "klods-input", **aria, **options)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# Renders a klods textarea field with wrapper, label, and error/help wiring.
|
|
16
|
+
#
|
|
17
|
+
# = f.klods_textarea :bio, label: "Bio", help: "Tell us about yourself"
|
|
18
|
+
def klods_textarea(method, label: nil, help: nil, required: false, **options)
|
|
19
|
+
render_klods_wrapper(method, label: label, help: help, required: required) do |aria|
|
|
20
|
+
render_textarea_input(method, class: "klods-textarea", **aria, **options)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Renders a klods select field with wrapper, label, and error/help wiring.
|
|
25
|
+
# choices is an array of ["Label", value] pairs (or plain values).
|
|
26
|
+
#
|
|
27
|
+
# = f.klods_select :role, [["Admin", "admin"], ["User", "user"]], label: "Role"
|
|
28
|
+
def klods_select(method, choices = [], label: nil, help: nil, required: false, **options)
|
|
29
|
+
render_klods_wrapper(method, label: label, help: help, required: required) do |aria|
|
|
30
|
+
select_el = render_select_input(method, choices, class: "klods-select", **aria, **options)
|
|
31
|
+
@template.content_tag(:div, select_el, class: "klods-select-wrapper")
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Renders a submit button styled as a primary klods button.
|
|
36
|
+
#
|
|
37
|
+
# = f.klods_submit "Save"
|
|
38
|
+
# = f.klods_submit "Sign up", class: "klods-button--wide" # extra class merged in
|
|
39
|
+
def klods_submit(label = nil, **options)
|
|
40
|
+
extra = options.delete(:class)
|
|
41
|
+
options[:class] = ["klods-button", "klods-button--primary", extra].compact.join(" ")
|
|
42
|
+
submit(label, **options)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
private
|
|
46
|
+
|
|
47
|
+
def render_klods_wrapper(method, label:, help:, required:, &input_block)
|
|
11
48
|
errors = Array(object&.errors&.[](method))
|
|
12
49
|
error_msg = errors.first
|
|
13
50
|
is_invalid = !error_msg.nil?
|
|
@@ -25,24 +62,12 @@ module Klods
|
|
|
25
62
|
|
|
26
63
|
@template.content_tag(:div, class: field_cls) do
|
|
27
64
|
label(method, label || method.to_s.humanize, class: label_cls) +
|
|
28
|
-
|
|
65
|
+
input_block.call(aria) +
|
|
29
66
|
((help && !is_invalid) ? @template.content_tag(:p, help, id: help_id, class: "klods-help") : "".html_safe) +
|
|
30
67
|
(error_msg ? @template.content_tag(:p, error_msg, id: error_id, class: "klods-error", role: "alert") : "".html_safe)
|
|
31
68
|
end
|
|
32
69
|
end
|
|
33
70
|
|
|
34
|
-
# Renders a submit button styled as a primary klods button.
|
|
35
|
-
#
|
|
36
|
-
# = f.klods_submit "Save"
|
|
37
|
-
# = f.klods_submit "Sign up", class: "klods-button--wide" # extra class merged in
|
|
38
|
-
def klods_submit(label = nil, **options)
|
|
39
|
-
extra = options.delete(:class)
|
|
40
|
-
options[:class] = ["klods-button", "klods-button--primary", extra].compact.join(" ")
|
|
41
|
-
submit(label, **options)
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
private
|
|
45
|
-
|
|
46
71
|
def input_helper_for(type)
|
|
47
72
|
{
|
|
48
73
|
email: :email_field,
|
|
@@ -52,9 +77,25 @@ module Klods
|
|
|
52
77
|
number: :number_field,
|
|
53
78
|
date: :date_field,
|
|
54
79
|
time: :time_field,
|
|
55
|
-
search: :search_field
|
|
56
|
-
textarea: :text_area
|
|
80
|
+
search: :search_field
|
|
57
81
|
}.fetch(type, :text_field)
|
|
58
82
|
end
|
|
83
|
+
|
|
84
|
+
# Rails 8 FormBuilder#text_area calls @template.textarea(...), which is shadowed
|
|
85
|
+
# by the klods textarea builder. Build the textarea directly to avoid the conflict.
|
|
86
|
+
def render_textarea_input(method, **options)
|
|
87
|
+
name = @object_name.present? ? "#{@object_name}[#{method}]" : method.to_s
|
|
88
|
+
value = object&.public_send(method).to_s
|
|
89
|
+
@template.content_tag(:textarea, value, name: name, id: field_id(method), **options)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# FormBuilder#select calls @template.select(...), which is shadowed by the klods
|
|
93
|
+
# select builder. Build the select directly using options_for_select + content_tag.
|
|
94
|
+
def render_select_input(method, choices, **options)
|
|
95
|
+
name = @object_name.present? ? "#{@object_name}[#{method}]" : method.to_s
|
|
96
|
+
selected = object&.public_send(method)
|
|
97
|
+
options_html = @template.options_for_select(choices, selected)
|
|
98
|
+
@template.content_tag(:select, options_html, name: name, id: field_id(method), **options)
|
|
99
|
+
end
|
|
59
100
|
end
|
|
60
101
|
end
|
data/lib/klods/version.rb
CHANGED