Skip to content

Commit

Permalink
fix(VDataTable): allow grouping by nested keys
Browse files Browse the repository at this point in the history
fixes #10323
  • Loading branch information
KaelWD committed Jan 25, 2020
1 parent 1d794e7 commit c8e9239
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
26 changes: 26 additions & 0 deletions packages/vuetify/src/components/VData/__tests__/VData.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,32 @@ describe('VData.ts', () => {
}))
})

it('should group items by deep keys', async () => {
const render = jest.fn()
const items = [
{ id: 1, text: 'foo', foo: { bar: 'one' } },
{ id: 2, text: 'bar', foo: { bar: 'two' } },
{ id: 3, text: 'baz', foo: { bar: 'one' } },
]

const wrapper = mountFunction({
propsData: {
items,
groupBy: ['foo.bar'],
},
scopedSlots: {
default: render,
},
})

expect(render).toHaveBeenCalledWith(expect.objectContaining({
groupedItems: {
one: [items[0], items[2]],
two: [items[1]],
},
}))
})

it('should group items with a custom group function', async () => {
const render = jest.fn()
const items = [
Expand Down
7 changes: 4 additions & 3 deletions packages/vuetify/src/util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,10 @@ export function groupItems<T extends any = any> (
groupDesc: boolean[]
): Record<string, T[]> {
const key = groupBy[0]
return items.reduce((rv, x) => {
(rv[x[key]] = rv[x[key]] || []).push(x)
return rv
return items.reduce((acc, item) => {
const val = getObjectValueByPath(item, key)
;(acc[val] = acc[val] || []).push(item)
return acc
}, {} as Record<string, T[]>)
}

Expand Down

0 comments on commit c8e9239

Please sign in to comment.