Skip to content
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

Allow options with no value #77

Closed
philippkuehn opened this issue Nov 9, 2016 · 28 comments
Closed

Allow options with no value #77

philippkuehn opened this issue Nov 9, 2016 · 28 comments

Comments

@philippkuehn
Copy link

Hey, I want to use an option with no value as a placeholder. But this doesn't work. The option will not be visible with choices.js.

<select>
  <option value selected>Please Choose…</option>
  <option value="one">One</option>
  <option value="two">Two</option>
  <option value="three">Three</option>
</select>

The select is empty and I'm not able to select this field anymore.
jmoqt7t60l
1xyzmhezme

This is valid HTML so I'm a bit confused why this isn't working.

@jshjohnson
Copy link
Collaborator

Hi,

Out of interest, if you pass a value to the placeholder option, does this work?

If so, this will be a really easy fix.

Thanks

@philippkuehn
Copy link
Author

No, unfortunately not 🙁

@jshjohnson
Copy link
Collaborator

jshjohnson commented Nov 14, 2016

I'm working on a fix that accepts a blank value attribute. That being said, what do you think the expected behaviour should be for placeholder items? Should they behave like disabled options or should they not appear in the dropdown list at all?

Thanks :)

@philippkuehn
Copy link
Author

philippkuehn commented Nov 14, 2016

That's a tough question. I think you always have to think about accessibility. Custom selects should work 100% if I drop all the javascript stuff.

So what I want is a javascript and non-javascript solution to:

  • have a placeholder with no value
  • after selecting some value I want to be able to re-select the placeholder value (for non-required selects)

So maybe you don't have to change the way choices works with empty values, if the placeholder options works properly. In #53 there is a nice suggestion to fix the current issue with the placeholder option:

const choices = new Choices(elements, {
    placeholderValue: 'Please Choose…',
    searchPlaceholderValue: 'Search…',
    removeItemButton: true,
}

Or maybe you could do something like this to recognize a placeholder option (the placeholder should not be in the dropdown):

<select>
  <option value data-choices-placeholder>Please Choose…</option>
  <option value="one">One</option>
  <option value="two">Two</option>
  <option value="three">Three</option>
</select>

In general I don't think it's a good idea that placeholder items behave like disabled fields because not all selects are required fields and otherwise you are not able to un-select an already selected field.

What do you think? 🤔

@lewishowles
Copy link

lewishowles commented Nov 17, 2016

Just to jump in here, found this recently after trying to move a new project to Vue.js and loving it so far. I came from using Selectize in a previous project and I'm having a much better time. (I had to re-write the CSS but that's just because I like to make things difficult!)

Jumping in with some opinions on this:

  • Agree accessibility is paramount. The 'please select' choice should really be in the vanilla select box.
  • Also agree it should be there in the dropdown as a choice - because someone might have selected a value incorrectly and not actually know the answer to what you're asking
  • It should always appear first in the list, even with sorting.
  • Perhaps it should get its own class, so we can style it differently (e.g. choices__item--placeholder
  • The complex part is what happens if there are multiple empty value choices. I think it should probably be allowed, and all behave the same - i.e. all appear at the top (sorted or not depending on options), all with the placeholder class.
  • For multi-select, I'm thinking the empty choice shouldn't appear in the dropdown, and should just be a placeholder visible in choices__inner if there is no selected item, while all but the first empty choice are ignored (as it doesn't make sense to me to have multiple empty choice in this sense, as reverting to an empty state is just a case of deleting the choices)

@jshjohnson
Copy link
Collaborator

Thanks for all your feedback. I think I'm going to experiment with using the placeholder attribute on option elements and then adding a placeholder class based on the existence of that attribute. This would be for single select elements only though as I don't think this is necessary for multiple selects as you can set the placeholder on the input.

@jbanety
Copy link

jbanety commented Nov 21, 2016

Hi,
It's also important to keep option with empty values in the list.
For example, I'm using empty values in select to filter records in DB.
Empty values are for setting my filter to "ALL RECORDS PLZ" with no filter...

Looks like this :
State :

  • Archived (value = -1)
  • Deleted (value = -2)
  • Published (value = 1)
  • Unpublished (value = 0)
  • All (value = '')

I haven't any placeholder. The current value is always selected for the filter.

@zslabs
Copy link
Contributor

zslabs commented Nov 22, 2016

Will be watching this one closely, great ideas everyone!

@jshjohnson
Copy link
Collaborator

jshjohnson commented Nov 24, 2016

Plan:

  • Allow options with no/empty values (but with a label - single/multiple select)
  • Remove current implementation of single select placeholders (single select only)
  • Set class based on options with a 'placeholder' attribute (single select only)
  • Style placeholder class (single select only)
  • Order placeholder item first in list

@jbanety
Copy link

jbanety commented Nov 25, 2016

👍

@zslabs
Copy link
Contributor

zslabs commented Dec 12, 2016

@jshjohnson Any testing I can do to help out with this one? Thanks!

@jshjohnson
Copy link
Collaborator

Hi @zslabs,

I'm uber busy at the moment at work - I don't think I'm going to make much progress on this until after New Year. I'm happy to review any pull requests though.

Apologies!

@anthonyav
Copy link

@jshjohnson Hello did you you have time since your last comment?

@jshjohnson
Copy link
Collaborator

jshjohnson commented Jan 23, 2017

I have implemented all parts of this feature besides forcing a placeholder to appear first in the choice list.

Experiment on the following branch: git/feature/select-placeholders

@benjaminprojas
Copy link

I'm looking forward to this update! Glad to see its being actively worked on.

@adammockor
Copy link
Contributor

So, what needs to happen to get this done? I am willing to help with this if it's possible.

@jshjohnson
Copy link
Collaborator

Hi @adammockor,

See my comment here: #77 (comment)

Any help would be appreciated :D

@jshjohnson
Copy link
Collaborator

Thanks to @adammockor, placeholders are now appearing first!

It would be really good if people could test feature/select-placeholders and flag any issues before I draft a new release.

Thanks

@adammockor
Copy link
Contributor

So maybe some kind of prerelease could help with testing?

@jshjohnson
Copy link
Collaborator

Good idea, pre-release can be found here: https://github.com/jshjohnson/Choices/releases/tag/v3.0.0-beta

@adammockor
Copy link
Contributor

I was thinking more like npm release. Although you can point package.json to specific tag in git, there is missing new build. So it's not easily pickable.

@chopfitzroy
Copy link

Hey @jshjohnson,

Looking at the new release will it be possible to set a placeholder via the config object passed to new Choices? I am working with some CMS generated forms and adding a literal placeholder option to the select is proving to be a mission.

Cheers.

@jshjohnson
Copy link
Collaborator

jshjohnson commented Apr 19, 2017

@CrashyBang You can do that already via the placeholderValue config option :)

@chopfitzroy
Copy link

Hey @jshjohnson,

Sorry I should have explained myself a little better, I am currently using the placeholderValue to set the text on the dropdown search input :).

What I am after is the placeholder="This is a search placeholder" functionality used on the first Single select input example, is it possible to do this via the config object?

Cheers.

@jshjohnson
Copy link
Collaborator

@CrashyBang Ah I see what you mean. There was a good suggestion here to make a new config option where you can define the placeholder of the search input separately. I'll look into this!

@rafaelrenanpacheco
Copy link

Hi there!

Is there any progress on this issue?

Being able to set the searchPlaceholderValue will be a good addition to this project. So far the search text must be the same as the input placeholder, which isn't very user friendly.

Best regards,
Rafael Pacheco.

@jshjohnson
Copy link
Collaborator

jshjohnson commented Aug 3, 2017

Hi all!

As of version 3.0.0, Choices now supports setting an option with no value as a placeholder (info here) as well as a searchPlaceholderValue config option!

Sorry for the long wait 🙈

Thanks for your help

@travist
Copy link
Contributor

travist commented Sep 9, 2017

We also may need this pull request, which provides a pseudo-placeholder for single selects.

#248

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests