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

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

const withList = ({
  getEntryProps = defaultEntryPropsGetter,  // function to accept entry and index values and return props to be passed into the item component
  getKey = defaultKeyGetter                 // funciton to accept entry and index values and return key prop value
}) => (ItemComponent) => (
  Vue.component('withList', {
    props: [
      'entries',                            // array of entry
      'entryProps',                         // additional props to be passed into each entry
      'entryListeners'                      // additional event listeners to be passed into each entry
    ],
    render (createElement) {
      return (
        <div class="with-list">
          {map(this.entries, (entry, index) => {
            const props = {
              key: getKey(entry, index),
              props: {
                ...this.$props.entryProps,
                ...getEntryProps(entry, index)
              },
              on: this.$props.entryListeners
            }
            return <ItemComponent {...props} />
          })}
          {isEmpty(this.entries) && this.$slots.empty && <div class="with-list-empty-content faint">{this.$slots.empty}</div>}
        </div>
      )
    }
  })
)

export default withList