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

refactor: rc-component/table #1219

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ es/
.storybook
.doc
!__mocks__/rc-util/lib
!__mocks__/@rc-component/util/lib
examples/debug.tsx
.history
~*
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/animation.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import CSSMotionList from 'rc-animate/lib/CSSMotionList';
import classNames from 'classnames';
import toArray from 'rc-util/lib/Children/toArray';
import toArray from '@rc-component/util/lib/Children/toArray';
import type { TableProps } from 'rc-table';
import Table from 'rc-table';
import '../../assets/index.less';
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rc-table",
"version": "7.50.2",
"name": "@rc-component/table",
"version": "1.0.0",
li-jia-nan marked this conversation as resolved.
Show resolved Hide resolved
"description": "table ui component for react",
"engines": {
"node": ">=8.x"
Expand Down Expand Up @@ -55,9 +55,9 @@
"dependencies": {
"@babel/runtime": "^7.10.1",
"@rc-component/context": "^1.4.0",
"@rc-component/util": "^1.1.0",
"classnames": "^2.2.5",
"rc-resize-observer": "^1.1.0",
"rc-util": "^5.44.3",
"rc-virtual-list": "^3.14.2"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/Cell/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type {
} from '../interface';
import useCellRender from './useCellRender';
import useHoverState from './useHoverState';
import { useEvent } from 'rc-util';
import { useEvent } from '@rc-component/util';

export interface CellProps<RecordType extends DefaultRecordType> {
prefixCls?: string;
Expand Down
8 changes: 4 additions & 4 deletions src/Cell/useCellRender.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import useMemo from 'rc-util/lib/hooks/useMemo';
import isEqual from 'rc-util/lib/isEqual';
import getValue from 'rc-util/lib/utils/get';
import warning from 'rc-util/lib/warning';
import useMemo from '@rc-component/util/lib/hooks/useMemo';
import isEqual from '@rc-component/util/lib/isEqual';
import getValue from '@rc-component/util/lib/utils/get';
import warning from '@rc-component/util/lib/warning';
import * as React from 'react';
import PerfContext from '../context/PerfContext';
import type { CellType, ColumnType, DataIndex, RenderedCell } from '../interface';
Expand Down
2 changes: 1 addition & 1 deletion src/FixedHolder/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useContext } from '@rc-component/context';
import classNames from 'classnames';
import { fillRef } from 'rc-util/lib/ref';
import { fillRef } from '@rc-component/util/lib/ref';
import * as React from 'react';
import { useMemo } from 'react';
import ColGroup from '../ColGroup';
Expand Down
16 changes: 8 additions & 8 deletions src/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
import type { CompareProps } from '@rc-component/context/lib/Immutable';
import classNames from 'classnames';
import ResizeObserver from 'rc-resize-observer';
import isVisible from 'rc-util/lib/Dom/isVisible';
import { isStyleSupport } from 'rc-util/lib/Dom/styleChecker';
import { getTargetScrollBarSize } from 'rc-util/lib/getScrollBarSize';
import useEvent from 'rc-util/lib/hooks/useEvent';
import pickAttrs from 'rc-util/lib/pickAttrs';
import getValue from 'rc-util/lib/utils/get';
import warning from 'rc-util/lib/warning';
import isVisible from '@rc-component/util/lib/Dom/isVisible';
import { isStyleSupport } from '@rc-component/util/lib/Dom/styleChecker';
import { getTargetScrollBarSize } from '@rc-component/util/lib/getScrollBarSize';
import useEvent from '@rc-component/util/lib/hooks/useEvent';
import pickAttrs from '@rc-component/util/lib/pickAttrs';
import getValue from '@rc-component/util/lib/utils/get';
import warning from '@rc-component/util/lib/warning';
import * as React from 'react';
import Body from './Body';
import ColGroup from './ColGroup';
Expand Down Expand Up @@ -75,7 +75,7 @@ import StickyScrollBar from './stickyScrollBar';
import Column from './sugar/Column';
import ColumnGroup from './sugar/ColumnGroup';
import { getColumnsKey, validateValue, validNumberValue } from './utils/valueUtil';
import { getDOM } from 'rc-util/lib/Dom/findDOMNode';
import { getDOM } from '@rc-component/util/lib/Dom/findDOMNode';

export const DEFAULT_PREFIX = 'rc-table';

Expand Down
4 changes: 2 additions & 2 deletions src/VirtualTable/index.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { CompareProps } from '@rc-component/context/lib/Immutable';
import classNames from 'classnames';
import { useEvent, warning } from 'rc-util';
import { useEvent, warning } from '@rc-component/util';
import * as React from 'react';
import { INTERNAL_HOOKS } from '../constant';
import { makeImmutable } from '../context/TableContext';
import type { CustomizeScrollBody, GetComponent, Reference } from '../interface';
import Table, { DEFAULT_PREFIX, type TableProps } from '../Table';
import Grid from './BodyGrid';
import { StaticContext } from './context';
import getValue from 'rc-util/lib/utils/get';
import getValue from '@rc-component/util/lib/utils/get';

const renderBody: CustomizeScrollBody<any> = (rawData, props) => {
const { ref, onScroll } = props;
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useColumns/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import toArray from 'rc-util/lib/Children/toArray';
import warning from 'rc-util/lib/warning';
import toArray from '@rc-component/util/lib/Children/toArray';
import warning from '@rc-component/util/lib/warning';
import * as React from 'react';
import { EXPAND_COLUMN } from '../../constant';
import type {
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useExpand.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import warning from 'rc-util/lib/warning';
import warning from '@rc-component/util/lib/warning';
import * as React from 'react';
import { INTERNAL_HOOKS } from '../constant';
import type {
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useFixedInfo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import useMemo from 'rc-util/lib/hooks/useMemo';
import isEqual from 'rc-util/lib/isEqual';
import useMemo from '@rc-component/util/lib/hooks/useMemo';
import isEqual from '@rc-component/util/lib/isEqual';
import type { ColumnType, Direction, StickyOffsets } from '../interface';
import { getCellFixedInfo } from '../utils/fixUtil';

Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useRowInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useContext } from '@rc-component/context';
import type { TableContextProps } from '../context/TableContext';
import TableContext from '../context/TableContext';
import { getColumnsKey } from '../utils/valueUtil';
import { useEvent } from 'rc-util';
import { useEvent } from '@rc-component/util';
import classNames from 'classnames';

export default function useRowInfo<RecordType>(
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useSticky.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import canUseDom from 'rc-util/lib/Dom/canUseDom';
import canUseDom from '@rc-component/util/lib/Dom/canUseDom';
import type { TableSticky } from '../interface';

// fix ssr render
Expand Down
43 changes: 21 additions & 22 deletions src/stickyScrollBar.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { useContext } from '@rc-component/context';
import classNames from 'classnames';
import addEventListener from 'rc-util/lib/Dom/addEventListener';
import getScrollBarSize from 'rc-util/lib/getScrollBarSize';
import getScrollBarSize from '@rc-component/util/lib/getScrollBarSize';
import * as React from 'react';
import TableContext from './context/TableContext';
import { useLayoutState } from './hooks/useFrame';
import raf from 'rc-util/lib/raf';
import raf from '@rc-component/util/lib/raf';
import { getOffset } from './utils/offsetUtil';

const MOUSEUP_EVENT: keyof WindowEventMap = 'mouseup';
const MOUSEMOVE_EVENT: keyof WindowEventMap = 'mousemove';
const SCROLL_EVENT: keyof WindowEventMap = 'scroll';
const RESIZE_EVENT: keyof WindowEventMap = 'resize';

interface StickyScrollBarProps {
scrollBodyRef: React.RefObject<HTMLDivElement>;
onScroll: (params: { scrollLeft?: number }) => void;
Expand Down Expand Up @@ -50,19 +54,19 @@ const StickyScrollBar: React.ForwardRefRenderFunction<unknown, StickyScrollBarPr
[],
);

const onMouseUp: React.MouseEventHandler<HTMLDivElement> = () => {
const onMouseUp = () => {
setActive(false);
};

const onMouseDown: React.MouseEventHandler<HTMLDivElement> = event => {
const onMouseDown = (event: React.MouseEvent) => {
event.persist();
refState.current.delta = event.pageX - scrollState.scrollLeft;
refState.current.x = 0;
setActive(true);
event.preventDefault();
};

const onMouseMove: React.MouseEventHandler<HTMLDivElement> = event => {
const onMouseMove = (event: any) => {
// https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttons
const { buttons } = event || (window?.event as any);
if (!isActive || buttons === 0) {
Expand All @@ -72,8 +76,7 @@ const StickyScrollBar: React.ForwardRefRenderFunction<unknown, StickyScrollBarPr
}
return;
}
let left: number =
refState.current.x + event.pageX - refState.current.x - refState.current.delta;
let left = refState.current.x + event.pageX - refState.current.x - refState.current.delta;

const isRTL = direction === 'rtl';
// Limit scroll range
Expand Down Expand Up @@ -135,22 +138,21 @@ const StickyScrollBar: React.ForwardRefRenderFunction<unknown, StickyScrollBarPr
}));

React.useEffect(() => {
const onMouseUpListener = addEventListener(document.body, 'mouseup', onMouseUp, false);
const onMouseMoveListener = addEventListener(document.body, 'mousemove', onMouseMove, false);
document.body.addEventListener(MOUSEUP_EVENT, onMouseUp, false);
document.body.addEventListener(MOUSEMOVE_EVENT, onMouseMove, false);
checkScrollBarVisible();
return () => {
onMouseUpListener.remove();
onMouseMoveListener.remove();
document.body.removeEventListener(MOUSEUP_EVENT, onMouseUp);
document.body.removeEventListener(MOUSEMOVE_EVENT, onMouseMove);
};
}, [scrollBarWidth, isActive]);

React.useEffect(() => {
const onScrollListener = addEventListener(container, 'scroll', checkScrollBarVisible, false);
const onResizeListener = addEventListener(window, 'resize', checkScrollBarVisible, false);

container.addEventListener(SCROLL_EVENT, checkScrollBarVisible, false);
window.addEventListener(RESIZE_EVENT, checkScrollBarVisible, false);
return () => {
onScrollListener.remove();
onResizeListener.remove();
container.removeEventListener(SCROLL_EVENT, checkScrollBarVisible);
window.removeEventListener(RESIZE_EVENT, checkScrollBarVisible);
};
}, [container]);

Expand All @@ -167,6 +169,7 @@ const StickyScrollBar: React.ForwardRefRenderFunction<unknown, StickyScrollBarPr
};
});
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [scrollState.isHiddenScrollBar]);

if (bodyScrollWidth <= bodyWidth || !scrollBarWidth || scrollState.isHiddenScrollBar) {
Expand All @@ -175,11 +178,7 @@ const StickyScrollBar: React.ForwardRefRenderFunction<unknown, StickyScrollBarPr

return (
<div
style={{
height: getScrollBarSize(),
width: bodyWidth,
bottom: offsetScroll,
}}
style={{ height: getScrollBarSize(), width: bodyWidth, bottom: offsetScroll }}
className={`${prefixCls}-sticky-scroll`}
>
<div
Expand Down
2 changes: 1 addition & 1 deletion src/utils/legacyUtil.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import warning from 'rc-util/lib/warning';
import warning from '@rc-component/util/lib/warning';
import type { ExpandableConfig, LegacyExpandableProps } from '../interface';

export const INTERNAL_COL_DEFINE = 'RC_TABLE_INTERNAL_COL_DEFINE';
Expand Down
4 changes: 2 additions & 2 deletions src/utils/offsetUtil.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getDOM } from 'rc-util/lib/Dom/findDOMNode';
import { getDOM } from '@rc-component/util/lib/Dom/findDOMNode';

// Copy from `rc-util/Dom/css.js`
// Copy from `rc-component/util/Dom/css.js`
export function getOffset(node: HTMLElement | Window) {
const element = getDOM(node);
const box = element.getBoundingClientRect();
Expand Down
2 changes: 1 addition & 1 deletion tests/Deprecated.spec.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { mount } from 'enzyme';
import { resetWarned } from 'rc-util/lib/warning';
import { resetWarned } from '@rc-component/util/lib/warning';
import React from 'react';
import Table from '../src';

Expand Down
4 changes: 2 additions & 2 deletions tests/ExpandRow.spec.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { render } from '@testing-library/react';
import { mount } from 'enzyme';
import { spyElementPrototype } from 'rc-util/lib/test/domHook';
import { resetWarned } from 'rc-util/lib/warning';
import { spyElementPrototype } from '@rc-component/util/lib/test/domHook';
import { resetWarned } from '@rc-component/util/lib/warning';
import React from 'react';
import { act } from 'react-dom/test-utils';
import Table from '../src';
Expand Down
4 changes: 2 additions & 2 deletions tests/FixedColumn-IE.spec.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { mount } from 'enzyme';
import { spyElementPrototype } from 'rc-util/lib/test/domHook';
import { spyElementPrototype } from '@rc-component/util/lib/test/domHook';
import React from 'react';
import { act } from 'react-dom/test-utils';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import RcResizeObserver from 'rc-resize-observer';
import Table from '../src';

vi.mock('rc-util/lib/Dom/styleChecker', () => {
vi.mock('@rc-component/util/lib/Dom/styleChecker', () => {
return {
isStyleSupport: (name, val) => val !== 'sticky',
};
Expand Down
2 changes: 1 addition & 1 deletion tests/FixedColumn.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { mount } from 'enzyme';
import { render } from '@testing-library/react';
import RcResizeObserver, { _rs } from 'rc-resize-observer';
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
import { spyElementPrototypes } from '@rc-component/util/lib/test/domHook';
import { act } from 'react-dom/test-utils';
import Table, { type ColumnsType } from '../src';
import { safeAct } from './utils';
Expand Down
2 changes: 1 addition & 1 deletion tests/FixedHeader.spec.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { mount } from 'enzyme';
import RcResizeObserver from 'rc-resize-observer';
import { spyElementPrototype } from 'rc-util/lib/test/domHook';
import { spyElementPrototype } from '@rc-component/util/lib/test/domHook';
import React from 'react';
import { act } from 'react-dom/test-utils';
import Table, { INTERNAL_COL_DEFINE } from '../src';
Expand Down
4 changes: 2 additions & 2 deletions tests/Hover.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { mount } from 'enzyme';
import toArray from 'rc-util/lib/Children/toArray';
import { resetWarned } from 'rc-util/lib/warning';
import toArray from '@rc-component/util/lib/Children/toArray';
import { resetWarned } from '@rc-component/util/lib/warning';
import React from 'react';
import Table from '../src';
import type { TableProps } from '../src/Table';
Expand Down
2 changes: 1 addition & 1 deletion tests/Scroll.spec.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { mount } from 'enzyme';
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
import { spyElementPrototypes } from '@rc-component/util/lib/test/domHook';
import React from 'react';
import { act } from 'react-dom/test-utils';
import Table from '../src';
Expand Down
2 changes: 1 addition & 1 deletion tests/Sticky.spec.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { mount } from 'enzyme';
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
import { spyElementPrototypes } from '@rc-component/util/lib/test/domHook';
import React from 'react';
import { act } from 'react-dom/test-utils';
import Table from '../src';
Expand Down
2 changes: 1 addition & 1 deletion tests/Table.spec.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { mount } from 'enzyme';
import { resetWarned } from 'rc-util/lib/warning';
import { resetWarned } from '@rc-component/util/lib/warning';
import React from 'react';
import { VariableSizeGrid as Grid } from 'react-window';
import Table, { INTERNAL_COL_DEFINE } from '../src';
Expand Down
4 changes: 2 additions & 2 deletions tests/Virtual.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { act, fireEvent, render } from '@testing-library/react';
import { _rs as onEsResize } from 'rc-resize-observer/es/utils/observerUtil';
import { _rs as onLibResize } from 'rc-resize-observer/lib/utils/observerUtil';
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
import { resetWarned } from 'rc-util/lib/warning';
import { spyElementPrototypes } from '@rc-component/util/lib/test/domHook';
import { resetWarned } from '@rc-component/util/lib/warning';
import React from 'react';
import { VirtualTable, type Reference, type VirtualTableProps } from '../src';

Expand Down
2 changes: 1 addition & 1 deletion tests/refs.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { render } from '@testing-library/react';
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
import { spyElementPrototypes } from '@rc-component/util/lib/test/domHook';
import React from 'react';
import Table, { type Reference } from '../src';

Expand Down
2 changes: 1 addition & 1 deletion tests/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ window.getComputedStyle = elt => getComputedStyle(elt);
global.requestAnimationFrame = cb => setTimeout(cb, 0);
require('regenerator-runtime');

vi.mock('rc-util/lib/getScrollBarSize');
vi.mock('@rc-component/util/lib/getScrollBarSize');

const Enzyme = require('enzyme');
const Adapter = require('enzyme-adapter-react-16');
Expand Down
Loading