Skip to content

Commit 3ca3c29

Browse files
committed
Render hint as a slot
This changes the behavior and implementation of Flowbite::InputField#hint to be in line with #label and #input.
1 parent d2b3998 commit 3ca3c29

File tree

6 files changed

+33
-13
lines changed

6 files changed

+33
-13
lines changed

app/components/flowbite/input_field.rb

Lines changed: 24 additions & 4 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,38 @@ 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

9295
component = Flowbite::Input::Hint.new(
9396
attribute: @attribute,
9497
form: @form,
95-
hint_attributes: {id: id_for_hint_element}
96-
).with_content(@hint)
98+
hint_attributes: default_hint_options
99+
).with_content(default_hint_content)
97100
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+
hint_options = @hint.dup
115+
hint_options.delete(:content)
116+
117+
{
118+
id: id_for_hint_element
119+
}.merge(hint_options || {})
120+
end
101121

102122
# Returns a Hash with the default attributes to apply to the input element.
103123
#

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ 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?")

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)