-
Notifications
You must be signed in to change notification settings - Fork 25
Improves select #33
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
base: main
Are you sure you want to change the base?
Improves select #33
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -69,6 +69,10 @@ def select(*collection, **attributes, &) | |
| Components::SelectField.new(self, attributes: attributes, collection: collection, &) | ||
| end | ||
|
|
||
| def multiple_select(*collection, **attributes, &) | ||
| Components::MultipleSelectField.new(self, attributes: attributes, collection: collection, &) | ||
| end | ||
|
|
||
| def title | ||
| key.to_s.titleize | ||
| end | ||
|
|
@@ -331,10 +335,33 @@ def false_option(&) | |
| end | ||
|
|
||
| protected | ||
|
|
||
| def map_options(collection) | ||
| OptionMapper.new(collection) | ||
| end | ||
| end | ||
|
|
||
| class MultipleSelectField < SelectField | ||
| def template(...) | ||
| # The HTML specification says when multiple parameter passed to select | ||
| # and all options got deselected web browsers do not send any value to | ||
| # server. Unfortunately this introduces a gotcha: if a User model has | ||
| # many roles and have role_ids accessor, and in the form that edits | ||
| # roles of the user the user deselects all roles from role_ids | ||
| # multiple select box, no role_ids parameter is sent. | ||
| input(type: "hidden", name: dom.name, value: "") | ||
| super(...) | ||
| end | ||
|
|
||
| protected | ||
|
|
||
| def field_attributes | ||
| super.merge( | ||
| name: "#{dom.name}[]", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The I haven't thought too much about exactly how this would be integrated, but if you're willing to bounce around a bit with these commits I think we can get there. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we prefer field.select(multiple: true) over field.multiple_select() i know i do. The 2nd link is broken, do you mean the Not entirely sure what you mean on how to use the collection I guess starting with radiobutton would be slightly easier as you don't have to deal with this particular issue about key naming as it is of singular value. As your suggestion here #13 Intuitively it is easier to have the element be responsible for both key and value 1 to 1. The code affecting value keys naming in superform. <select name="fruits[]" multiple>
<option value="apple" selected>Apple</option>
<option value="banana">Banana</option>
<option value="cherry" selected>Cherry</option>
</select>
<input type="checkbox" name="fruits[]" value="Apple">
<input type="checkbox" name="fruits[]" value="Banana">
<input type="checkbox" name="fruits[]" value="Cherry">
<input type="radio" name="fruit" value="Apple">
<input type="radio" name="fruit" value="Banana">
<input type="radio" name="fruit" value="Cherry"> |
||
| multiple: true | ||
| ) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also used the argument forwarding, but i realized that this was introduced in ruby 2.7 so then it needs to bump required_ruby_version in the gemspec. If you don't want to include that in this ticket then switch to *, **, & to catch all arguments and can forward explicit, or implicitly super without parenthesis. For these discussions we could include more code style guidance (e.g. rubocop)?