Skip to content

Commit 8d3dbc8

Browse files
authored
Merge pull request #21 from substancelab/hint
Make hint work like label and input
2 parents 9c19ece + 572659d commit 8d3dbc8

File tree

10 files changed

+73
-45
lines changed

10 files changed

+73
-45
lines changed

app/components/flowbite/input/hint.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,16 @@ def styles
2424

2525
def call
2626
tag.p(
27-
@hint,
27+
content,
2828
class: classes,
29-
**@hint_attributes
29+
**@options
3030
)
3131
end
3232

33-
def initialize(attribute:, form:, hint:, hint_attributes: {})
33+
def initialize(attribute:, form:, options: {})
3434
@attribute = attribute
3535
@form = form
36-
@hint = hint
37-
@hint_attributes = hint_attributes
36+
@options = options
3837
@object = form.object
3938
end
4039

app/components/flowbite/input_field.rb

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ module Flowbite
6262
# To render an input without labels or error messages etc, use
6363
# `Flowbite::Input::Field` instead.
6464
class InputField < ViewComponent::Base
65+
renders_one :hint
6566
renders_one :input
6667
renders_one :label
6768

@@ -85,19 +86,39 @@ def input_component
8586
::Flowbite::Input::Field
8687
end
8788

89+
protected
90+
8891
# Returns the HTML to use for the hint element if any
89-
def hint
92+
def default_hint
9093
return unless hint?
9194

92-
render(Flowbite::Input::Hint.new(
95+
component = Flowbite::Input::Hint.new(
9396
attribute: @attribute,
9497
form: @form,
95-
hint: @hint,
96-
hint_attributes: {id: id_for_hint_element}
97-
))
98+
options: default_hint_options
99+
).with_content(default_hint_content)
100+
render(component)
98101
end
99102

100-
protected
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+
hint_options = @hint.dup
116+
hint_options.delete(:content)
117+
118+
{
119+
id: id_for_hint_element
120+
}.merge(hint_options[:options] || {})
121+
end
101122

102123
# Returns a Hash with the default attributes to apply to the input element.
103124
#

app/components/flowbite/input_field/checkbox.rb

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,15 @@ def input_component
1515
def hint
1616
return unless hint?
1717

18-
render(
19-
Flowbite::Input::Hint.new(
20-
attribute: @attribute,
21-
form: @form,
22-
hint_attributes: {
23-
class: hint_classes,
24-
id: id_for_hint_element
25-
},
26-
hint: @hint
27-
)
28-
)
18+
component = Flowbite::Input::Hint.new(
19+
attribute: @attribute,
20+
form: @form,
21+
options: {
22+
class: hint_classes,
23+
id: id_for_hint_element
24+
}
25+
).with_content(@hint)
26+
render(component)
2927
end
3028

3129
# Returns the HTML to use for the label element

app/components/flowbite/input_field/radio_button.rb

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,15 @@ def default_label_options
4343
def hint
4444
return unless hint?
4545

46-
render(
47-
Flowbite::Input::Hint.new(
48-
attribute: @attribute,
49-
form: @form,
50-
hint_attributes: {
51-
class: hint_classes,
52-
id: id_for_hint_element
53-
},
54-
hint: @hint
55-
)
56-
)
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)
5755
end
5856

5957
private

demo/test/components/previews/input_field_preview.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def helper_text
8181
Flowbite::InputField::Text.new(
8282
attribute: :email,
8383
form: form,
84-
hint: "We’ll never share your details."
84+
hint: {content: "We’ll never share your details."}
8585
)
8686
)
8787
end

test/components/flowbite/input_field_test.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,25 @@ def setup
1212
end
1313

1414
def test_renders_a_hint
15-
render_inline(Flowbite::InputField.new(form: @form, attribute: :title, hint: "What's the title?"))
15+
render_inline(Flowbite::InputField.new(form: @form, attribute: :title, hint: {content: "What's the title?"}))
1616

1717
assert_selector("input[type='text'][value='The Great Gatsby']")
1818
assert_selector("p", text: "What's the title?")
1919
end
2020

2121
def test_adds_aria_attributes_for_hint
22-
render_inline(Flowbite::InputField.new(form: @form, attribute: :title, hint: "What's the title?"))
22+
render_inline(Flowbite::InputField.new(form: @form, attribute: :title, hint: {content: "What's the title?"}))
2323

2424
assert_selector("input[aria-describedby='book_title_hint']")
2525
assert_selector("p#book_title_hint", text: "What's the title?")
2626
end
2727

28+
def test_adds_extra_attributes_to_hint
29+
render_inline(Flowbite::InputField.new(form: @form, attribute: :title, hint: {content: "What's the title?", options: {class: "custom-hint-class"}}))
30+
31+
assert_selector("p#book_title_hint.custom-hint-class", text: "What's the title?")
32+
end
33+
2834
def test_renders_an_input_element
2935
render_inline(Flowbite::InputField.new(form: @form, attribute: :title))
3036

test/components/input/hint_test.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,21 @@ def setup
1212
end
1313

1414
def test_renders_a_hint
15-
render_inline(Flowbite::Input::Hint.new(form: @form, attribute: :title, hint: "What's the title?"))
15+
render_inline(Flowbite::Input::Hint.new(form: @form, attribute: :title)) { "What's the title?" }
1616

1717
assert_component_rendered
1818
assert_selector("p.text-sm.text-gray-500", text: "What's the title?")
1919
end
2020

21-
def test_accepts_custom_attributes
22-
render_inline(Flowbite::Input::Hint.new(form: @form, attribute: :title, hint: "What's the title?", hint_attributes: {id: "custom_hint"}))
21+
def test_accepts_custom_id
22+
render_inline(Flowbite::Input::Hint.new(form: @form, attribute: :title, options: {id: "custom_hint"})) { "What's the title?" }
2323

2424
assert_selector("p#custom_hint.text-sm.text-gray-500", text: "What's the title?")
2525
end
26+
27+
def test_accepts_custom_attributes
28+
render_inline(Flowbite::Input::Hint.new(form: @form, attribute: :title, options: {class: "custom-class"})) { "What's the title?" }
29+
30+
assert_selector("p.custom-class", text: "What's the title?")
31+
end
2632
end

test/components/input_field/file_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ def test_renders_a_label
2424
end
2525

2626
def test_renders_a_hint
27-
render_inline(Flowbite::InputField::File.new(form: @form, attribute: :avatar, hint: "Upload your profile picture"))
27+
render_inline(Flowbite::InputField::File.new(form: @form, attribute: :avatar, hint: {content: "Upload your profile picture"}))
2828

2929
assert_selector("input[type='file']")
3030
assert_selector("p", text: "Upload your profile picture")
3131
end
3232

3333
def test_adds_aria_attributes_for_hint
34-
render_inline(Flowbite::InputField::File.new(form: @form, attribute: :avatar, hint: "Upload your profile picture"))
34+
render_inline(Flowbite::InputField::File.new(form: @form, attribute: :avatar, hint: {content: "Upload your profile picture"}))
3535

3636
assert_selector("input[aria-describedby='user_avatar_hint']")
3737
assert_selector("p#user_avatar_hint", text: "Upload your profile picture")

test/components/input_field/password_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ def test_renders_a_label
2424
end
2525

2626
def test_renders_a_hint
27-
render_inline(Flowbite::InputField::Password.new(form: @form, attribute: :password, hint: "Enter a secure password"))
27+
render_inline(Flowbite::InputField::Password.new(form: @form, attribute: :password, hint: {content: "Enter a secure password"}))
2828

2929
assert_selector("input[type='password']")
3030
assert_selector("p", text: "Enter a secure password")
3131
end
3232

3333
def test_adds_aria_attributes_for_hint
34-
render_inline(Flowbite::InputField::Password.new(form: @form, attribute: :password, hint: "Enter a secure password"))
34+
render_inline(Flowbite::InputField::Password.new(form: @form, attribute: :password, hint: {content: "Enter a secure password"}))
3535

3636
assert_selector("input[aria-describedby='user_password_hint']")
3737
assert_selector("p#user_password_hint", text: "Enter a secure password")

test/components/input_field/url_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ def test_renders_a_label
2424
end
2525

2626
def test_renders_a_hint
27-
render_inline(Flowbite::InputField::Url.new(form: @form, attribute: :website_url, hint: "Enter your website URL"))
27+
render_inline(Flowbite::InputField::Url.new(form: @form, attribute: :website_url, hint: {content: "Enter your website URL"}))
2828

2929
assert_selector("input[type='url']")
3030
assert_selector("p", text: "Enter your website URL")
3131
end
3232

3333
def test_adds_aria_attributes_for_hint
34-
render_inline(Flowbite::InputField::Url.new(form: @form, attribute: :website_url, hint: "Enter your website URL"))
34+
render_inline(Flowbite::InputField::Url.new(form: @form, attribute: :website_url, hint: {content: "Enter your website URL"}))
3535

3636
assert_selector("input[aria-describedby='user_website_url_hint']")
3737
assert_selector("p#user_website_url_hint", text: "Enter your website URL")

0 commit comments

Comments
 (0)