-
Notifications
You must be signed in to change notification settings - Fork 0
/
email_field.rb
150 lines (131 loc) · 4.71 KB
/
email_field.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# frozen_string_literal: true
require "view_component"
module Felt
module InputGroup
class EmailField < ViewComponent::Base
attr_reader :attribute, :form, :options
# Returns the classes to use for the root element of the input.
def classes
Felt.configuration.classes.dig(:input_group, :text_field)
end
# Returns the error messages to output in the input group. Returns [] if no
# errors.
#
# This returns the full error messages for the attribute, see
# ActiveModel::Errors#full_messages for more details.
def errors
form.object.errors.full_messages_for(attribute)
end
# Returns true if the input group has errors.
def errors?
errors.any?
end
# Returns the help text for the input group. If no help is configured,
# returns nil.
#
# Help text is looked up in the following order:
#
# 1. The help argument passed to the component.
# 2. The `help` key in the `forms.<object_name>.<attribute>` translation.
def help
@help ||=
translate("help")
end
# Returns true if the input group has a hint configured
def help?
help.present?
end
# Returns the hint for the input group. If no hint is configured, returns nil.
#
# Hints are looked up in the following order:
#
# 1. The hint argument passed to the component.
# 2. The `hint` key in the `forms.<object_name>.<attribute>` translation.
def hint
@hint ||=
translate("hint")
end
# Returns true if the input group has a hint configured
def hint?
hint.present?
end
# - hint: The hint for the input group. If not provided, the hint will be
# looked up in the `forms.<object_name>.<attribute>` translation. See
# #hint for more details. To disable the hint, pass an empty string.
#
# - label: The label for the input group. If not provided, the label will
# be looked up in the `forms.<object_name>.<attribute>` translation. See
# #label for more details. To disable the label, pass an empty string.
#
# - placeholder: The placeholder for the input field. If not provided, the
# placeholder will be looked up in the `forms.<object_name>.<attribute>`
# translation. See #placeholder for more details. To disable the
# placeholder, pass an empty string.
#
# All remaining keyword arguments are passed to the wrapping div element
# of the input group. See ActionView::Helpers::TagHelper#content_tag for
# details.
def initialize(attribute:, form:, help: nil, hint: nil, label: nil, placeholder: nil, **options)
@attribute = attribute
@form = form
@help = help
@hint = hint
@label = label
@options = options
@placeholder = placeholder
end
# Returns the classes to use for the input field. Use configuration from
# `input_group.input.invalid` if the input group has errors,
# `input_group.input.default` otherwise.
def input_classes
key = if errors?
:invalid
else
:default
end
Felt.configuration.classes.dig(:input_group, :input, key)
end
# Returns the label for the input group. If no label is configured, returns
# nil.
#
# Labels are looked up in the following order:
#
# 1. The label argument passed to the component.
# 2. The `label` key in the `forms.<object_name>.<attribute>` translation.
# 3. The translation value found under `helpers.label.<modelname>.<attribute>`
# (like with ActionView::Helpers::FormBuilder#label).
def label
@label ||=
translate("label") ||
form.label(attribute)
end
# Returns true if the input group has a label configured
def label?
label.present?
end
# Returns the placeholder for the input group. If no placeholder is
# configured, returns nil.
#
# Placeholders are looked up in the following order:
#
# 1. The placeholder argument passed to the component.
# 2. The `placeholder` key in the `forms.<object_name>.<attribute>`
# translation.
def placeholder
@placeholder ||=
translate("placeholder")
end
# Returns true if the input group has a placeholder configured
def placeholder?
placeholder.present?
end
private
def translate(key)
I18n.translate(key, default: nil, scope: translation_scope)
end
def translation_scope
[:forms, form.object_name, attribute].join(".")
end
end
end
end