Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(VDataTable): highlight filter matches #21073

Open
wants to merge 11 commits into
base: dev
Choose a base branch
from
3 changes: 3 additions & 0 deletions packages/vuetify/src/components/VDataTable/VDataTable.sass
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,6 @@

&-active
color: $data-table-header-mobile-chip-icon-color-active

.v-data-table__mask
background: rgb(var(--v-theme-on-surface-variant))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way we can make this more flexible?

<v-data-table :headers="headers" :items="items" :search="search" class="bg-primary" item-value="name" />

image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is bg-primary class without additional theme="dark" a valid use case for v-data-table?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it fixes it but I hate that fix. Mainly because when we say theme="dark", we're saying use those colors. So now everything inside of that component would operate with a different palette.

3 changes: 2 additions & 1 deletion packages/vuetify/src/components/VDataTable/VDataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export const VDataTable = genericComponent<new <T extends readonly any[], V>(
const { items } = useDataTableItems(props, columns)

const search = toRef(props, 'search')
const { filteredItems } = useFilter(props, items, search, {
const { filteredItems, getMatches } = useFilter(props, items, search, {
transform: item => item.columns,
customKeyFilter: filterFunctions,
})
Expand Down Expand Up @@ -262,6 +262,7 @@ export const VDataTable = genericComponent<new <T extends readonly any[], V>(
{ ...attrs }
{ ...dataTableRowsProps }
items={ paginatedItems.value }
getMatches={ getMatches }
v-slots={ slots }
/>
)}
Expand Down
10 changes: 9 additions & 1 deletion packages/vuetify/src/components/VDataTable/VDataTableRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useHeaders } from './composables/headers'
import { useSelection } from './composables/select'
import { useSort } from './composables/sort'
import { makeDisplayProps, useDisplay } from '@/composables/display'
import { highlightResult } from '@/composables/filter'

// Utilities
import { toDisplayString, withModifiers } from 'vue'
Expand All @@ -34,6 +35,7 @@ export const makeVDataTableRowProps = propsFactory({
index: Number,
item: Object as PropType<DataTableItem>,
cellProps: [Object, Function] as PropType<CellProps<any>>,
getMatches: Function,
onClick: EventProp<[MouseEvent]>(),
onContextmenu: EventProp<[MouseEvent]>(),
onDblclick: EventProp<[MouseEvent]>(),
Expand Down Expand Up @@ -162,7 +164,13 @@ export const VDataTableRow = genericComponent<new <T>(
)
}

const displayValue = toDisplayString(slotProps.value)
const displayValue = props.getMatches
? highlightResult(
'v-data-table',
toDisplayString(slotProps.value),
props.getMatches(item)?.[column.key!]
)
: toDisplayString(slotProps.value)

return !mobile.value ? displayValue : (
<>
Expand Down
2 changes: 2 additions & 0 deletions packages/vuetify/src/components/VDataTable/VDataTableRows.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const makeVDataTableRowsProps = propsFactory({
},
rowProps: [Object, Function] as PropType<RowProps<any>>,
cellProps: [Object, Function] as PropType<CellProps<any>>,
getMatches: Function,

...makeDisplayProps(),
}, 'VDataTableRows')
Expand Down Expand Up @@ -164,6 +165,7 @@ export const VDataTableRows = genericComponent<new <T>(
{ slots.item ? slots.item(itemSlotProps) : (
<VDataTableRow
{ ...itemSlotProps.props }
getMatches={ props.getMatches }
v-slots={ slots }
/>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export const VDataTableVirtual = genericComponent<new <T extends readonly any[],
const { items } = useDataTableItems(props, columns)

const search = toRef(props, 'search')
const { filteredItems } = useFilter(props, items, search, {
const { filteredItems, getMatches } = useFilter(props, items, search, {
transform: item => item.columns,
customKeyFilter: filterFunctions,
})
Expand Down Expand Up @@ -231,6 +231,7 @@ export const VDataTableVirtual = genericComponent<new <T extends readonly any[],
{ ...attrs }
{ ...dataTableRowsProps }
items={ displayItems.value }
getMatches={ getMatches }
>
{{
...slots,
Expand All @@ -247,6 +248,7 @@ export const VDataTableVirtual = genericComponent<new <T extends readonly any[],
ref={ itemRef }
key={ itemSlotProps.internalItem.index }
index={ itemSlotProps.internalItem.index }
getMatches={ getMatches }
v-slots={ slots }
/>
)
Expand Down
4 changes: 3 additions & 1 deletion packages/vuetify/src/composables/filter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ export function useFilter <T extends InternalItem> (
results.forEach(({ index, matches }) => {
const item = originalItems[index]
_filteredItems.push(item)
_filteredMatches.set(item.value, matches)
if (item.value !== undefined) {
_filteredMatches.set(item.value, matches)
}
})
filteredItems.value = _filteredItems
filteredMatches.value = _filteredMatches
Expand Down