akkoma-fe/src/components/selectable_list/selectable_list.js

41 lines
761 B
JavaScript
Raw Normal View History

2019-04-04 01:43:24 +00:00
import Checkbox from '../checkbox/checkbox.js'
const SelectableList = {
2019-04-04 01:43:24 +00:00
components: {
Checkbox
},
props: {
items: {
type: Array,
default: () => []
2019-04-04 03:26:13 +00:00
},
getKey: {
type: Function,
default: item => item
}
},
data () {
return {
selected: []
}
},
methods: {
toggle (checked, item) {
const oldChecked = this.isChecked(item)
if (checked !== oldChecked) {
const key = this.getKey(item)
if (checked) {
this.selected.push(key)
} else {
this.selected.splice(this.selected.indexOf(key), 1)
}
}
},
isChecked (item) {
return this.selected.indexOf(this.getKey(item)) !== -1
}
2019-04-04 01:43:24 +00:00
}
}
export default SelectableList