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

fix(pro:table): layout tool tree shouldn't show empty #1891

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
76 changes: 52 additions & 24 deletions packages/pro/table/src/composables/useColumns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@

import { type ComputedRef, computed, watch } from 'vue'

import { type VKey, callEmit, useState } from '@idux/cdk/utils'
import { type VKey, callEmit, filterTree, useState } from '@idux/cdk/utils'
import { ɵGetColumnKey } from '@idux/components/table'
import { type ProTableConfig } from '@idux/pro/config'

import { type ProTableColumn, type ProTableProps } from '../types'
import { loopColumns } from '../utils'

export interface ColumnsContext {
checkedColumnKeys: ComputedRef<{
start: VKey[]
center: VKey[]
end: VKey[]
}>
mergedColumns: ComputedRef<ProTableColumn[]>
setMergedColumns: (columns: ProTableColumn[]) => void
mergedColumnMap: ComputedRef<Map<VKey, ProTableColumn>>
Expand All @@ -25,19 +29,58 @@ export interface ColumnsContext {
export function useColumns(props: ProTableProps, config: ProTableConfig): ColumnsContext {
const originalColumns = computed(() => props.columns)
const [mergedColumns, setMergedColumns] = useState(mergeColumns(originalColumns.value, config))
const mergedColumnMap = computed(() => {

const mergedContext = computed(() => {
const map = new Map<VKey, ProTableColumn>()
loopColumns(mergedColumns.value, column => map.set(column.key!, column))
return map
const checkedKeys = {
start: [] as VKey[],
center: [] as VKey[],
end: [] as VKey[],
}

const displayColumns = filterTree(
mergedColumns.value as (ProTableColumn & { children?: ProTableColumn[] })[],
'children',
(column, parents) => {
const key = column.key!
map.set(key, column)

if (column.visible === false || parents.some(parent => parent.visible === false)) {
return false
}

if (
!column.children?.length &&
column.layoutable !== false &&
parents.every(parent => parent.layoutable !== false)
) {
if (isFixed('start', column, parents)) {
checkedKeys.start.push(key)
} else if (isFixed('end', column, parents)) {
checkedKeys.end.push(key)
} else {
checkedKeys.center.push(key)
}
}

return true
},
'and',
) as ProTableColumn[]

return { map, checkedKeys, displayColumns }
})
const displayColumns = computed(() => getDisplayColumns(mergedColumns.value))

const mergedColumnMap = computed(() => mergedContext.value.map)
const checkedColumnKeys = computed(() => mergedContext.value.checkedKeys)
const displayColumns = computed(() => mergedContext.value.displayColumns)

watch(originalColumns, columns => setMergedColumns(mergeColumns(columns, config)))
watch(mergedColumns, newColumns => callEmit(props.onColumnsChange, newColumns))

const resetColumns = () => setMergedColumns(mergeColumns(originalColumns.value, config))

return { mergedColumns, setMergedColumns, mergedColumnMap, displayColumns, resetColumns }
return { checkedColumnKeys, mergedColumns, setMergedColumns, mergedColumnMap, displayColumns, resetColumns }
}

function mergeColumns(columns: ProTableColumn[], config: ProTableConfig, parentKey?: VKey) {
Expand All @@ -54,21 +97,6 @@ function convertMergeColumn(column: ProTableColumn, config: ProTableConfig, pare
return mergeColumn
}

function getDisplayColumns(columns: ProTableColumn[]): ProTableColumn[] {
const result: ProTableColumn[] = []
columns.forEach(column => {
if (column.visible === false) {
return
}
if ('children' in column && column.children) {
const newChildren = getDisplayColumns(column.children)
if (newChildren.length === 0) {
return
}
result.push({ ...column, children: newChildren })
} else {
result.push(column)
}
})
return result
function isFixed(fixed: 'start' | 'end', column: ProTableColumn, parents: ProTableColumn[]) {
return column.fixed === fixed || parents.some(parent => parent.fixed === fixed)
}