-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfield.rb
More file actions
126 lines (108 loc) · 3.85 KB
/
field.rb
File metadata and controls
126 lines (108 loc) · 3.85 KB
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
# frozen_string_literal: true
module Flowbite
module Input
# The indivdual input component for use in forms without labels or error
# messages.
#
# Use this when you want to render an input field on its own without any
# surrounding elements, ie as a building block in more complex input
# components.
#
# To render a complete input field with labels and error messages, use
# `Flowbite::InputField` instead.
class Field < ViewComponent::Base
SIZES = {
sm: ["px-2.5", "py-2", "text-sm"],
default: ["px-3", "py-2.5", "text-sm"],
lg: ["px-3.5", "py-3", "text-base"]
}.freeze
STATES = [
DEFAULT = :default,
DISABLED = :disabled,
ERROR = :error
].freeze
attr_reader :options, :size, :style
class << self
def classes(size: :default, state: :default, style: :default)
style = styles.fetch(style)
state_classes = style.fetch(state)
state_classes + sizes.fetch(size)
end
# Returns the sizes this Field supports.
#
# This is effectively the SIZES constant, but provided as a method to
# return the constant from the current class, not the superclass.
#
# @return [Hash] A hash mapping size names to their corresponding CSS
# classes.
def sizes
const_get(:SIZES)
end
# rubocop:disable Layout/LineLength
def styles
Flowbite::Styles.from_hash(
{
default: {
default: ["bg-neutral-secondary-medium", "border", "border-default-medium", "text-heading", "rounded-base", "focus:ring-brand", "focus:border-brand", "block", "w-full", "shadow-xs", "placeholder:text-body"],
disabled: ["bg-neutral-secondary-medium", "border", "border-default-medium", "text-fg-disabled", "rounded-base", "focus:ring-brand", "focus:border-brand", "block", "w-full", "shadow-xs", "cursor-not-allowed", "placeholder:text-fg-disabled"],
error: ["bg-danger-soft", "border", "border-danger-subtle", "text-fg-danger-strong", "rounded-base", "focus:ring-danger", "focus:border-danger", "block", "w-full", "shadow-xs", "placeholder:text-fg-danger-strong"]
}
}.freeze
)
end
# rubocop:enable Layout/LineLength
end
def initialize(attribute:, form:, class: nil, disabled: false, options: {}, size: :default)
@attribute = attribute
@class = Array.wrap(binding.local_variable_get(:class))
@disabled = disabled
@form = form
@options = options || {}
@object = form.object
@size = size
end
# Returns the HTML to use for the actual input field element.
def call
@form.send(
input_field_type,
@attribute,
**input_options
)
end
# Returns the CSS classes to apply to the input field
def classes
self.class.classes(size: size, state: state) + @class
end
# Returns the name of the method used to generate HTML for the input field
def input_field_type
:text_field
end
protected
# Returns true if the field is disabled
def disabled?
!!@disabled
end
# Returns true if the object has errors. Returns false if there is no
# object.
#
# @return [Boolean] true if there are errors, false otherwise.
def errors?
return false unless @object
@object.errors.include?(@attribute.intern)
end
private
# Returns the options argument for the input field
def input_options
{
class: classes,
disabled: disabled?
}.merge(options)
end
def state
return DISABLED if disabled?
return ERROR if errors?
DEFAULT
end
end
end
end