Skip to content

Commit 4ce6540

Browse files
authored
fix(Table): handle reactive columns (#4412)
1 parent fb9e7bb commit 4ce6540

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

‎src/runtime/components/Table.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,9 @@ const tableRef = ref<HTMLTableElement | null>(null)
233233
const tableApi = useVueTable({
234234
...reactiveOmit(props, 'as', 'data', 'columns', 'caption', 'sticky', 'loading', 'loadingColor', 'loadingAnimation', 'class', 'ui'),
235235
data,
236-
columns: columns.value,
236+
get columns() {
237+
return columns.value
238+
},
237239
meta: meta.value,
238240
getCoreRowModel: getCoreRowModel(),
239241
...(props.globalFilterOptions || {}),

‎test/components/Table.spec.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import { h } from 'vue'
1+
import { h, ref, computed } from 'vue'
22
import { describe, it, expect } from 'vitest'
3+
import { flushPromises } from '@vue/test-utils'
4+
import { mountSuspended } from '@nuxt/test-utils/runtime'
35
import { UCheckbox, UButton, UBadge, UDropdownMenu } from '#components'
46
import Table, { type TableProps, type TableSlots, type TableColumn } from '../../src/runtime/components/Table.vue'
57
import ComponentRender from '../component-render'
@@ -168,4 +170,46 @@ describe('Table', () => {
168170
const html = await ComponentRender(nameOrHtml, options, Table)
169171
expect(html).toMatchSnapshot()
170172
})
173+
174+
it('reactive columns', async () => {
175+
const wrapper = await mountSuspended({
176+
components: { Table },
177+
setup() {
178+
const filter = ref<1 | 2>(1)
179+
180+
const columns = computed<TableColumn<typeof data[number]>[]>(() => [
181+
{
182+
accessorKey: 'id'
183+
},
184+
...(filter.value === 2
185+
? [
186+
{
187+
accessorKey: 'amount',
188+
header: () => h('div', { ['data-test-th']: 'amount' }, 'Amount')
189+
} satisfies TableColumn<typeof data[number]>
190+
]
191+
: [])
192+
])
193+
194+
function onClick() {
195+
filter.value = 2
196+
}
197+
198+
return { data, columns, onClick }
199+
},
200+
template: `
201+
<div>
202+
<button @click="onClick">Change filter</button>
203+
<Table :data :columns />
204+
</div>
205+
`
206+
})
207+
208+
expect(wrapper.find('[data-test-th="amount"]').exists()).toBeFalsy()
209+
210+
wrapper.find('button').trigger('click')
211+
await flushPromises()
212+
213+
expect(wrapper.find('[data-test-th="amount"]').exists()).toBeTruthy()
214+
})
171215
})

0 commit comments

Comments
 (0)