refactor: table

pull/4639/head
tangjinzhou 2021-09-04 17:16:37 +08:00
parent a948e663a9
commit 0d06e328a6
20 changed files with 50 additions and 45 deletions

View File

@ -70,7 +70,7 @@ export default defineComponent<BodyRowProps<unknown>>({
}; };
// =========================== onRow =========================== // =========================== onRow ===========================
let additionalProps = computed<Record<string, any>>( const additionalProps = computed<Record<string, any>>(
() => props.customRow?.(props.record, props.index) || {}, () => props.customRow?.(props.record, props.index) || {},
); );
@ -84,7 +84,7 @@ export default defineComponent<BodyRowProps<unknown>>({
} }
}; };
let computeRowClassName = computed(() => { const computeRowClassName = computed(() => {
const { record, index, indent } = props; const { record, index, indent } = props;
const { rowClassName } = bodyContext; const { rowClassName } = bodyContext;
if (typeof rowClassName === 'string') { if (typeof rowClassName === 'string') {

View File

@ -1,4 +1,4 @@
import { CustomizeComponent } from '../interface'; import type { CustomizeComponent } from '../interface';
import Cell from '../Cell'; import Cell from '../Cell';
import { defineComponent } from 'vue'; import { defineComponent } from 'vue';
import { useInjectTable } from '../context/TableContext'; import { useInjectTable } from '../context/TableContext';

View File

@ -1,5 +1,5 @@
import VCResizeObserver from 'ant-design-vue/es/vc-resize-observer'; import VCResizeObserver from 'ant-design-vue/es/vc-resize-observer';
import { Key } from '../interface'; import type { Key } from '../interface';
export interface MeasureCellProps { export interface MeasureCellProps {
columnKey: Key; columnKey: Key;

View File

@ -1,6 +1,7 @@
import classNames from 'ant-design-vue/es/_util/classNames'; import classNames from 'ant-design-vue/es/_util/classNames';
import { isValidElement, parseStyleText } from 'ant-design-vue/es/_util/props-util'; import { isValidElement, parseStyleText } from 'ant-design-vue/es/_util/props-util';
import { CSSProperties, defineComponent, HTMLAttributes } from 'vue'; import type { CSSProperties, HTMLAttributes } from 'vue';
import { defineComponent } from 'vue';
import type { import type {
DataIndex, DataIndex,

View File

@ -1,6 +1,7 @@
import type { HeaderProps } from '../Header/Header'; import type { HeaderProps } from '../Header/Header';
import ColGroup from '../ColGroup'; import ColGroup from '../ColGroup';
import type { ColumnsType, ColumnType, DefaultRecordType } from '../interface'; import type { ColumnsType, ColumnType, DefaultRecordType } from '../interface';
import type { Ref } from 'vue';
import { import {
computed, computed,
defineComponent, defineComponent,
@ -8,7 +9,6 @@ import {
onBeforeUnmount, onBeforeUnmount,
onMounted, onMounted,
ref, ref,
Ref,
toRef, toRef,
watchEffect, watchEffect,
} from 'vue'; } from 'vue';
@ -107,11 +107,11 @@ export default defineComponent<FixedHeaderProps<DefaultRecordType>>({
}), }),
}; };
columnsWithScrollbar.value = combinationScrollBarSize columnsWithScrollbar.value = combinationScrollBarSize.value
? [...props.columns, ScrollBarColumn] ? [...props.columns, ScrollBarColumn]
: props.columns; : props.columns;
flattenColumnsWithScrollbar.value = combinationScrollBarSize flattenColumnsWithScrollbar.value = combinationScrollBarSize.value
? [...props.flattenColumns, ScrollBarColumn] ? [...props.flattenColumns, ScrollBarColumn]
: props.flattenColumns; : props.flattenColumns;
}); });

View File

@ -1,4 +1,4 @@
import { computed, defineComponent, FunctionalComponent, onBeforeUnmount, watchEffect } from 'vue'; import { computed, defineComponent, onBeforeUnmount, watchEffect } from 'vue';
import { useInjectTable } from '../context/TableContext'; import { useInjectTable } from '../context/TableContext';
import Cell from './Cell'; import Cell from './Cell';
import Row from './Row'; import Row from './Row';
@ -7,15 +7,12 @@ export interface SummaryProps {
fixed?: boolean | 'top' | 'bottom'; fixed?: boolean | 'top' | 'bottom';
} }
export interface SummaryFC extends FunctionalComponent<SummaryProps> {
Row: typeof Row;
Cell: typeof Cell;
}
let indexGuid = 0; let indexGuid = 0;
const Summary = defineComponent<SummaryProps>({ const Summary = defineComponent<SummaryProps>({
props: ['fixed'] as any, props: ['fixed'] as any,
name: 'Summary', name: 'Summary',
Row,
Cell,
setup(props, { slots }) { setup(props, { slots }) {
const tableContext = useInjectTable(); const tableContext = useInjectTable();
const uniKey = `table-summary-uni-key-${++indexGuid}`; const uniKey = `table-summary-uni-key-${++indexGuid}`;

View File

@ -1,7 +1,8 @@
import Summary from './Summary'; import Summary from './Summary';
import type { DefaultRecordType, StickyOffsets } from '../interface'; import type { DefaultRecordType, StickyOffsets } from '../interface';
import { computed, defineComponent, reactive, toRef } from 'vue'; import { computed, defineComponent, reactive, toRef } from 'vue';
import { FlattenColumns, useProvideSummary } from '../context/SummaryContext'; import type { FlattenColumns } from '../context/SummaryContext';
import { useProvideSummary } from '../context/SummaryContext';
import { useInjectTable } from '../context/TableContext'; import { useInjectTable } from '../context/TableContext';
export interface FooterProps<RecordType = DefaultRecordType> { export interface FooterProps<RecordType = DefaultRecordType> {
@ -10,8 +11,8 @@ export interface FooterProps<RecordType = DefaultRecordType> {
} }
export default defineComponent({ export default defineComponent({
props: ['stickyOffsets', 'flattenColumns'],
name: 'Footer', name: 'Footer',
props: ['stickyOffsets', 'flattenColumns'],
setup(props, { slots }) { setup(props, { slots }) {
const tableContext = useInjectTable(); const tableContext = useInjectTable();
useProvideSummary( useProvideSummary(

View File

@ -20,7 +20,7 @@ function parseHeaderRows<RecordType>(
function fillRowCells( function fillRowCells(
columns: ColumnsType<RecordType>, columns: ColumnsType<RecordType>,
colIndex: number, colIndex: number,
rowIndex: number = 0, rowIndex = 0,
): number[] { ): number[] {
// Init rows // Init rows
rows[rowIndex] = rows[rowIndex] || []; rows[rowIndex] = rows[rowIndex] || [];
@ -35,7 +35,7 @@ function parseHeaderRows<RecordType>(
colStart: currentColIndex, colStart: currentColIndex,
}; };
let colSpan: number = 1; let colSpan = 1;
const subColumns = (column as ColumnGroupType<RecordType>).children; const subColumns = (column as ColumnGroupType<RecordType>).children;
if (subColumns && subColumns.length > 0) { if (subColumns && subColumns.length > 0) {

View File

@ -1,7 +1,7 @@
import { defineComponent } from 'vue'; import { defineComponent } from 'vue';
import Cell from '../Cell'; import Cell from '../Cell';
import { useInjectTable } from '../context/TableContext'; import { useInjectTable } from '../context/TableContext';
import { import type {
CellType, CellType,
StickyOffsets, StickyOffsets,
ColumnType, ColumnType,

View File

@ -29,9 +29,9 @@ import { getCellFixedInfo } from './utils/fixUtil';
import StickyScrollBar from './stickyScrollBar'; import StickyScrollBar from './stickyScrollBar';
import useSticky from './hooks/useSticky'; import useSticky from './hooks/useSticky';
import FixedHolder from './FixedHolder'; import FixedHolder from './FixedHolder';
import type { CSSProperties } from 'vue';
import { import {
computed, computed,
CSSProperties,
defineComponent, defineComponent,
nextTick, nextTick,
onMounted, onMounted,
@ -49,7 +49,7 @@ import { toPx } from '../_util/util';
import isVisible from '../vc-util/Dom/isVisible'; import isVisible from '../vc-util/Dom/isVisible';
import { getTargetScrollBarSize } from '../_util/getScrollBarSize'; import { getTargetScrollBarSize } from '../_util/getScrollBarSize';
import classNames from '../_util/classNames'; import classNames from '../_util/classNames';
import { EventHandler } from '../_util/EventInterface'; import type { EventHandler } from '../_util/EventInterface';
import VCResizeObserver from '../vc-resize-observer'; import VCResizeObserver from '../vc-resize-observer';
import { useProvideTable } from './context/TableContext'; import { useProvideTable } from './context/TableContext';
import { useProvideBody } from './context/BodyContext'; import { useProvideBody } from './context/BodyContext';
@ -262,9 +262,9 @@ export default defineComponent<TableProps>({
}; };
// Scroll // Scroll
let scrollXStyle = ref<CSSProperties>({}); const scrollXStyle = ref<CSSProperties>({});
let scrollYStyle = ref<CSSProperties>({}); const scrollYStyle = ref<CSSProperties>({});
let scrollTableStyle = ref<CSSProperties>({}); const scrollTableStyle = ref<CSSProperties>({});
watchEffect(() => { watchEffect(() => {
if (fixHeader.value) { if (fixHeader.value) {
@ -356,7 +356,7 @@ export default defineComponent<TableProps>({
}; };
const onFullTableResize = ({ width }) => { const onFullTableResize = ({ width }) => {
if (width !== componentWidth) { if (width !== componentWidth.value) {
triggerOnScroll(); triggerOnScroll();
componentWidth.value = fullTableRef.value ? fullTableRef.value.offsetWidth : width; componentWidth.value = fullTableRef.value ? fullTableRef.value.offsetWidth : width;
} }
@ -561,7 +561,7 @@ export default defineComponent<TableProps>({
> >
{bodyColGroup} {bodyColGroup}
{bodyTable} {bodyTable}
{!fixFooter && summaryNode && ( {!fixFooter.value && summaryNode && (
<Footer stickyOffsets={stickyOffsets} flattenColumns={flattenColumns.value}> <Footer stickyOffsets={stickyOffsets} flattenColumns={flattenColumns.value}>
{summaryNode} {summaryNode}
</Footer> </Footer>

View File

@ -1,4 +1,4 @@
import { import type {
ColumnType, ColumnType,
DefaultRecordType, DefaultRecordType,
ColumnsType, ColumnsType,
@ -9,7 +9,8 @@ import {
TriggerEventHandler, TriggerEventHandler,
ExpandedRowRender, ExpandedRowRender,
} from '../interface'; } from '../interface';
import { inject, InjectionKey, provide } from 'vue'; import type { InjectionKey } from 'vue';
import { inject, provide } from 'vue';
export interface BodyContextProps<RecordType = DefaultRecordType> { export interface BodyContextProps<RecordType = DefaultRecordType> {
rowClassName: string | RowClassName<RecordType>; rowClassName: string | RowClassName<RecordType>;

View File

@ -1,5 +1,6 @@
import { inject, InjectionKey, provide } from 'vue'; import type { InjectionKey } from 'vue';
import { Key } from '../interface'; import { inject, provide } from 'vue';
import type { Key } from '../interface';
interface ResizeContextProps { interface ResizeContextProps {
onColumnResize: (columnKey: Key, width: number) => void; onColumnResize: (columnKey: Key, width: number) => void;

View File

@ -1,5 +1,6 @@
import { inject, InjectionKey, provide } from 'vue'; import type { InjectionKey } from 'vue';
import { ColumnType, StickyOffsets } from '../interface'; import { inject, provide } from 'vue';
import type { ColumnType, StickyOffsets } from '../interface';
export type FlattenColumns<RecordType> = readonly (ColumnType<RecordType> & { export type FlattenColumns<RecordType> = readonly (ColumnType<RecordType> & {
scrollbar?: boolean; scrollbar?: boolean;

View File

@ -1,6 +1,7 @@
import { inject, InjectionKey, provide } from 'vue'; import type { InjectionKey } from 'vue';
import { GetComponent } from '../interface'; import { inject, provide } from 'vue';
import { FixedInfo } from '../utils/fixUtil'; import type { GetComponent } from '../interface';
import type { FixedInfo } from '../utils/fixUtil';
export interface TableContextProps { export interface TableContextProps {
// Table context // Table context

View File

@ -1,5 +1,6 @@
import { warning } from 'ant-design-vue/es/vc-util/warning'; import { warning } from 'ant-design-vue/es/vc-util/warning';
import { computed, ComputedRef, Ref, watchEffect } from 'vue'; import type { ComputedRef, Ref } from 'vue';
import { computed, watchEffect } from 'vue';
import type { import type {
ColumnsType, ColumnsType,
ColumnType, ColumnType,

View File

@ -1,8 +1,9 @@
import { computed, defineComponent, onBeforeUnmount, onMounted, ref, Ref, watch } from 'vue'; import type { Ref } from 'vue';
import { computed, defineComponent, onBeforeUnmount, onMounted, ref, watch } from 'vue';
import addEventListenerWrap from '../vc-util/Dom/addEventListener'; import addEventListenerWrap from '../vc-util/Dom/addEventListener';
import { getOffset } from '../vc-util/Dom/css'; import { getOffset } from '../vc-util/Dom/css';
import classNames from '../_util/classNames'; import classNames from '../_util/classNames';
import { MouseEventHandler } from '../_util/EventInterface'; import type { MouseEventHandler } from '../_util/EventInterface';
import getScrollBarSize from '../_util/getScrollBarSize'; import getScrollBarSize from '../_util/getScrollBarSize';
import { useInjectTable } from './context/TableContext'; import { useInjectTable } from './context/TableContext';
import { useLayoutState } from './hooks/useFrame'; import { useLayoutState } from './hooks/useFrame';
@ -188,7 +189,7 @@ export default defineComponent<StickyScrollBarProps>({
<div <div
style={{ style={{
height: `${scrollbarSize}px`, height: `${scrollbarSize}px`,
width: `${bodyWidth}px`, width: `${bodyWidth.value}px`,
bottom: `${props.offsetScroll}px`, bottom: `${props.offsetScroll}px`,
}} }}
class={`${prefixCls}-sticky-scroll`} class={`${prefixCls}-sticky-scroll`}

View File

@ -1,5 +1,5 @@
import { FunctionalComponent } from 'vue'; import type { FunctionalComponent } from 'vue';
import { ColumnType } from '../interface'; import type { ColumnType } from '../interface';
export type ColumnProps<RecordType> = ColumnType<RecordType>; export type ColumnProps<RecordType> = ColumnType<RecordType>;

View File

@ -1,5 +1,5 @@
import { ColumnType } from '../interface'; import type { ColumnType } from '../interface';
import { FunctionalComponent } from 'vue'; import type { FunctionalComponent } from 'vue';
/* istanbul ignore next */ /* istanbul ignore next */
/** /**
* This is a syntactic sugar for `columns` prop. * This is a syntactic sugar for `columns` prop.

View File

@ -1,4 +1,4 @@
import { RenderExpandIconProps, Key, GetRowKey } from '../interface'; import type { RenderExpandIconProps, Key, GetRowKey } from '../interface';
export function renderExpandIcon<RecordType>({ export function renderExpandIcon<RecordType>({
prefixCls, prefixCls,

View File

@ -1,4 +1,4 @@
import { Key, DataIndex } from '../interface'; import type { Key, DataIndex } from '../interface';
const INTERNAL_KEY_PREFIX = 'RC_TABLE_KEY'; const INTERNAL_KEY_PREFIX = 'RC_TABLE_KEY';