flowbite-components 0.1.1 → 0.1.2
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/app/assets/tailwind/flowbite_components/engine.css +2 -0
- data/app/components/flowbite/button/outline.rb +22 -0
- data/app/components/flowbite/button/pill.rb +40 -0
- data/app/components/flowbite/button.rb +92 -0
- data/app/components/flowbite/card.rb +43 -0
- data/app/components/flowbite/input/checkbox.rb +37 -0
- data/app/components/flowbite/input/date.rb +11 -0
- data/app/components/flowbite/input/email.rb +12 -0
- data/app/components/flowbite/input/field.rb +117 -0
- data/app/components/flowbite/input/file.rb +30 -0
- data/app/components/flowbite/input/hint.rb +57 -0
- data/app/components/flowbite/input/label.rb +82 -0
- data/app/components/flowbite/input/number.rb +11 -0
- data/app/components/flowbite/input/password.rb +11 -0
- data/app/components/flowbite/input/phone.rb +11 -0
- data/app/components/flowbite/input/radio_button.rb +50 -0
- data/app/components/flowbite/input/select.rb +49 -0
- data/app/components/flowbite/input/textarea.rb +42 -0
- data/app/components/flowbite/input/url.rb +12 -0
- data/app/components/flowbite/input/validation_error.rb +11 -0
- data/app/components/flowbite/input_field/checkbox.html.erb +14 -0
- data/app/components/flowbite/input_field/checkbox.rb +49 -0
- data/app/components/flowbite/input_field/date.rb +13 -0
- data/app/components/flowbite/input_field/email.rb +13 -0
- data/app/components/flowbite/input_field/file.rb +13 -0
- data/app/components/flowbite/input_field/input_field.html.erb +8 -0
- data/app/components/flowbite/input_field/number.rb +13 -0
- data/app/components/flowbite/input_field/password.rb +13 -0
- data/app/components/flowbite/input_field/phone.rb +13 -0
- data/app/components/flowbite/input_field/radio_button.html.erb +14 -0
- data/app/components/flowbite/input_field/radio_button.rb +88 -0
- data/app/components/flowbite/input_field/select.rb +31 -0
- data/app/components/flowbite/input_field/text.rb +8 -0
- data/app/components/flowbite/input_field/textarea.rb +13 -0
- data/app/components/flowbite/input_field/url.rb +13 -0
- data/app/components/flowbite/input_field.rb +187 -0
- data/app/components/flowbite/style.rb +13 -0
- data/lib/flowbite/components/version.rb +1 -1
- metadata +39 -1
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Flowbite
|
4
|
+
module Input
|
5
|
+
# The `Select` component renders a select input field for use in forms.
|
6
|
+
#
|
7
|
+
# https://flowbite.com/docs/forms/select/
|
8
|
+
#
|
9
|
+
# Wraps `ActionView::Helpers::FormOptionsHelper#select`: https://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select
|
10
|
+
class Select < Field
|
11
|
+
SIZES = {
|
12
|
+
sm: ["p-2", "text-xs"],
|
13
|
+
default: ["p-2.5", "text-sm"],
|
14
|
+
lg: ["px-4", "py-3", "text-base"]
|
15
|
+
}.freeze
|
16
|
+
|
17
|
+
def initialize(form:, attribute:, collection: [], disabled: false, options: {}, size: :default)
|
18
|
+
super(form: form, attribute: attribute, disabled: disabled, options: options, size: size)
|
19
|
+
@collection = collection
|
20
|
+
end
|
21
|
+
|
22
|
+
# Returns the HTML to use for the actual input field element.
|
23
|
+
def call
|
24
|
+
@form.send(
|
25
|
+
input_field_type,
|
26
|
+
@attribute,
|
27
|
+
@collection,
|
28
|
+
{},
|
29
|
+
html_options
|
30
|
+
)
|
31
|
+
end
|
32
|
+
|
33
|
+
# Returns the name of the method used to generate HTML for the input field
|
34
|
+
def input_field_type
|
35
|
+
:select
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
# Returns the html_options argument for the select method
|
41
|
+
def html_options
|
42
|
+
{
|
43
|
+
class: classes,
|
44
|
+
disabled: disabled?
|
45
|
+
}.merge(options)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Flowbite
|
4
|
+
module Input
|
5
|
+
class Textarea < Field
|
6
|
+
class << self
|
7
|
+
# rubocop:disable Layout/LineLength
|
8
|
+
def styles
|
9
|
+
{
|
10
|
+
default: Flowbite::Style.new(
|
11
|
+
default: ["block", "w-full", "text-gray-900", "bg-gray-50", "rounded-lg", "border", "border-gray-300", "focus:ring-blue-500", "focus:border-blue-500", "dark:bg-gray-700", "dark:border-gray-600", "dark:placeholder-gray-400", "dark:text-white", "dark:focus:ring-blue-500", "dark:focus:border-blue-500"],
|
12
|
+
disabled: ["block", "w-full", "bg-gray-100", "rounded-lg", "border", "border-gray-300", "text-gray-900", "cursor-not-allowed"],
|
13
|
+
error: ["block", "w-full", "bg-red-50", "border", "border-red-500", "text-red-900", "placeholder-red-700", "rounded-lg", "focus:ring-red-500", "focus:border-red-500", "dark:bg-gray-700", "dark:text-red-500", "dark:placeholder-red-500", "dark:border-red-500"]
|
14
|
+
)
|
15
|
+
}.freeze
|
16
|
+
end
|
17
|
+
# rubocop:enable Layout/LineLength
|
18
|
+
end
|
19
|
+
|
20
|
+
# Returns the HTML to use for the actual input field element.
|
21
|
+
def call
|
22
|
+
@form.send(
|
23
|
+
input_field_type,
|
24
|
+
@attribute,
|
25
|
+
**input_options
|
26
|
+
)
|
27
|
+
end
|
28
|
+
|
29
|
+
protected
|
30
|
+
|
31
|
+
# Returns the CSS classes to apply to the input field
|
32
|
+
def classes
|
33
|
+
self.class.classes(size: size, state: state)
|
34
|
+
end
|
35
|
+
|
36
|
+
# Returns the name of the method used to generate HTML for the input field
|
37
|
+
def input_field_type
|
38
|
+
:text_area
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<div class="flex">
|
2
|
+
<div class="flex items-center h-5">
|
3
|
+
<%= input %>
|
4
|
+
</div>
|
5
|
+
|
6
|
+
<div class="ms-2 text-sm">
|
7
|
+
<%= label %>
|
8
|
+
<%= hint %>
|
9
|
+
</div>
|
10
|
+
|
11
|
+
<% errors.each do |error| %>
|
12
|
+
<%= render(Flowbite::Input::ValidationError.new) { error.upcase_first } %>
|
13
|
+
<% end %>
|
14
|
+
</div>
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Flowbite
|
4
|
+
class InputField
|
5
|
+
class Checkbox < InputField
|
6
|
+
protected
|
7
|
+
|
8
|
+
def input_component
|
9
|
+
::Flowbite::Input::Checkbox
|
10
|
+
end
|
11
|
+
|
12
|
+
protected
|
13
|
+
|
14
|
+
def default_hint_options
|
15
|
+
return {} unless @hint
|
16
|
+
|
17
|
+
{
|
18
|
+
class: hint_classes,
|
19
|
+
id: id_for_hint_element
|
20
|
+
}.merge(@hint[:options] || {})
|
21
|
+
end
|
22
|
+
|
23
|
+
def default_label_options
|
24
|
+
options = super
|
25
|
+
options[:options] ||= {}
|
26
|
+
options[:options][:class] = options.dig(:options, :class) || label_classes
|
27
|
+
options
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def hint_classes
|
33
|
+
if disabled?
|
34
|
+
"text-xs font-normal text-gray-400 dark:text-gray-500"
|
35
|
+
else
|
36
|
+
"text-xs font-normal text-gray-500 dark:text-gray-300"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def label_classes
|
41
|
+
if disabled?
|
42
|
+
"font-medium text-gray-400 dark:text-gray-500"
|
43
|
+
else
|
44
|
+
"font-medium text-gray-900 dark:text-gray-300"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<div class="flex">
|
2
|
+
<div class="flex items-center h-5">
|
3
|
+
<%= input %>
|
4
|
+
</div>
|
5
|
+
|
6
|
+
<div class="ms-2 text-sm">
|
7
|
+
<%= label %>
|
8
|
+
<%= hint %>
|
9
|
+
</div>
|
10
|
+
|
11
|
+
<% errors.each do |error| %>
|
12
|
+
<%= render(Flowbite::Input::ValidationError.new) { error.upcase_first } %>
|
13
|
+
<% end %>
|
14
|
+
</div>
|
@@ -0,0 +1,88 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Flowbite
|
4
|
+
class InputField
|
5
|
+
class RadioButton < InputField
|
6
|
+
protected
|
7
|
+
|
8
|
+
def initialize(attribute:, form:, value:, disabled: false, hint: nil, input: {}, label: {})
|
9
|
+
super(attribute: attribute, form: form, disabled: disabled, hint: hint, input: input, label: label)
|
10
|
+
@value = value
|
11
|
+
end
|
12
|
+
|
13
|
+
def input_component
|
14
|
+
::Flowbite::Input::RadioButton
|
15
|
+
end
|
16
|
+
|
17
|
+
protected
|
18
|
+
|
19
|
+
def default_input
|
20
|
+
args = {
|
21
|
+
attribute: @attribute,
|
22
|
+
disabled: disabled?,
|
23
|
+
form: @form,
|
24
|
+
options: default_input_options.merge(@input[:options] || {}),
|
25
|
+
value: @value
|
26
|
+
}
|
27
|
+
|
28
|
+
input_component.new(**args)
|
29
|
+
end
|
30
|
+
|
31
|
+
# Returns options for the default label element. This includes CSS classes
|
32
|
+
# since they are specific to RadioButton labels (and Checkbox ones).
|
33
|
+
def default_label_options
|
34
|
+
super.merge({
|
35
|
+
options: {
|
36
|
+
class: label_classes,
|
37
|
+
for: id_for_input_element
|
38
|
+
}
|
39
|
+
})
|
40
|
+
end
|
41
|
+
|
42
|
+
# Returns the HTML to use for the hint element if any
|
43
|
+
def hint
|
44
|
+
return unless hint?
|
45
|
+
|
46
|
+
component = Flowbite::Input::Hint.new(
|
47
|
+
attribute: @attribute,
|
48
|
+
form: @form,
|
49
|
+
options: {
|
50
|
+
class: hint_classes,
|
51
|
+
id: id_for_hint_element
|
52
|
+
}
|
53
|
+
).with_content(@hint)
|
54
|
+
render(component)
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def hint_classes
|
60
|
+
if disabled?
|
61
|
+
"text-xs font-normal text-gray-400 dark:text-gray-500"
|
62
|
+
else
|
63
|
+
"text-xs font-normal text-gray-500 dark:text-gray-300"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def id_for_input_element
|
68
|
+
[
|
69
|
+
@form.object_name,
|
70
|
+
@attribute,
|
71
|
+
@value
|
72
|
+
].join("_")
|
73
|
+
end
|
74
|
+
|
75
|
+
def id_for_hint_element
|
76
|
+
[id_for_input_element, "hint"].join("_")
|
77
|
+
end
|
78
|
+
|
79
|
+
def label_classes
|
80
|
+
if disabled?
|
81
|
+
"font-medium text-gray-400 dark:text-gray-500"
|
82
|
+
else
|
83
|
+
"font-medium text-gray-900 dark:text-gray-300"
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Flowbite
|
4
|
+
class InputField
|
5
|
+
class Select < InputField
|
6
|
+
def initialize(attribute:, form:, collection: [], disabled: false, hint: nil, input: {}, label: {}, size: :default)
|
7
|
+
super(attribute: attribute, disabled: disabled, form: form, hint: hint, input: input, label: label, size: size)
|
8
|
+
@collection = collection
|
9
|
+
end
|
10
|
+
|
11
|
+
def input
|
12
|
+
render(
|
13
|
+
input_component.new(
|
14
|
+
attribute: @attribute,
|
15
|
+
collection: @collection,
|
16
|
+
disabled: @disabled,
|
17
|
+
form: @form,
|
18
|
+
options: input_options,
|
19
|
+
size: @size
|
20
|
+
)
|
21
|
+
)
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def input_component
|
27
|
+
::Flowbite::Input::Select
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,187 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Flowbite
|
4
|
+
# A form element for a single field, containing label, input field, error
|
5
|
+
# messages, helper text and whatever else is needed for a user friendly input
|
6
|
+
# experience.
|
7
|
+
#
|
8
|
+
# @see https://flowbite.com/docs/forms/input-field/
|
9
|
+
#
|
10
|
+
# The input field is an important part of the form element that can be used to
|
11
|
+
# create interactive controls to accept data from the user based on multiple
|
12
|
+
# input types, such as text, email, number, password, URL, phone number, and
|
13
|
+
# more.
|
14
|
+
#
|
15
|
+
# Usually you'd use one of the subclasses of this class which implement the
|
16
|
+
# different input types, like `Flowbite::InputField::Text`,
|
17
|
+
# `Flowbite::InputField::Email`, etc.
|
18
|
+
#
|
19
|
+
# Expects 2 arguments:
|
20
|
+
#
|
21
|
+
# @param attribute [Symbol] The name of the attribute to render in this input
|
22
|
+
# field.
|
23
|
+
#
|
24
|
+
# @param form [ActionView::Helpers::FormBuilder] The form builder object that
|
25
|
+
# will be used to generate the input field.
|
26
|
+
#
|
27
|
+
# Supports additional arguments:
|
28
|
+
#
|
29
|
+
# @param hint [String] A hint to display below the input field, providing
|
30
|
+
# additional context or instructions for the user. This is optional. See
|
31
|
+
# https://flowbite.com/docs/forms/input-field/#helper-text
|
32
|
+
#
|
33
|
+
# @param label [Hash] A hash with options for the label. These are passed to
|
34
|
+
# Flowbite::Input::Label, see that for details. Can contain:
|
35
|
+
# - `content`: The content of the label. If not provided, the label will
|
36
|
+
# default to the attribute name.
|
37
|
+
# - `options`: A hash of additional options to pass to the label component.
|
38
|
+
# This can be used to set the class, for example.
|
39
|
+
#
|
40
|
+
# @param disabled [Boolean] Whether the input field should be disabled.
|
41
|
+
# Defaults to `false`.
|
42
|
+
#
|
43
|
+
# @param input [Hash] A hash with options for the default input component.
|
44
|
+
# These are passed to the input components constructor, so see whatever
|
45
|
+
# component is being used for details. Can contain:
|
46
|
+
# - `options`: Additional HTML attributes to pass to the input element.
|
47
|
+
#
|
48
|
+
# @param size [Symbol] The size of the input field. Can be one of `:sm`,
|
49
|
+
# `:md`, or `:lg`. Defaults to `:md`.
|
50
|
+
#
|
51
|
+
# Sample usage
|
52
|
+
#
|
53
|
+
# <% form_for @person do |form| %>
|
54
|
+
# <%= render(
|
55
|
+
# Flowbite::InputField::Number.new(
|
56
|
+
# :attribute => :name,
|
57
|
+
# :form => form
|
58
|
+
# )
|
59
|
+
# ) %>
|
60
|
+
# <% end %>
|
61
|
+
#
|
62
|
+
# To render an input without labels or error messages etc, use
|
63
|
+
# `Flowbite::Input::Field` instead.
|
64
|
+
class InputField < ViewComponent::Base
|
65
|
+
renders_one :hint
|
66
|
+
renders_one :input
|
67
|
+
renders_one :label
|
68
|
+
|
69
|
+
# Returns the errors for attribute
|
70
|
+
def errors
|
71
|
+
@object.errors[@attribute] || []
|
72
|
+
end
|
73
|
+
|
74
|
+
def initialize(attribute:, form:, disabled: false, hint: nil, input: {}, label: {}, size: :default)
|
75
|
+
@attribute = attribute
|
76
|
+
@disabled = disabled
|
77
|
+
@form = form
|
78
|
+
@hint = hint
|
79
|
+
@input = input
|
80
|
+
@label = label
|
81
|
+
@object = form.object
|
82
|
+
@size = size
|
83
|
+
end
|
84
|
+
|
85
|
+
def input_component
|
86
|
+
::Flowbite::Input::Field
|
87
|
+
end
|
88
|
+
|
89
|
+
protected
|
90
|
+
|
91
|
+
# Returns the HTML to use for the hint element if any
|
92
|
+
def default_hint
|
93
|
+
return unless hint?
|
94
|
+
|
95
|
+
component = Flowbite::Input::Hint.new(
|
96
|
+
attribute: @attribute,
|
97
|
+
form: @form,
|
98
|
+
options: default_hint_options
|
99
|
+
).with_content(default_hint_content)
|
100
|
+
render(component)
|
101
|
+
end
|
102
|
+
|
103
|
+
def default_hint_content
|
104
|
+
return nil unless @hint
|
105
|
+
|
106
|
+
@hint[:content]
|
107
|
+
end
|
108
|
+
|
109
|
+
# Returns a Hash with the default attributes to apply to the hint element.
|
110
|
+
#
|
111
|
+
# The default attributes can be overriden by passing the `hint[options]`
|
112
|
+
# argument to the constructor.
|
113
|
+
def default_hint_options
|
114
|
+
return {} unless @hint
|
115
|
+
|
116
|
+
{
|
117
|
+
id: id_for_hint_element
|
118
|
+
}.merge(@hint[:options] || {})
|
119
|
+
end
|
120
|
+
|
121
|
+
# Returns a Hash with the default attributes to apply to the input element.
|
122
|
+
#
|
123
|
+
# The default attributes can be overriden by passing the `input[options]`
|
124
|
+
# argument to the constructor.
|
125
|
+
def default_input_options
|
126
|
+
if hint?
|
127
|
+
{
|
128
|
+
"aria-describedby": id_for_hint_element
|
129
|
+
}
|
130
|
+
else
|
131
|
+
{}
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
# Returns the HTML to use for the default input element.
|
136
|
+
def default_input
|
137
|
+
render(input_component.new(
|
138
|
+
form: @form,
|
139
|
+
attribute: @attribute,
|
140
|
+
disabled: @disabled,
|
141
|
+
options: input_options,
|
142
|
+
size: @size
|
143
|
+
))
|
144
|
+
end
|
145
|
+
|
146
|
+
def default_label
|
147
|
+
component = Flowbite::Input::Label.new(**default_label_options)
|
148
|
+
if default_label_content
|
149
|
+
component.with_content(default_label_content)
|
150
|
+
else
|
151
|
+
component
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
def default_label_content
|
156
|
+
@label[:content]
|
157
|
+
end
|
158
|
+
|
159
|
+
def default_label_options
|
160
|
+
label_options = @label.dup
|
161
|
+
label_options.delete(:content)
|
162
|
+
|
163
|
+
{
|
164
|
+
attribute: @attribute,
|
165
|
+
form: @form
|
166
|
+
}.merge(label_options)
|
167
|
+
end
|
168
|
+
|
169
|
+
# Returns true if the input field is disabled, false otherwise.
|
170
|
+
def disabled?
|
171
|
+
!!@disabled
|
172
|
+
end
|
173
|
+
|
174
|
+
# Returns true if the input field has a hint, false otherwise.
|
175
|
+
def hint?
|
176
|
+
@hint.present?
|
177
|
+
end
|
178
|
+
|
179
|
+
def id_for_hint_element
|
180
|
+
"#{@form.object_name}_#{@attribute}_hint"
|
181
|
+
end
|
182
|
+
|
183
|
+
def input_options
|
184
|
+
default_input_options.merge(@input[:options] || {})
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|