Skip to content

Commit

Permalink
fix(VDataTable): default to first itemsPerPage option (#8578)
Browse files Browse the repository at this point in the history
default to first itemsPerPage option if itemsPerPage is not found in list of options

closes #8184
  • Loading branch information
nekosaur authored and johnleider committed Aug 22, 2019
1 parent 2277942 commit cb045a2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
7 changes: 5 additions & 2 deletions packages/vuetify/src/components/VData/VData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,11 @@ export default Vue.extend({
itemsPerPage (itemsPerPage: number) {
this.updateOptions({ itemsPerPage })
},
'internalOptions.itemsPerPage' (itemsPerPage: number) {
this.$emit('update:items-per-page', itemsPerPage)
'internalOptions.itemsPerPage': {
handler (itemsPerPage: number) {
this.$emit('update:items-per-page', itemsPerPage)
},
immediate: true,
},
sortBy (sortBy: string | string[]) {
this.updateOptions({ sortBy: wrapInArray(sortBy) })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default Themeable.extend({
someItems (): boolean {
return this.internalCurrentItems.some((i: any) => this.isSelected(i))
},
sanitizedFooterProps (): object {
sanitizedFooterProps (): Record<string, any> {
return camelizeObjectKeys(this.footerProps)
},
},
Expand Down
12 changes: 11 additions & 1 deletion packages/vuetify/src/components/VDataTable/VDataTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,18 @@ export default VDataIterator.extend({
headersWithoutCustomFilters (): TableHeader[] {
return this.computedHeaders.filter(header => !header.filter)
},
sanitizedHeaderProps (): object {
sanitizedHeaderProps (): Record<string, any> {
return camelizeObjectKeys(this.headerProps)
},
computedItemsPerPage (): number {
const itemsPerPage = this.options && this.options.itemsPerPage ? this.options.itemsPerPage : this.itemsPerPage
if (this.sanitizedFooterProps.itemsPerPageOptions && !this.sanitizedFooterProps.itemsPerPageOptions.includes(itemsPerPage)) {
const firstOption = this.sanitizedFooterProps.itemsPerPageOptions[0]
return typeof firstOption === 'object' ? firstOption.value : firstOption
}

return itemsPerPage
},
},

created () {
Expand Down Expand Up @@ -533,6 +542,7 @@ export default VDataIterator.extend({
...this.$props,
customFilter: this.customFilterWithColumns,
customSort: this.customSortWithHeaders,
itemsPerPage: this.computedItemsPerPage,
},
on: {
'update:options': (v: DataOptions, old: DataOptions) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,4 +398,28 @@ describe('VDataTable.ts', () => {

expect(wrapper.html()).toMatchSnapshot()
})

// https://github.com/vuetifyjs/vuetify/issues/8184
it('should default to first option in itemsPerPageOptions if it does not include itemsPerPage', async () => {
const itemsPerPage = jest.fn()
const options = jest.fn()
const wrapper = mountFunction({
propsData: {
headers: testHeaders,
items: testItems,
footerProps: {
itemsPerPageOptions: [5, 6],
},
},
listeners: {
'update:items-per-page': itemsPerPage,
'update:options': options,
},
})

expect(itemsPerPage).toHaveBeenCalledWith(5)
expect(options).toHaveBeenCalledWith(expect.objectContaining({
itemsPerPage: 5,
}))
})
})

0 comments on commit cb045a2

Please sign in to comment.