Allow hints on select fields

Just like we allow them on input fields.
This commit is contained in:
Javi Martín
2020-09-09 11:43:25 +02:00
parent e4c1a8cf45
commit cd418e45be
2 changed files with 30 additions and 1 deletions

View File

@@ -35,6 +35,13 @@ class ConsulFormBuilder < FoundationRailsHelper::FormBuilder
super(attribute, tag_value, { label: default_label }.merge(options)) super(attribute, tag_value, { label: default_label }.merge(options))
end end
def select(attribute, choices, options = {}, html_options = {})
label_with_hint(attribute, options.merge(label_options: label_options_for(options))) +
super(attribute, choices, options.merge(label: false, hint: nil), html_options.merge({
aria: { describedby: help_text_id(attribute, options) }
}))
end
private private
def custom_label(attribute, text, options) def custom_label(attribute, text, options)

View File

@@ -3,7 +3,8 @@ require "rails_helper"
describe ConsulFormBuilder do describe ConsulFormBuilder do
class DummyModel class DummyModel
include ActiveModel::Model include ActiveModel::Model
attr_accessor :title OPTIONS = %w[Good Bad Ugly].freeze
attr_accessor :title, :quality
end end
let(:builder) { ConsulFormBuilder.new(:dummy, DummyModel.new, ActionView::Base.new, {}) } let(:builder) { ConsulFormBuilder.new(:dummy, DummyModel.new, ActionView::Base.new, {}) }
@@ -30,6 +31,27 @@ describe ConsulFormBuilder do
end end
end end
describe "#select" do
it "renders the label and the select with the given options" do
render builder.select(:quality, DummyModel::OPTIONS)
expect(page).to have_css "label", count: 1
expect(page).to have_css "label", text: "Quality"
expect(page).to have_css "select", count: 1
expect(page).to have_css "option", count: 3
expect(page).to have_css "option", text: "Good"
expect(page).to have_css "option", text: "Bad"
expect(page).to have_css "option", text: "Ugly"
end
it "accepts hints" do
render builder.select(:quality, DummyModel::OPTIONS, hint: "Ugly is neither good nor bad")
expect(page).to have_css ".help-text", text: "Ugly is neither good nor bad"
expect(page).to have_css "select[aria-describedby='dummy_quality-help-text']"
end
end
attr_reader :content attr_reader :content
def render(content) def render(content)