Skip to content

Commit af20234

Browse files
authored
fix(VDataTable): allow filters on all columns (#21876)
1 parent d91dad0 commit af20234

File tree

2 files changed

+32
-46
lines changed

2 files changed

+32
-46
lines changed

packages/vuetify/src/composables/__tests__/filter.spec.ts

Lines changed: 28 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -64,30 +64,10 @@ describe('filter', () => {
6464
value: (s: string) => s === '1',
6565
}
6666
const items = [
67-
{
68-
title: 'foo',
69-
subtitle: 'bar',
70-
value: '1',
71-
custom: '1',
72-
},
73-
{
74-
title: 'fizz',
75-
subtitle: 'buzz',
76-
value: '1',
77-
custom: 'bar',
78-
},
79-
{
80-
title: 'foobar',
81-
subtitle: 'fizzbuzz',
82-
value: '2',
83-
custom: 'bar',
84-
},
85-
{
86-
title: 'buzz',
87-
subtitle: 'buzz',
88-
value: '1',
89-
custom: 'buzz',
90-
},
67+
{ title: 'foo', subtitle: 'bar', value: '1', custom: '1' },
68+
{ title: 'fizz', subtitle: 'buzz', value: '1', custom: 'bar' },
69+
{ title: 'foobar', subtitle: 'fizzbuzz', value: '2', custom: 'bar' },
70+
{ title: 'buzz', subtitle: 'buzz', value: '1', custom: 'buzz' },
9171
] as any
9272
const filterKeys = ['title', 'value', 'subtitle', 'custom']
9373

@@ -122,26 +102,10 @@ describe('filter', () => {
122102
value: (s: string) => s === '1',
123103
}
124104
const items = [
125-
{
126-
title: 'foo',
127-
subtitle: 'bar',
128-
value: '1',
129-
},
130-
{
131-
title: 'fizz',
132-
subtitle: 'buzz',
133-
value: '1',
134-
},
135-
{
136-
title: 'foobar',
137-
subtitle: 'fizzbuzz',
138-
value: '2',
139-
},
140-
{
141-
title: 'buzz',
142-
subtitle: 'buzz',
143-
value: '2',
144-
},
105+
{ title: 'foo', subtitle: 'bar', value: '1' },
106+
{ title: 'fizz', subtitle: 'buzz', value: '1' },
107+
{ title: 'foobar', subtitle: 'fizzbuzz', value: '2' },
108+
{ title: 'buzz', subtitle: 'buzz', value: '2' },
145109
] as any
146110
const filterKeys = ['title', 'value']
147111

@@ -161,14 +125,33 @@ describe('filter', () => {
161125
filterKeys,
162126
customKeyFilter,
163127
filterMode: 'intersection',
164-
})).toHaveLength(0)
128+
})).toHaveLength(2)
165129

166130
expect(filterItems(items, '', {
167131
filterKeys,
168132
customKeyFilter,
169133
filterMode: 'every',
170134
})).toHaveLength(2)
171135
})
136+
137+
// https://github.com/vuetifyjs/vuetify/pull/21876
138+
it('should return filtered rows when all columns have filters', () => {
139+
const customKeyFilter = {
140+
title: (s: string) => s.length < 5,
141+
subtitle: (s: string) => s.startsWith('b'),
142+
value: (s: any) => Number(s) > 0,
143+
}
144+
const items = [
145+
{ title: 'foo', subtitle: 'bar', value: 1 },
146+
{ title: 'fizz', subtitle: 'buzz', value: 1 },
147+
{ title: 'foobar', subtitle: 'fizzbuzz', value: 2 },
148+
] as any
149+
150+
expect(filterItems(items, '', {
151+
customKeyFilter,
152+
filterMode: 'intersection',
153+
})).toHaveLength(2)
154+
})
172155
})
173156

174157
describe('useFilter', () => {

packages/vuetify/src/composables/filter.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,15 @@ export function filterItems (
102102
let match: FilterMatch = -1
103103

104104
if ((query || customFiltersLength > 0) && !options?.noFilter) {
105+
let hasOnlyCustomFilters = false
106+
105107
if (typeof item === 'object') {
106108
if (item.type === 'divider' || item.type === 'subheader') {
107109
continue
108110
}
109111

110112
const filterKeys = keys || Object.keys(transformed)
113+
hasOnlyCustomFilters = filterKeys.length === customFiltersLength
111114

112115
for (const key of filterKeys) {
113116
const value = getPropertyFromItem(transformed, key)
@@ -146,7 +149,7 @@ export function filterItems (
146149
options?.filterMode === 'intersection' &&
147150
(
148151
customMatchesLength !== customFiltersLength ||
149-
!defaultMatchesLength
152+
(!defaultMatchesLength && customFiltersLength > 0 && !hasOnlyCustomFilters)
150153
)
151154
) continue
152155
}

0 commit comments

Comments
 (0)