aboutsummaryrefslogtreecommitdiff
path: root/src/components/selectable_list/selectable_list.js
blob: 7a7241449e2fd1d99b71a42ba8028ca00de1f5f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import Checkbox from '../checkbox/checkbox.js'

const SelectableList = {
  components: {
    Checkbox
  },
  props: {
    items: {
      type: Array,
      default: () => []
    },
    getKey: {
      type: Function,
      default: item => item
    }
  },
  data () {
    return {
      selected: []
    }
  },
  methods: {
    toggle (checked, item) {
      const oldChecked = this.isSelected(item)
      if (checked !== oldChecked) {
        const key = this.getKey(item)
        if (checked) {
          this.selected.push(key)
        } else {
          this.selected.splice(this.selected.indexOf(key), 1)
        }
      }
    },
    isSelected (item) {
      return this.selected.indexOf(this.getKey(item)) !== -1
    }
  }
}

export default SelectableList