From c7dea71e81ce1419831377a15aa67d4e5b5b0261 Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Wed, 24 Jul 2024 14:47:42 +0800 Subject: [PATCH] feat(components): [table-column] add `filter-icon` slot (#17272) * feat(components): [table] add `filter-icon` prop * chore: remove * chore: remove * Update docs/en-US/component/table.md * feat: filter-icon slot * test: test case * chore: update * chore: update --- docs/en-US/component/table.md | 9 ++- .../components/table/__tests__/table.test.ts | 79 +++++++++++++++++++ .../components/table/src/filter-panel.vue | 6 +- .../table/src/table-column/render-helper.ts | 6 ++ .../table/src/table-header/index.ts | 25 ++++-- 5 files changed, 112 insertions(+), 13 deletions(-) diff --git a/docs/en-US/component/table.md b/docs/en-US/component/table.md index 1e94f99189..984e790cab 100644 --- a/docs/en-US/component/table.md +++ b/docs/en-US/component/table.md @@ -371,10 +371,11 @@ table/table-layout ### Table-column Slots -| Name | Description | Type | -| ------- | -------------------------------- | ---------------------------------------------------- | -| default | Custom content for table columns | ^[object]`{ row: any, column: any, $index: number }` | -| header | Custom content for table header | ^[object]`{ column: any, $index: number }` | +| Name | Description | Type | +| -------------------- | -------------------------------- | ---------------------------------------------------- | +| default | Custom content for table columns | ^[object]`{ row: any, column: any, $index: number }` | +| header | Custom content for table header | ^[object]`{ column: any, $index: number }` | +| filter-icon ^(2.7.8) | Custom content for filter icon | ^[object]`{ filterOpened: boolean }` | ## Type Declarations diff --git a/packages/components/table/__tests__/table.test.ts b/packages/components/table/__tests__/table.test.ts index dfd974e431..2410ab5af3 100644 --- a/packages/components/table/__tests__/table.test.ts +++ b/packages/components/table/__tests__/table.test.ts @@ -4,6 +4,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import ElCheckbox from '@element-plus/components/checkbox' import triggerEvent from '@element-plus/test-utils/trigger-event' import { rAF } from '@element-plus/test-utils/tick' +import { CaretBottom, CaretTop } from '@element-plus/icons-vue' import ElTable from '../src/table.vue' import ElTableColumn from '../src/table-column' import { @@ -439,6 +440,84 @@ describe('Table.vue', () => { }) }) + describe('filter filter-icon slot', () => { + let wrapper: VueWrapper + + beforeEach(async () => { + wrapper = mount({ + components: { + ElTable, + ElTableColumn, + CaretBottom, + CaretTop, + }, + template: ` + + + + + + + + + `, + + created() { + this.testData = getTestData() + }, + + methods: { + filterMethod(value, row) { + return value === row.director + }, + handleFilterChange(filters) { + this.filters = filters + }, + }, + }) + await doubleWait() + }) + + afterEach(() => wrapper.unmount()) + + it('render', () => { + expect( + wrapper.find('.el-table__column-filter-trigger') + ).not.toBeUndefined() + expect( + wrapper.find('.el-table__column-filter-trigger .bottom') + ).not.toBeUndefined() + }) + + it('click filter-trigger', async () => { + const btn = wrapper.find('.el-table__column-filter-trigger') + + btn.trigger('click') + await doubleWait() + expect( + wrapper.find('.el-table__column-filter-trigger .top') + ).not.toBeUndefined() + + btn.trigger('click') + await doubleWait() + expect( + wrapper.find('.el-table__column-filter-trigger .bottom') + ).not.toBeUndefined() + }) + }) + describe('events', () => { const createTable = function (prop = '') { return mount({ diff --git a/packages/components/table/src/filter-panel.vue b/packages/components/table/src/filter-panel.vue index fb4ecfac75..2753ebbed6 100644 --- a/packages/components/table/src/filter-panel.vue +++ b/packages/components/table/src/filter-panel.vue @@ -78,8 +78,10 @@ @click="showFilterPanel" > - - + + + + diff --git a/packages/components/table/src/table-column/render-helper.ts b/packages/components/table/src/table-column/render-helper.ts index 0470f8e484..96e2f33e98 100644 --- a/packages/components/table/src/table-column/render-helper.ts +++ b/packages/components/table/src/table-column/render-helper.ts @@ -124,6 +124,12 @@ function useRender( } } + if (slots['filter-icon']) { + column.renderFilterIcon = (scope) => { + return renderSlot(slots, 'filter-icon', scope) + } + } + let originRenderCell = column.renderCell // TODO: 这里的实现调整 if (column.type === 'expand') { diff --git a/packages/components/table/src/table-header/index.ts b/packages/components/table/src/table-header/index.ts index c3a3df9834..1d7410e3b5 100644 --- a/packages/components/table/src/table-header/index.ts +++ b/packages/components/table/src/table-header/index.ts @@ -224,14 +224,25 @@ export default defineComponent({ ] ), column.filterable && - h(FilterPanel, { - store, - placement: column.filterPlacement || 'bottom-start', - column, - upDataColumn: (key, value) => { - column[key] = value + h( + FilterPanel, + { + store, + placement: column.filterPlacement || 'bottom-start', + column, + upDataColumn: (key, value) => { + column[key] = value + }, }, - }), + { + 'filter-icon': () => + column.renderFilterIcon + ? column.renderFilterIcon({ + filterOpened: column.filterOpened, + }) + : null, + } + ), ] ), ]