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

Can Vue add support for checkbox's indeterminate state? #4094

Closed
smalllong opened this issue Nov 3, 2016 · 9 comments
Closed

Can Vue add support for checkbox's indeterminate state? #4094

smalllong opened this issue Nov 3, 2016 · 9 comments

Comments

@smalllong
Copy link

Vue.js version

1.0.26

What is Expected?

The checkboxes binded with Vuejs can have THREE states: true, false, or indeterminate. For example, when binded with a checkbox and the value is "indeterminate", the checkbox's indeterminate property is set to true.

What is actually happening?

The checkboxes that binded with Vuejs only got two states: true or false.

@sirlancelot
Copy link

indeterminate doesn't have a DOM attribute equivalent. With that in mind, I don't think it's really Vue's concern. You could write your own directive to handle it quite easily though.

@sirlancelot
Copy link

sirlancelot commented Nov 3, 2016

I saw that you're requesting Vue 1.x so I made the one-line directive for you with an example:

http://codepen.io/sirlancelot/pen/BQBVvz

(I also added an untested, commented out Vue 2.x version)

@yyx990803
Copy link
Member

The problem is what value in JS should be used to represent this visual state - it may totally depend on your use case and app logic. So it's probably better to handle it in your own code, as @sirlancelot suggested.

@simplesmiler
Copy link
Member

@yyx990803 to be fair, checkboxes already support binding of true-value and false-value, maybe it worth adding indeterminate-value?

@simplesmiler
Copy link
Member

According to WC3, indeterminate is not a distinct state as checked and unchecked are. Instead it "obscures" the actual state of the checkbox without changing it. Thus, it's more of a candidate for :indeterminate="true" rather than :checked="undefined". But there are few things to consider.

  1. The :checked (and some other bindings) does not bind the checked attribute, because this attribute represents the default value, not the current value. But there's no "default value attribute" for indeterminate, so it would be hijacking to bind it.
  2. Because there's no "default value attribute" for indeterminate, DOM rehydration after SSR may be awkward.

@yyx990803 what's your final word on how should I proceed with this, core or userland?

@yyx990803
Copy link
Member

@simplesmiler I still think this belongs more in userland. In fact in 2.0 you can simply do:

<input type="checkbox" :indeterminate.prop="true">

In v1, a simple custom directive would do.

@rhyek
Copy link

rhyek commented Jul 18, 2017

Any way to set something like :indeterminate.prop with v-bind? Something like:

<input type="checkbox" v-bind="someFunc(id)">

someFunc (id) {
  return {
    checked: true,
    'indeterminate.prop': true
  }
}

@totalamd
Copy link

totalamd commented Oct 22, 2017

@rhyek, you can use this way to one-way binding:

<input type="checkbox" v-bind.prop="state(id)">
state(id) {
  return {
    checked: true,
    indeterminate: true
  }
}

I couldn't find a way how to create proper getter of someFunc() to get "checked" value.

@hartwork
Copy link

hartwork commented May 3, 2020

I have implemented and published a fully functional Vue.js component for a cycling tristate checkbox now. It supports click-cycling through all three states, v-model binding, form submission of the indeterminate value, customization of all values, and more. All supported properties are documented. I'll be happy about constructive feedback and bug reports over at https://github.com/hartwork/vue-tristate-checkbox . 🍻 Best, Sebastian

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

7 participants