Skip to content

Commit

Permalink
fix(table): fix both use ellipsis and onFilter error (#770)
Browse files Browse the repository at this point in the history
Co-authored-by: zhaoting zhou <zhouzhaoting@growingio.com>
  • Loading branch information
Lee Hon and zhaoting zhou authored Feb 5, 2021
1 parent 53f4eef commit 4722d54
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/components/avatar/__tests__/Avatar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('Testing Avatar', () => {
const wrapper = mount(<Avatar displayTooltip>这是一个很长的文字</Avatar>);
wrapper.setProps({ placement: 'top' });
wrapper.find('.gio-avatar').at(0).simulate('mouseenter');
await waitForComponentToPaint(wrapper);
await waitForComponentToPaint(wrapper, 3000);
expect(wrapper.exists('.gio-tooltip-placement-top')).toBe(true);
});

Expand Down
32 changes: 17 additions & 15 deletions src/components/table/FilterPopover.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import React, { useState } from 'react';
/* eslint-disable react/jsx-wrap-multilines */
import React, { useState, useContext } from 'react';
import Button from '../button';
import Popover from '../popover';
import List from '../list';
import SearchBar from '../search-bar';
import { TableContext } from './Table';

interface FilterPopoverProps {
prefixCls: string;
Expand All @@ -17,36 +19,36 @@ const FilterPopover = (props: FilterPopoverProps): React.ReactElement => {
const [seachValue, setSearchValue] = useState<string>('');
const [selectFilterKeys, setSelectFilterKeys] = useState<string[]>(values);
const [visible, setVisible] = useState<boolean>(false);
const { tableRef } = useContext(TableContext);
return (
<Popover
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
getTooltipContainer={(triggerNode) => triggerNode.parentElement!}
getTooltipContainer={(triggerNode) => tableRef?.current || triggerNode.parentElement!}
arrowContent={null}
visible={visible}
onVisibleChange={setVisible}
placement="bottomLeft"
trigger="click"
contentArea={(
contentArea={
<>
<SearchBar placeholder='搜索过滤条件' size='small' value={seachValue} onChange={setSearchValue} />
<SearchBar placeholder="搜索过滤条件" size="small" value={seachValue} onChange={setSearchValue} />
<List
wrapStyle={{ padding: 0 }}
isMultiple
value={selectFilterKeys}
onChange={setSelectFilterKeys}
width={220}
height={160}
dataSource={
filters
dataSource={filters
.filter((item: string | number) => item.toString().includes(seachValue))
.map((item: string | number) => ({ label: item.toString(), value: item.toString()}))
}
.map((item: string | number) => ({ label: item.toString(), value: item.toString() }))}
/>
<div className='filter-popover-footer'>
<div className="filter-popover-footer">
<Button
style={{ color: '#c7cbd8'}}
type='text'
style={{ color: '#c7cbd8' }}
type="text"
ghost
size='small'
size="small"
onClick={() => {
setSearchValue('');
setSelectFilterKeys([]);
Expand All @@ -55,8 +57,8 @@ const FilterPopover = (props: FilterPopoverProps): React.ReactElement => {
清除
</Button>
<Button
style={{ float: 'right'}}
size='small'
style={{ float: 'right' }}
size="small"
onClick={() => {
onClick(selectFilterKeys);
setVisible(false);
Expand All @@ -66,7 +68,7 @@ const FilterPopover = (props: FilterPopoverProps): React.ReactElement => {
</Button>
</div>
</>
)}
}
>
{children}
</Popover>
Expand Down
16 changes: 16 additions & 0 deletions src/components/table/Table.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Subtitle, ArgsTable } from '@storybook/addon-docs/blocks';
import Table from './index';

# Table 列表

<Subtitle>通用列表。</Subtitle>

## 参数说明

<ArgsTable of={Table} />

## 代码演示

### 所有Demo

[Example](/?path=/story/functional-components-table--default)
22 changes: 19 additions & 3 deletions src/components/table/Table.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import React from 'react';
import { Story, Meta } from '@storybook/react/types-6-0';

import Docs from './Table.mdx';
import Table from './index';
import { TableProps } from './interface';
import './style';

export default {
title: 'Components/Functional/Table',
title: 'Functional Components/Table',
component: Table,
parameters: {
docs: {
page: Docs,
},
},
} as Meta;

const Template: Story<TableProps<ExampleData>> = (args) => <Table {...args} />;
Expand All @@ -28,7 +33,7 @@ const dataSource: ExampleData[] = [
},
{
key: '2',
name: '列表文本',
name: '列表文本2',
age: '列表文本',
address: '列表文本',
},
Expand All @@ -50,6 +55,17 @@ const columns = [
title: '列标题2',
dataIndex: 'age',
key: 'age',
filters: ['名字四个字', '名字不是四个字'],
ellipsis: true,
onFilter: (value: string, record: ExampleData) => {
if (value === '名字四个字') {
return record.name.length === 4;
}
if (value === '名字不是四个字') {
return record.name.length !== 4;
}
return false;
},
},
{
title: '列标题3',
Expand Down
60 changes: 36 additions & 24 deletions src/components/table/Table.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React, { useMemo } from 'react';
import React, { useMemo, forwardRef, createContext } from 'react';
import RcTable from 'rc-table';
import classNames from 'classnames';
import { cloneDeep, isUndefined, get, has, set } from 'lodash';
import { compose } from 'lodash/fp';
import usePrefixCls from '../../utils/hooks/use-prefix-cls';
import useMergeRef from '../../utils/hooks/useMergeRef';
import useSorter from './hook/useSorter';
import useFilter from './hook/useFilter';
import usePagination from './hook/usePagination';
Expand All @@ -17,7 +18,15 @@ import Loading from '../loading';
import useDebounceLoading from '../../utils/hooks/useDebounceLoading';
import useHackOnRow from './hook/useHackOnRow';

const Table = <RecordType,>(props: TableProps<RecordType>): React.ReactElement => {
interface TableContextType {
tableRef: null | React.MutableRefObject<HTMLDivElement>;
}
export const TableContext = createContext({ tableRef: null } as TableContextType);

const Table = <RecordType,>(
props: TableProps<RecordType>,
ref: React.MutableRefObject<HTMLDivElement>
): React.ReactElement => {
const {
prefixCls: customizePrefixCls,
title,
Expand All @@ -37,7 +46,7 @@ const Table = <RecordType,>(props: TableProps<RecordType>): React.ReactElement =
style,
...rest
} = props;

const mergedRef = useMergeRef(ref);
const prefixCls = usePrefixCls('table', customizePrefixCls);
const debounceLoading = useDebounceLoading(loading, 1000);
const onHackRow = useHackOnRow(onRow, hackRowEvent);
Expand Down Expand Up @@ -113,27 +122,30 @@ const Table = <RecordType,>(props: TableProps<RecordType>): React.ReactElement =
);

return (
<div
className={classNames(`${prefixCls}-wrapper`, className, {
[`${prefixCls}-showHover`]: showHover,
})}
style={style}
>
<Loading loading={debounceLoading}>
<RcTable
title={title ? () => title : undefined}
prefixCls={prefixCls}
columns={composedColumns}
data={paginationedData}
emptyText={emptyElement}
rowKey={rowKey}
onRow={onHackRow}
{...rest}
/>
<PaginationComponent onTriggerStateUpdate={onTriggerStateUpdate} />
</Loading>
</div>
<TableContext.Provider value={{ tableRef: mergedRef }}>
<div
className={classNames(`${prefixCls}-wrapper`, className, {
[`${prefixCls}-showHover`]: showHover,
})}
style={style}
ref={mergedRef}
>
<Loading loading={debounceLoading}>
<RcTable
title={title ? () => title : undefined}
prefixCls={prefixCls}
columns={composedColumns}
data={paginationedData}
emptyText={emptyElement}
rowKey={rowKey}
onRow={onHackRow}
{...rest}
/>
<PaginationComponent onTriggerStateUpdate={onTriggerStateUpdate} />
</Loading>
</div>
</TableContext.Provider>
);
};

export default Table;
export default forwardRef(Table);
1 change: 1 addition & 0 deletions src/components/table/style/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

.@{table-prefix-cls} {
font-size: @size-font-14;
text-align: left;
&-title {
margin-bottom: 20px;
color: @color-text-table-head;
Expand Down
3 changes: 2 additions & 1 deletion src/utils/composeRef.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import React from 'react';

const composeRef = <T>(...refs: React.Ref<T>[]): React.RefCallback<T> => (node: T) => {
const composeRef = <T>(...refs: React.Ref<T>[]): React.Ref<T> => (node: T) => {
refs.forEach((ref) => {
if (typeof ref === 'function') {
ref(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const columns2 = [
title: '年龄',
dataIndex: 'age',
key: 'age',
ellipsis: true,
info: '这里是用户的年龄',
filters: ['小孩子', '大人'],
onFilter: (value, record) => {
Expand Down

1 comment on commit 4722d54

@vercel
Copy link

@vercel vercel bot commented on 4722d54 Feb 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.