Skip to content

Commit

Permalink
โœจ feat(list): support expandIcon(#1710) (#1771)
Browse files Browse the repository at this point in the history
* โœจ feat(list): support expandIcon(#1710)

* ๐ŸŽจ chore: Delete unused variables

* ๐Ÿ› fix(list): fix Typescript type error

* ๐Ÿ› fix(list): fix test

* fix error

* fix ts

* โœจ feat(list): add expandIcon test

* ๐Ÿ› fix(list): fix test

Co-authored-by: chenshuai2144 <qixian.cs@outlook.com>
  • Loading branch information
DerrickTel and chenshuai2144 committed Jan 29, 2021
1 parent 12220d5 commit ff822b6
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 12 deletions.
32 changes: 24 additions & 8 deletions packages/list/src/Item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,45 @@ import React, { useContext } from 'react';
import { List, Avatar, Skeleton, ConfigProvider } from 'antd';
import type { ProCardProps } from '@ant-design/pro-card';
import ProCard from '@ant-design/pro-card';
import { RightOutlined } from '@ant-design/icons';
import useMergedState from 'rc-util/lib/hooks/useMergedState';
import type { ListGridType } from 'antd/lib/list';
import type { ExpandableConfig } from 'antd/lib/table/interface';
import { RightOutlined } from '@ant-design/icons';
import classNames from 'classnames';

export type RenderExpandIconProps = {
export type RenderExpandIconProps<RecordType> = {
prefixCls: string;
expanded: boolean;
expandIcon: React.ReactNode;
expandIcon:
| React.ReactNode
| ((props: {
onExpand: (expanded: boolean) => void;
expanded: boolean;
record: RecordType;
}) => React.ReactNode);
onExpand: (expanded: boolean) => void;
record: RecordType;
};

export function renderExpandIcon({
export function renderExpandIcon<RecordType>({
prefixCls,
expandIcon = <RightOutlined />,
onExpand,
expanded,
}: RenderExpandIconProps) {
record,
}: RenderExpandIconProps<RecordType>) {
let icon = expandIcon;
const expandClassName = `${prefixCls}-row-expand-icon`;

const onClick: React.MouseEventHandler<HTMLElement> = (event) => {
onExpand(!expanded);
event.stopPropagation();
};

if (typeof expandIcon === 'function') {
icon = expandIcon({ expanded, onExpand, record });
}

return (
<span
className={classNames(expandClassName, {
Expand All @@ -36,12 +49,12 @@ export function renderExpandIcon({
})}
onClick={onClick}
>
{expandIcon}
{icon}
</span>
);
}

export type ItemProps = {
export type ItemProps<RecordType> = {
title?: React.ReactNode;
subTitle?: React.ReactNode;
checkbox?: React.ReactNode;
Expand Down Expand Up @@ -71,9 +84,10 @@ export type ItemProps = {
isEditable: boolean;
recordKey: string | number | undefined;
cardProps?: ProCardProps;
record?: RecordType;
};

function ProListItem(props: ItemProps) {
function ProListItem<RecordType>(props: ItemProps<RecordType>) {
const { prefixCls: customizePrefixCls } = props;
const { getPrefixCls } = useContext(ConfigProvider.ConfigContext);
const prefixCls = getPrefixCls('pro-list', customizePrefixCls);
Expand Down Expand Up @@ -103,6 +117,7 @@ function ProListItem(props: ItemProps) {
type,
style,
className: propsClassName = defaultClassName,
record,
...rest
} = props;

Expand Down Expand Up @@ -148,6 +163,7 @@ function ProListItem(props: ItemProps) {
expandIcon,
onExpand,
expanded,
record,
})}
</div>
{title || avatar || subTitle || description ? (
Expand Down
1 change: 1 addition & 0 deletions packages/list/src/ListView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ function ListView<RecordType>(props: ListViewProps<RecordType>) {
onExpand={() => {
onTriggerExpand(item);
}}
record={item}
showActions={showActions}
rowSupportExpand={!rowExpandable || (rowExpandable && rowExpandable(item))}
selected={selectedKeySet.has(getRowKey(item, index))}
Expand Down
8 changes: 4 additions & 4 deletions packages/table/src/components/Form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ const getFromProps = (isForm: boolean, searchConfig: any, name: string) => {
* ไปŽformConfigไธญ่Žทๅ–ไผ ็ป™็›ธๅบ”่กจๅ•็š„้…็ฝฎ
*
* @param isForm
* @param formCofig
* @param formConfig
*/
const getFormConfigs = (isForm: boolean, formCofig: any) => {
const getFormConfigs = (isForm: boolean, formConfig: any) => {
if (isForm) {
// ไผ ็ป™Form็š„้…็ฝฎ
return omit(formCofig, ['ignoreRules']);
return omit(formConfig, ['ignoreRules']);
}
// ไผ ็ป™Filter็š„้…็ฝฎ
return { ignoreRules: true, ...formCofig };
return { ignoreRules: true, ...formConfig };
};

export type TableFormItem<T, U = any> = {
Expand Down
35 changes: 35 additions & 0 deletions tests/list/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,41 @@ describe('List', () => {
);
});

it('๐Ÿš expandable with expandIcon', async () => {
const fn = jest.fn();
const Wrapper = () => {
return (
<ProList
dataSource={[
{
name: 'ๆˆ‘ๆ˜ฏๅ็งฐ',
content: <div>ๆˆ‘ๆ˜ฏๅ†…ๅฎน</div>,
},
]}
metas={{
title: {
dataIndex: 'name',
},
content: {},
}}
expandable={{
expandIcon: ({ record }) => (
<div onClick={() => fn(record.name)} className="expand-icon" />
),
}}
rowKey={(item) => {
return item.name;
}}
/>
);
};
const html = mount(<Wrapper />);

expect(html.find('.expand-icon')).toHaveLength(1);

expect(fn).toBeCalledWith('ๆˆ‘ๆ˜ฏๅ็งฐ');
});

it('๐Ÿš rowSelection', async () => {
const Wrapper = () => {
const [selectedRowKeys, setSelectedRowKeys] = useState<ReactText[]>([]);
Expand Down

0 comments on commit ff822b6

Please sign in to comment.