Skip to content

Commit

Permalink
feat(pro:table): support resizable column (#945)
Browse files Browse the repository at this point in the history
  • Loading branch information
danranVm committed Jun 9, 2022
1 parent ae91476 commit 47b90dd
Show file tree
Hide file tree
Showing 22 changed files with 374 additions and 99 deletions.
40 changes: 18 additions & 22 deletions packages/cdk/resize/demo/ResizableBasic.vue
Original file line number Diff line number Diff line change
Expand Up @@ -88,31 +88,27 @@
}
}
&:not(.cdk-resizable-resizing) {
.cdk-resizable-handler {
&-top,
&-bottom {
cursor: ns-resize;
}
&-start,
&-end {
cursor: ew-resize;
}
&-topStart,
&-bottomEnd {
cursor: nwse-resize;
}
&-topEnd,
&-bottomStart {
cursor: nesw-resize;
}
&:not(&-resizing) &-handler {
&-top,
&-bottom {
cursor: ns-resize;
}
&-start,
&-end {
cursor: ew-resize;
}
&-topStart,
&-bottomEnd {
cursor: nwse-resize;
}
&-topEnd,
&-bottomStart {
cursor: nesw-resize;
}
}
&-disabled {
.cdk-resizable-handler {
pointer-events: none;
}
&-disabled &-handler {
pointer-events: none;
}
}
</style>
40 changes: 18 additions & 22 deletions packages/cdk/resize/demo/ResizableHandler.vue
Original file line number Diff line number Diff line change
Expand Up @@ -117,31 +117,27 @@
}
}
&:not(.cdk-resizable-resizing) {
.cdk-resizable-handler {
&-top,
&-bottom {
cursor: ns-resize;
}
&-start,
&-end {
cursor: ew-resize;
}
&-topStart,
&-bottomEnd {
cursor: nwse-resize;
}
&-topEnd,
&-bottomStart {
cursor: nesw-resize;
}
&:not(&-resizing) &-handler {
&-top,
&-bottom {
cursor: ns-resize;
}
&-start,
&-end {
cursor: ew-resize;
}
&-topStart,
&-bottomEnd {
cursor: nwse-resize;
}
&-topEnd,
&-bottomStart {
cursor: nesw-resize;
}
}
&-disabled {
.cdk-resizable-handler {
pointer-events: none;
}
&-disabled &-handler {
pointer-events: none;
}
}
</style>
2 changes: 1 addition & 1 deletion packages/cdk/resize/src/resizable/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export type ResizableHandlerComponent = DefineComponent<
>
export type ResizableHandlerInstance = InstanceType<DefineComponent<ResizableHandlerProps>>

export type ResizableOptions = Omit<ResizablePublicProps, 'disabled' | 'is'>
export type ResizableOptions = Omit<ResizablePublicProps, 'disabled' | 'is' | 'handlers'>

export type ResizableHandlerPlacement = typeof allHandlerPlacements[number]

Expand Down
35 changes: 32 additions & 3 deletions packages/cdk/resize/src/resizable/useResizable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export function useResizable(
if (!startPosition.value) {
return
}

setBodyCursor(_placement)
resizing.value = true

const { width: currWidth, height: currHeight } = calcSizeByEvent(_placement, evt)
Expand All @@ -158,7 +158,7 @@ export function useResizable(
if (!startPosition.value) {
return
}

clearBodyCursor()
resizing.value = false

startPosition.value = undefined
Expand All @@ -174,6 +174,35 @@ export function useResizable(
}
}

function ensureInBounds(value: number, boundValue: number): number {
function ensureInBounds(value: number, boundValue: number) {
return value < boundValue ? value : boundValue
}

function setBodyCursor(placement: ResizableHandlerPlacement) {
let cursor = ''
switch (placement) {
case 'top':
case 'bottom':
cursor = 'ns-resize'
break
case 'start':
case 'end':
cursor = 'ew-resize'
break
case 'topStart':
case 'bottomEnd':
cursor = 'nwse-resize'
break
case 'topEnd':
case 'bottomStart':
cursor = 'nesw-resize'
break
}
document.body.style.cursor = cursor
document.body.style.userSelect = 'none'
}

function clearBodyCursor() {
document.body.style.cursor = ''
document.body.style.userSelect = ''
}
1 change: 1 addition & 0 deletions packages/components/table/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type {
TableColumnExpandable,
TableColumnSelectable,
TableCustomAdditional,
TableCustomTag,
TablePagination,
TablePaginationPosition,
TableScroll,
Expand Down
2 changes: 1 addition & 1 deletion packages/components/table/src/main/ColGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function renderCol(
[`${prefixCls}-selectable-with-dropdown`]: type === 'selectable' && mergedSelectableMenus.value.length > 0,
})

const mergedWidth = width ?? column.width
const mergedWidth = column.width ?? width
const style = mergedWidth ? { width: convertCssPixel(mergedWidth) } : undefined
return <col key={column.key} class={className} style={style}></col>
}
7 changes: 4 additions & 3 deletions packages/components/table/src/main/body/BodyCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,13 @@ export default defineComponent({
const customAdditional = customAdditionalFn
? customAdditionalFn({ column, record: props.record, rowIndex: props.rowIndex })
: undefined

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const Tag = (tableProps.customTag?.bodyCell ?? 'td') as any
return (
<td class={classes.value} style={style.value} title={title} {...additional} {...customAdditional}>
<Tag class={classes.value} style={style.value} title={title} {...additional} {...customAdditional}>
{type === 'expandable' && renderExpandableChildren(props, slots, expandable, mergedPrefixCls.value)}
{children}
</td>
</Tag>
)
}
},
Expand Down
7 changes: 4 additions & 3 deletions packages/components/table/src/main/body/BodyRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ export default defineComponent({
const customAdditional = customAdditionalFn
? customAdditionalFn({ record: props.record, rowIndex: props.rowIndex })
: undefined

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const Tag = (tableProps.customTag?.bodyRow ?? 'tr') as any
return (
<tr class={classes.value} {...clickEvents.value} {...customAdditional}>
<Tag class={classes.value} {...clickEvents.value} {...customAdditional}>
{renderChildren(
props,
flattedColumns,
Expand All @@ -82,7 +83,7 @@ export default defineComponent({
selectDisabled,
handleSelect,
)}
</tr>
</Tag>
)
}
},
Expand Down
7 changes: 4 additions & 3 deletions packages/components/table/src/main/head/HeadCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,10 @@ export default defineComponent({

const customAdditionalFn = tableProps.customAdditional?.headCell
const customAdditional = customAdditionalFn ? customAdditionalFn({ column }) : undefined

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const Tag = (tableProps.customTag?.headCell ?? 'th') as any
return (
<th
<Tag
class={classes.value}
style={style.value}
colSpan={titleColSpan === 1 ? undefined : titleColSpan}
Expand All @@ -156,7 +157,7 @@ export default defineComponent({
onClick={onClick}
>
{children}
</th>
</Tag>
)
}
},
Expand Down
6 changes: 4 additions & 2 deletions packages/components/table/src/main/head/HeadRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ export default defineComponent({
const { columns } = props
const customAdditionalFn = tableProps.customAdditional?.headRow
const customAdditional = customAdditionalFn ? customAdditionalFn({ columns }) : undefined
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const Tag = (tableProps.customTag?.headRow ?? 'tr') as any
return (
<tr {...customAdditional}>
<Tag {...customAdditional}>
{columns
.filter(column => column.titleColSpan !== 0)
.map(column => (
<HeadCell key={column.key} column={column} />
))}
</tr>
</Tag>
)
}
},
Expand Down
36 changes: 21 additions & 15 deletions packages/components/table/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,19 @@

/* eslint-disable @typescript-eslint/no-explicit-any */

import { type DefineComponent, FunctionalComponent, type HTMLAttributes, type PropType, type VNodeChild } from 'vue'

import { type BreakpointKey } from '@idux/cdk/breakpoint'
import { type VirtualScrollToFn } from '@idux/cdk/scroll'
import { type ExtractInnerPropTypes, type ExtractPublicPropTypes, type MaybeArray, type VKey } from '@idux/cdk/utils'
import { type EmptyProps } from '@idux/components/empty'
import { type HeaderProps } from '@idux/components/header'
import { type MenuClickOptions, type MenuData } from '@idux/components/menu'
import { type PaginationProps } from '@idux/components/pagination'
import { type SpinProps } from '@idux/components/spin'

import { type TableColumnMerged, type TableColumnMergedExtra } from './composables/useColumns'
import { type FlattedData } from './composables/useDataSource'
import { type ActiveFilter } from './composables/useFilterable'
import { type ActiveSorter } from './composables/useSortable'
import type { TableColumnMerged, TableColumnMergedExtra } from './composables/useColumns'
import type { FlattedData } from './composables/useDataSource'
import type { ActiveFilter } from './composables/useFilterable'
import type { ActiveSorter } from './composables/useSortable'
import type { BreakpointKey } from '@idux/cdk/breakpoint'
import type { VirtualScrollToFn } from '@idux/cdk/scroll'
import type { ExtractInnerPropTypes, ExtractPublicPropTypes, MaybeArray, VKey } from '@idux/cdk/utils'
import type { EmptyProps } from '@idux/components/empty'
import type { HeaderProps } from '@idux/components/header'
import type { MenuClickOptions, MenuData } from '@idux/components/menu'
import type { PaginationProps } from '@idux/components/pagination'
import type { SpinProps } from '@idux/components/spin'
import type { Component, DefineComponent, FunctionalComponent, HTMLAttributes, PropType, VNodeChild } from 'vue'

export const tableProps = {
expandedRowKeys: { type: Array as PropType<VKey[]>, default: undefined },
Expand All @@ -32,6 +30,7 @@ export const tableProps = {
childrenKey: { type: String, default: undefined },
columns: { type: Array as PropType<TableColumn[]>, default: () => [] },
customAdditional: { type: Object as PropType<TableCustomAdditional>, default: undefined },
customTag: { type: Object as PropType<TableCustomTag>, default: undefined },
dataSource: { type: Array as PropType<any[]>, default: () => [] },
ellipsis: { type: Boolean, default: false },
empty: { type: [String, Object] as PropType<string | EmptyProps>, default: undefined },
Expand Down Expand Up @@ -161,6 +160,13 @@ export interface TableCustomAdditional<T = any> {
headRow?: (data: { columns: TableColumn<T>[] }) => Record<string, any> | undefined
}

export interface TableCustomTag {
bodyCell?: string | Component
bodyRow?: string | Component
headCell?: string | Component
headRow?: string | Component
}

export interface TablePagination extends PaginationProps {
position?: TablePaginationPosition
}
Expand Down
7 changes: 0 additions & 7 deletions packages/pro/table/demo/Basic.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
<a style="margin-right: 8px">Invite {{ record.name }}</a>
<a>Delete</a>
</template>
<template #expand="{ record }">
<span>{{ record.description }}</span>
</template>
</IxProTable>
</template>

Expand Down Expand Up @@ -46,11 +43,7 @@ const onColumnsChange = console.log
const columns: ProTableColumn<Data>[] = [
{
type: 'indexable',
},
{
type: 'expandable',
changeVisible: false,
customExpand: 'expand',
},
{
title: 'Name',
Expand Down
9 changes: 0 additions & 9 deletions packages/pro/table/demo/HeadGroup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
<a style="margin-right: 8px">Invite {{ record.name }}</a>
<a>Delete</a>
</template>
<template #expand="{ record }">
<span>{{ record.description }}</span>
</template>
</IxProTable>
</template>

Expand All @@ -33,13 +30,7 @@ const columns: ProTableColumn<Data>[] = [
{
type: 'indexable',
fixed: 'start',
},
{
type: 'expandable',
fixed: 'start',
changeVisible: false,
onChange: expendedRowKeys => console.log(expendedRowKeys),
customExpand: 'expand',
},
{
title: 'Name',
Expand Down
14 changes: 14 additions & 0 deletions packages/pro/table/demo/Resizable.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
order: 10
title:
zh: 可调整列宽
en: Resizable column
---

## zh

可以通过配置 `resizable`,开启拖拽调整列宽,还可以配置 `maxWidth` `minWidth` 来限制列宽的范围。

## en

Column width can be adjusted by configuring `resizable`, enabling drag and drop, and column width can be limited by configuring `maxWidth` and `minWidth`.
Loading

0 comments on commit 47b90dd

Please sign in to comment.