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

Make vModelCheckbox allow Set as model (similar to Array) #1946

Closed
floj opened this issue Aug 23, 2020 · 2 comments · Fixed by #1957
Closed

Make vModelCheckbox allow Set as model (similar to Array) #1946

floj opened this issue Aug 23, 2020 · 2 comments · Fixed by #1957
Labels
has workaround A workaround has been found to avoid the problem ✨ feature request New feature or request

Comments

@floj
Copy link

floj commented Aug 23, 2020

What problem does this feature solve?

Currently it is possible to bind a checkbox to an array and let Vue populate the array with the checked values. A similar behavior for a native Set as a model would be nice.

The Set API is more accessible in some cases, especially when checking (Set.has vs. Array.find/Array.indexOf) and removing (Set.delete vs. Array.splice/Array.filter) elements from the collection.

What does the proposed API look like?

The API stays the same, just the type of the data model is different. Picking up the example from the docs:

<div id="v-model-multiple-checkboxes">
  <input type="checkbox" id="jack" value="Jack" v-model="checkedNames" />
  <label for="jack">Jack</label>
  <input type="checkbox" id="john" value="John" v-model="checkedNames" />
  <label for="john">John</label>
  <input type="checkbox" id="mike" value="Mike" v-model="checkedNames" />
  <label for="mike">Mike</label>
  <br />
  <span>Checked names: {{ checkedNames }}</span>
</div>
Vue.createApp({
  data() {
    return {
      checkedNames: new Set()
    }
  }
}).mount('#v-model-multiple-checkboxes')
@CyberAP
Copy link
Contributor

CyberAP commented Aug 24, 2020

You can use a factory pattern for this:

const createSetModel = name => ({
  get() { return Array.from(this[name]) },
  set(value) { this[name] = new Set(value) }
})

export default {
  data() {
    return {
      value: new Set()
    }
  },
  computed: {
    model: createSetModel('value')
  }
}

Example: https://codesandbox.io/s/recursing-brook-6n41k?file=/src/App.vue

@yyx990803 yyx990803 added ✨ feature request New feature or request has workaround A workaround has been found to avoid the problem labels Aug 24, 2020
@floj
Copy link
Author

floj commented Aug 31, 2020

@CyberAP Thanks for the heads up, this of cause works as expected.
I think its worth considering to merge #1957 because having Set as a model working without a computed property saved some lines of code and eliminates the need the wrap the datas property.

@github-actions github-actions bot locked and limited conversation to collaborators Nov 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
has workaround A workaround has been found to avoid the problem ✨ feature request New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants