-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Grouping labels & fields #36
Comments
Here are some syntax options for groups, which one do you prefer? Or maybe we can implement both? 1. Additional params to the existing Rails helpersWe could pass strings: <%= f.text_field :first_name, label: "First name", hint: "How should we call you?" %> <div>
<label for="user_first_name">First name</label>
<span>How should we call you?</span>
<input type="text" id="user_first_name" name"user[first_name]" />
</div> or booleans... in this case the label and the hint come from the locale files: <%= f.text_field :first_name, label: true, hint: true %> # config/locale/en.yml
helpers:
label:
user:
first_name: First name
hint:
user:
first_name: How should we call you? We can also pass hashes with a <%= f.text_field :first_name, label: { text: "First name" }, hint: { text: "How should we call you?" } %>
<%= f.text_field :first_name, label: { text: "First name", class: "my-custom-label" }, hint: { text: "How should we call you?", position: :after_input } %> <div>
<label for="user_first_name" class="my-custom-label">First name</label>
<input type="text" id="user_first_name" name"user[first_name]" />
<span>How should we call you?</span>
</div> 2. Using a
|
I personally prefer the second one |
After discussing it, we'll go for the second option (Using a While working on a first implementation, we hit the following roadblocks:
A potential solution to 1. 2. 3. is for the block to receive its own FormBuilder instance as an argument, so instead of: <%= f.group :first_name, hint: "How should we call you?" do %>
<%= f.text_field :first_name %>
<% end %> we would have <%= f.group :first_name, hint: "How should we call you?" do |g| %>
<%= g.text_field :first_name %>
<% end %> Then we could inject the For 4. we may need to create another helper, such as Feel free to contribute ideas 🙏 |
Hi, it would be great to have the "primitives" for this in the code... I created something like this. Maybe it is useful for anyone: https://gist.github.com/tmaier/22966c6bddac86e3612c8eddc072b919 |
Related to #127 |
I've been playing around with a slot-based solution for this. There are some downsides as the syntax is not exactly the same as using a proper FormBuilder.
It needs some more work (I have no idea, why it outputs an Admin instance), but I think there is some potential. |
A common use case when building forms is the need to group labels and fields, or multiple fields together. Let's discuss these cases.
A label + a field
It's the most common use case. That's for instance what is generated by Rails scaffolding:
When the field is a
check_box
or aradio_button
, you usually want to invert the label and the input:We could have a
ViewComponent::Form::GroupComponent
for this purpose.We could also add a
label
option to some helpers (see #16).Errors
When a field has errors, it's a good practice to display them next to the field. The errors could be handled by the
ViewComponent::Form::GroupComponent
.Hints
Some fields require additional information to help the user. The
ViewComponent::Form::GroupComponent
could accept ahint
option for this. See GOV.UK for an example implementation.A group of fields (and their labels)
The
<fieldset>
element is used for this.Rails does not provide a dedicated helper for this element.
We could have a
ViewComponent::Form::FieldsetComponent
for this purpose, or reuse theViewComponent::Form::GroupComponent
(but it would make it more complex).The text was updated successfully, but these errors were encountered: