yandere_fe/src/components/selectable_list/selectable_list.js

55 lines
1.0 KiB
JavaScript
Raw Normal View History

import List from '../list/list.vue'
2019-04-06 10:16:26 -07:00
import Checkbox from '../checkbox/checkbox.vue'
2019-04-03 18:43:24 -07:00
const SelectableList = {
2019-04-03 18:43:24 -07:00
components: {
List,
2019-04-03 18:43:24 -07:00
Checkbox
},
props: {
items: {
type: Array,
default: () => []
},
getKey: {
type: Function,
default: item => item.id
}
},
2019-04-03 20:26:13 -07:00
data () {
return {
selected: []
}
},
computed: {
allSelected () {
return !this.items.find(item => !this.isSelected(item))
}
},
2019-04-03 20:26:13 -07:00
methods: {
isSelected (item) {
return this.selected.indexOf(this.getKey(item)) !== -1
},
toggle (checked, item) {
const key = this.getKey(item)
const oldChecked = this.isSelected(key)
2019-04-03 20:26:13 -07:00
if (checked !== oldChecked) {
if (checked) {
this.selected.push(key)
} else {
this.selected.splice(this.selected.indexOf(key), 1)
}
}
},
toggleAll (value) {
if (value) {
this.selected = this.items.map(this.getKey)
} else {
this.selected = []
}
}
2019-04-03 18:43:24 -07:00
}
}
export default SelectableList