Add datalist_tag to create datalist form elements#52137
Conversation
|
First time of even hearing of this tag, is it common enough that every Rails application could use it? |
|
To be honest, this is the first time I've used it. Looking for a method to generate it in the documentation/API was natural, but I could not find a solution there. Datalist is a simple native alternative for JavaScript solutions like Select2. Not perfect, but good enough. Rails has many methods to generate standard HTML form elements. I'd be glad to find a solution for |
Is there any guidance about what kind of tags are expected to have view helpers? |
|
Note: you can also use (this won't include special I think initially I was also unsure about the usefulness of a new tag, but handling option arrays/hashes like |
|
Thanks, @skipkayhil. I was not aware of these The equivalent code with tag.datalist(
options_for_select(
['Argentina', ['Brazil', { class: 'brazilian_option' }], ['Chile', 'CL', { disabled: true }]]
),
id: 'countries_datalist', class: 'sa-countries-sample'
)Shorter, but still not a natural interface. What do you guys think? Thanks. |
|
@zzak for what it's worth, I've seen it being used a handful of times on my client projects. I'm not sure if that is enough to justify a specialized helper, given that's possible to write something with |
|
I'm not convinced this meets the criteria to be included in Rails, but will defer to other members. Please feel free to post your PR on discord if you don't get any other feedback in some time. |
|
What is the criteria to be included, @zzak ? This is probably related to @andresakata's question. I'll wait for a while and ping it there if needed. Thanks! |
I would guess that new view helpers are not added often, the most recent ones I can think of are the
This is just based on my experience, not my decision, but it hopefully gives you some idea of expectations. Your experience may be different from mine. I wish the criteria for a new feature was a bit more explicit, but something to aim for:
Usually if one of those is true, you will get feedback on the other. e.g. if the idea is solid but the API needs work, or if it seems like a nice feature but who is it for? Being able to promote your idea is important, and I would say this example is maybe not sufficient:
Who does it save that code for? Is it only being used in rare occasions, or is it being called everywhere? Sorry that is kind of a brain dump. |
|
Thanks for clarifying, @zzak. Rethinking about the things you said and the proposed feature, I'd say:
In my opinion, these are the main considerations to add it. |
5e175c4 to
d8d905a
Compare
|
+1 for making this a proper helper. The I'd like to add that browser support for datalist in combination with text fields is practically universal across browsers, the only major browser (> 1% market share) with limited support is Firefox: it has no support for datalists on date and time fields. |
|
I've also been looking for this... It's not exactly new or unusual to need a combobox input, i.e. a select box that also allows a write-in option (easier to implement as the opposite: a text box that allows selection). This idea has been part of surveys and ballots for centuries already, in terms of programming languages Visual Basic had it in 2003 already, and I'm amazed that it has taken the HTML standard so long to catch up. Now it's supported by all browsers (including Firefox) though and W3Schools is educating developers about it. It would be great for Rails to have a native way to do it with a familiar syntax resembling select_tag. I just had to code my own Formtastic input for it... |
|
While a datalist helps with a basic autocomplete, I just want to point out that it does not limit the values to what's on the list. Users still can submit whatever they want. Not for or against this PR, just a reminder. |
Yes, that's the point of it. Using this for autocomplete is a secondary case I guess, but the main use of a combobox has always been the "select or write-in", e.g. when users can add tags to their blog posts and they can choose either an existing tag or write in a new one. Right now, a common workaround is to offer an "Other" or "New" option in the select and that causes a text field to appear, but that's worse UX than what Visual Basic offered in the early 2000s. |
d8d905a to
55a26d6
Compare
55a26d6 to
68f84c4
Compare
Motivation / Background
In order to generate an HTML datalist element with Rails I needed the following code:
It would be great to have a simple way to do that.
Detail
This Pull Request adds
datalist_tag: