Skip to content

Commit

Permalink
enh(data-iterable): added synced expanded prop
Browse files Browse the repository at this point in the history
allows one to modify expansion outside of data-iterable components

closes #2890
  • Loading branch information
nekosaur committed May 19, 2018
1 parent a11be7a commit ca56a29
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/mixins/data-iterable.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default {
sortBy: null,
totalItems: 0
},
expanded: {},
expandedKeys: {},
actionsClasses: 'v-data-iterator__actions',
actionsRangeControlsClasses: 'v-data-iterator__actions__range-controls',
actionsSelectClasses: 'v-data-iterator__actions__select',
Expand Down Expand Up @@ -151,6 +151,10 @@ export default {
pagination: {
type: Object,
default: () => {}
},
expanded: {
type: Array,
default: () => ([])
}
},

Expand Down Expand Up @@ -222,7 +226,14 @@ export default {
})
},
'computedPagination.sortBy': 'resetPagination',
'computedPagination.descending': 'resetPagination'
'computedPagination.descending': 'resetPagination',
expanded (items) {
this.expandedKeys = {}
const keys = items.map(item => item[this.itemKey])
for (let i = 0; i < keys.length; i++) {
this.$set(this.expandedKeys, keys[i], true)
}
}
},

methods: {
Expand Down Expand Up @@ -254,7 +265,7 @@ export default {
return this.selected[getObjectValueByPath(item, this.itemKey)]
},
isExpanded (item) {
return this.expanded[getObjectValueByPath(item, this.itemKey)]
return this.expandedKeys[getObjectValueByPath(item, this.itemKey)]
},
filteredItemsImpl (...additionalFilterArgs) {
if (this.totalItems) return this.items
Expand Down Expand Up @@ -327,18 +338,20 @@ export default {
})

Object.defineProperty(props, 'expanded', {
get: () => this.expanded[itemKey],
get: () => this.expandedKeys[itemKey],
set: value => {
if (itemKey == null) {
consoleWarn(`"${keyProp}" attribute must be defined for item`, this)
}

if (!this.expand) {
for (const key in this.expanded) {
this.expanded.hasOwnProperty(key) && this.$set(this.expanded, key, false)
for (const key in this.expandedKeys) {
this.expandedKeys.hasOwnProperty(key) && this.$set(this.expandedKeys, key, false)
}
}
this.$set(this.expanded, itemKey, value)
this.$set(this.expandedKeys, itemKey, value)

this.$emit('update:expanded', this.items.filter(item => this.expandedKeys[item[this.itemKey]]))
}
})

Expand Down

0 comments on commit ca56a29

Please sign in to comment.