aboutsummaryrefslogtreecommitdiff
path: root/src/hocs/with_list/with_list.js
blob: 21aa288b18a7c60b3ac3a441119cdbec432c1942 (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
import Vue from 'vue'
import map from 'lodash/map'

const defaultEntryPropsGetter = entry => ({ entry })
const defaultKeyGetter = entry => entry.id

const withList = (Component, getEntryProps = defaultEntryPropsGetter, getKey = defaultKeyGetter) => {
  return Vue.component('withList', {
    render (createElement) {
      return (
        <div class="with-list">
          {map(this.entries, (entry, index) => {
            const props = {
              key: getKey(entry, index),
              ...this.$props.entryProps,
              ...getEntryProps(entry, index)
            }
            return <Component {...{ attrs: props }} />
          })}
        </div>
      )
    },
    props: ['entries', 'entryProps']
  })
}

export default withList