Choices: Allow choice value to be 0

Created on 9 Dec 2017  路  6Comments  路  Source: Choices-js/Choices

I have a choice which value is 0, and it seems like nothing happens when I click this option. ( It does not trigger the change event )

const choices = [{
  value: 0,
  label: 'Label 1'
}]

I tried to change the value to any number other than zero, and it works fine.

Most helpful comment

Looks like this is caused by this line: https://github.com/jshjohnson/Choices/blob/master/assets/scripts/src/choices.js#L1142

Anything 'falsey' will not trigger the change event (ie; 0 or an empty string). I'm not sure what the original reason for this check was, but it seems like it should explicitly be looking for null or undefined instead.

All 6 comments

Looks like this is caused by this line: https://github.com/jshjohnson/Choices/blob/master/assets/scripts/src/choices.js#L1142

Anything 'falsey' will not trigger the change event (ie; 0 or an empty string). I'm not sure what the original reason for this check was, but it seems like it should explicitly be looking for null or undefined instead.

You are right. Thanks.

Just encountered this issue myself, had to mess around with several implementations to work around this issue.

It would be nice to allow any value to be used for choices. Even null or undefined. For example, in our application we have some parameters which can be set or unset, so we give user options like:

[
    // Here we'd like to use `value: null` as this is consistent with our data model, 
    // but now we have to use -1 as a value and perform conversions `null` <--> -1 every 
    // time we need to sync data model and dropdown.
    {value: -1, label: '[Not used]'},
    {value: 1, label: 'Option 1'},
    {value: 2, label: 'Option 2'}
]

Looks like the issue with 0 value is fixed by #349, but it still restricts null or undefined values :)

Great to see this is fixed! Would it be possible to cherry pick this into master as a patch release? More than happy to submit a PR to master with just the changes from #349 if thats helpful :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jkrehm picture jkrehm  路  4Comments

ckotzbauer picture ckotzbauer  路  5Comments

zslabs picture zslabs  路  6Comments

jchamb picture jchamb  路  4Comments

notflip picture notflip  路  3Comments