Browse Source

feat: migrate less to token for Transfer (#42431)

*  feat: migrate less to token for Transfer

*  feat: update

*  feat: update demo

*  feat: update snap

*  feat: update

*  feat: update

*  feat: update demo

*  feat: update

*  feat: update

*  feat: update

*  feat: update

*  feat: update

*  feat: update

*  feat: update

*  feat: update

*  feat: update

*  feat: update
pull/42819/head
黑雨 2 years ago
committed by GitHub
parent
commit
6227806842
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 3517
      components/transfer/__tests__/__snapshots__/demo-extend.test.ts.snap
  2. 2669
      components/transfer/__tests__/__snapshots__/demo.test.ts.snap
  3. 7
      components/transfer/demo/component-token.md
  4. 207
      components/transfer/demo/component-token.tsx
  5. 1
      components/transfer/index.en-US.md
  6. 1
      components/transfer/index.zh-CN.md
  7. 70
      components/transfer/style/index.ts
  8. 14
      docs/react/migrate-less-variables.en-US.md
  9. 14
      docs/react/migrate-less-variables.zh-CN.md

3517
components/transfer/__tests__/__snapshots__/demo-extend.test.ts.snap

File diff suppressed because it is too large

2669
components/transfer/__tests__/__snapshots__/demo.test.ts.snap

File diff suppressed because it is too large

7
components/transfer/demo/component-token.md

@ -0,0 +1,7 @@
## zh-CN
Component Token Debug.
## en-US
Component Token Debug.

207
components/transfer/demo/component-token.tsx

@ -0,0 +1,207 @@
import { ConfigProvider, Space, Switch, Table, Tag, Transfer } from 'antd';
import type { ColumnsType, TableRowSelection } from 'antd/es/table/interface';
import type { TransferDirection, TransferItem, TransferProps } from 'antd/es/transfer';
import difference from 'lodash/difference';
import React, { useState } from 'react';
interface RecordType {
key: string;
title: string;
description: string;
disabled: boolean;
tag: string;
}
interface DataType {
key: string;
title: string;
description: string;
disabled: boolean;
tag: string;
}
interface TableTransferProps extends TransferProps<TransferItem> {
dataSource: DataType[];
leftColumns: ColumnsType<DataType>;
rightColumns: ColumnsType<DataType>;
}
// Customize Table Transfer
const TableTransfer = ({ leftColumns, rightColumns, ...restProps }: TableTransferProps) => (
<Transfer {...restProps}>
{({
direction,
filteredItems,
onItemSelectAll,
onItemSelect,
selectedKeys: listSelectedKeys,
disabled: listDisabled,
}) => {
const columns = direction === 'left' ? leftColumns : rightColumns;
const rowSelection: TableRowSelection<TransferItem> = {
getCheckboxProps: (item) => ({ disabled: listDisabled || item.disabled }),
onSelectAll(selected, selectedRows) {
const treeSelectedKeys = selectedRows
.filter((item) => !item.disabled)
.map(({ key }) => key);
const diffKeys = selected
? difference(treeSelectedKeys, listSelectedKeys)
: difference(listSelectedKeys, treeSelectedKeys);
onItemSelectAll(diffKeys as string[], selected);
},
onSelect({ key }, selected) {
onItemSelect(key as string, selected);
},
selectedRowKeys: listSelectedKeys,
};
return (
<Table
rowSelection={rowSelection}
columns={columns}
dataSource={filteredItems}
size="small"
style={{ pointerEvents: listDisabled ? 'none' : undefined }}
onRow={({ key, disabled: itemDisabled }) => ({
onClick: () => {
if (itemDisabled || listDisabled) return;
onItemSelect(key as string, !listSelectedKeys.includes(key as string));
},
})}
/>
);
}}
</Transfer>
);
const mockTags = ['cat', 'dog', 'bird'];
const mockData: RecordType[] = Array.from({ length: 20 }).map((_, i) => ({
key: i.toString(),
title: `content${i + 1}`,
description: `description of content${i + 1}`,
disabled: i % 4 === 0,
tag: mockTags[i % 3],
}));
const leftTableColumns: ColumnsType<DataType> = [
{
dataIndex: 'title',
title: 'Name',
},
{
dataIndex: 'tag',
title: 'Tag',
render: (tag) => <Tag>{tag}</Tag>,
},
{
dataIndex: 'description',
title: 'Description',
},
];
const rightTableColumns: ColumnsType<Pick<DataType, 'title'>> = [
{
dataIndex: 'title',
title: 'Name',
},
];
const initialTargetKeys = mockData.filter((item) => Number(item.key) > 10).map((item) => item.key);
const App: React.FC = () => {
const [targetKeys, setTargetKeys] = useState(initialTargetKeys);
const [selectedKeys, setSelectedKeys] = useState<string[]>([]);
const onChange = (nextTargetKeys: string[], direction: TransferDirection, moveKeys: string[]) => {
console.log('targetKeys:', nextTargetKeys);
console.log('direction:', direction);
console.log('moveKeys:', moveKeys);
setTargetKeys(nextTargetKeys);
};
const onSelectChange = (sourceSelectedKeys: string[], targetSelectedKeys: string[]) => {
console.log('sourceSelectedKeys:', sourceSelectedKeys);
console.log('targetSelectedKeys:', targetSelectedKeys);
setSelectedKeys([...sourceSelectedKeys, ...targetSelectedKeys]);
};
const onScroll = (direction: TransferDirection, e: React.SyntheticEvent<HTMLUListElement>) => {
console.log('direction:', direction);
console.log('target:', e.target);
};
const [disabled, setDisabled] = useState(false);
const [showSearch, setShowSearch] = useState(false);
const secondOnChange = (nextTargetKeys: string[]) => {
setTargetKeys(nextTargetKeys);
};
const triggerDisable = (checked: boolean) => {
setDisabled(checked);
};
const triggerShowSearch = (checked: boolean) => {
setShowSearch(checked);
};
return (
<ConfigProvider
theme={{
components: {
Transfer: {
listWidth: 40,
listWidthLG: 50,
listHeight: 30,
itemHeight: 20,
itemPaddingBlock: 10,
headerHeight: 18,
},
},
}}
>
<Transfer
dataSource={mockData}
titles={['Source', 'Target']}
targetKeys={targetKeys}
selectedKeys={selectedKeys}
onChange={onChange}
onSelectChange={onSelectChange}
onScroll={onScroll}
render={(item) => item.title}
/>
<Transfer status="error" />
<Transfer status="warning" showSearch />
<TableTransfer
dataSource={mockData}
targetKeys={targetKeys}
disabled={disabled}
showSearch={showSearch}
onChange={secondOnChange}
filterOption={(inputValue, item) =>
item.title!.indexOf(inputValue) !== -1 || item.tag.indexOf(inputValue) !== -1
}
leftColumns={leftTableColumns}
rightColumns={rightTableColumns}
/>
<Space style={{ marginTop: 16 }}>
<Switch
unCheckedChildren="disabled"
checkedChildren="disabled"
checked={disabled}
onChange={triggerDisable}
/>
<Switch
unCheckedChildren="showSearch"
checkedChildren="showSearch"
checked={showSearch}
onChange={triggerShowSearch}
/>
</Space>
</ConfigProvider>
);
};
export default App;

1
components/transfer/index.en-US.md

@ -32,6 +32,7 @@ One or more elements can be selected from either column, one click on the proper
<code src="./demo/tree-transfer.tsx">Tree Transfer</code>
<code src="./demo/status.tsx">Status</code>
<code src="./demo/custom-select-all-labels.tsx" debug>Custom Select All Labels</code>
<code src="./demo/component-token.tsx" debug>Component Token</code>
## API

1
components/transfer/index.zh-CN.md

@ -33,6 +33,7 @@ coverDark: https://mdn.alipayobjects.com/huamei_7uahnr/afts/img/A*yv12S4sSRAEAAA
<code src="./demo/tree-transfer.tsx">树穿梭框</code>
<code src="./demo/status.tsx">自定义状态</code>
<code src="./demo/custom-select-all-labels.tsx" debug>自定义全选文字</code>
<code src="./demo/component-token.tsx" debug>组件 Token</code>
## API

70
components/transfer/style/index.ts

@ -1,20 +1,19 @@
import type { CSSObject } from '@ant-design/cssinjs';
import { resetComponent, resetIcon, textEllipsis } from '../../style';
import type { FullToken, GenerateStyle } from '../../theme/internal';
import { genComponentStyleHook, mergeToken } from '../../theme/internal';
import { resetComponent, resetIcon, textEllipsis } from '../../style';
export interface ComponentToken {
listWidth: number;
listWidthLG: number;
listHeight: number;
itemHeight: number;
itemPaddingBlock: number;
headerHeight: number;
}
interface TransferToken extends FullToken<'Transfer'> {
transferItemHeight: number;
transferHeaderVerticalPadding: number;
transferItemPaddingVertical: number;
transferHeaderHeight: number;
}
const genTransferCustomizeStyle: GenerateStyle<TransferToken> = (
@ -89,12 +88,11 @@ const genTransferListStyle: GenerateStyle<TransferToken> = (token: TransferToken
colorBorder,
colorSplit,
lineWidth,
transferItemHeight,
transferHeaderHeight,
itemHeight,
headerHeight,
transferHeaderVerticalPadding,
transferItemPaddingVertical,
itemPaddingBlock,
controlItemBgActive,
controlItemBgActiveHover,
colorTextDisabled,
listHeight,
listWidth,
@ -105,6 +103,11 @@ const genTransferListStyle: GenerateStyle<TransferToken> = (token: TransferToken
lineType,
iconCls,
motionDurationSlow,
controlItemBgHover,
borderRadiusLG,
colorBgContainer,
colorText,
controlItemBgActiveHover,
} = token;
return {
@ -130,15 +133,15 @@ const genTransferListStyle: GenerateStyle<TransferToken> = (token: TransferToken
display: 'flex',
flex: 'none',
alignItems: 'center',
height: transferHeaderHeight,
height: headerHeight,
// border-top is on the transfer dom. We should minus 1px for this
padding: `${
transferHeaderVerticalPadding - lineWidth
}px ${paddingSM}px ${transferHeaderVerticalPadding}px`,
color: token.colorText,
background: token.colorBgContainer,
color: colorText,
background: colorBgContainer,
borderBottom: `${lineWidth}px ${lineType} ${colorSplit}`,
borderRadius: `${token.borderRadiusLG}px ${token.borderRadiusLG}px 0 0`,
borderRadius: `${borderRadiusLG}px ${borderRadiusLG}px 0 0`,
'> *:not(:last-child)': {
marginInlineEnd: 4, // This is magic and fixed number, DO NOT use token since it may change.
@ -191,8 +194,8 @@ const genTransferListStyle: GenerateStyle<TransferToken> = (token: TransferToken
'&-item': {
display: 'flex',
alignItems: 'center',
minHeight: transferItemHeight,
padding: `${transferItemPaddingVertical}px ${paddingSM}px`,
minHeight: itemHeight,
padding: `${itemPaddingBlock}px ${paddingSM}px`,
transition: `all ${motionDurationSlow}`,
'> *:not(:last-child)': {
@ -221,14 +224,14 @@ const genTransferListStyle: GenerateStyle<TransferToken> = (token: TransferToken
'&::after': {
position: 'absolute',
insert: `-${transferItemPaddingVertical}px -50%`,
inset: `-${itemPaddingBlock}px -50%`,
content: '""',
},
},
[`&:not(${componentCls}-list-content-item-disabled)`]: {
'&:hover': {
backgroundColor: token.controlItemBgHover,
backgroundColor: controlItemBgHover,
cursor: 'pointer',
},
@ -280,12 +283,13 @@ const genTransferStyle: GenerateStyle<TransferToken> = (token: TransferToken): C
antCls,
iconCls,
componentCls,
transferHeaderHeight,
headerHeight,
marginXS,
marginXXS,
fontSizeIcon,
fontSize,
lineHeight,
colorBgContainerDisabled,
} = token;
return {
@ -298,7 +302,7 @@ const genTransferStyle: GenerateStyle<TransferToken> = (token: TransferToken): C
[`${componentCls}-disabled`]: {
[`${componentCls}-list`]: {
background: token.colorBgContainerDisabled,
background: colorBgContainerDisabled,
},
},
@ -326,7 +330,7 @@ const genTransferStyle: GenerateStyle<TransferToken> = (token: TransferToken): C
},
[`${antCls}-empty-image`]: {
maxHeight: transferHeaderHeight / 2 - Math.round(fontSize * lineHeight),
maxHeight: headerHeight / 2 - Math.round(fontSize * lineHeight),
},
},
};
@ -345,17 +349,10 @@ const genTransferRTLStyle: GenerateStyle<TransferToken> = (token: TransferToken)
export default genComponentStyleHook(
'Transfer',
(token) => {
const { fontSize, lineHeight, lineWidth, controlHeightLG, controlHeight } = token;
const { fontSize, lineHeight, lineWidth, controlHeightLG } = token;
const fontHeight = Math.round(fontSize * lineHeight);
const transferHeaderHeight = controlHeightLG;
const transferItemHeight = controlHeight;
const transferToken = mergeToken<TransferToken>(token, {
transferItemHeight,
transferHeaderHeight,
transferHeaderVerticalPadding: Math.ceil((transferHeaderHeight - lineWidth - fontHeight) / 2),
transferItemPaddingVertical: (transferItemHeight - fontHeight) / 2,
transferHeaderVerticalPadding: Math.ceil((controlHeightLG - lineWidth - fontHeight) / 2),
});
return [
@ -365,9 +362,16 @@ export default genComponentStyleHook(
genTransferRTLStyle(transferToken),
];
},
{
listWidth: 180,
listHeight: 200,
listWidthLG: 250,
(token) => {
const { fontSize, lineHeight, controlHeight, controlHeightLG } = token;
const fontHeight = Math.round(fontSize * lineHeight);
return {
listWidth: 180,
listHeight: 200,
listWidthLG: 250,
headerHeight: controlHeightLG,
itemHeight: controlHeight,
itemPaddingBlock: (controlHeight - fontHeight) / 2,
};
},
);

14
docs/react/migrate-less-variables.en-US.md

@ -431,7 +431,19 @@ This document contains the correspondence between all the less variables related
<!-- ### Tooltip -->
<!-- ### Transfer -->
### Transfer
<!-- prettier-ignore -->
| Less variables | Component Token | Note |
| --- | --- | --- |
| `@transfer-header-height` | `headerHeight` | - |
| `@transfer-item-height` | `itemHeight` | - |
| `@transfer-disabled-bg` | `colorBgContainerDisabled` | Global Token |
| `@transfer-list-height` | `listHeight` | - |
| `@transfer-item-hover-bg` | `controlItemBgHover` | Global Token |
| `@transfer-item-selected-hover-bg` | `controlItemBgActiveHover` | Global Token |
| `@transfer-item-padding-vertical` | `itemPaddingBlock` | - |
| `@transfer-list-search-icon-top` | - | Deprecated |
<!-- ### Tree -->

14
docs/react/migrate-less-variables.zh-CN.md

@ -428,7 +428,19 @@ Mentions 提及
<!-- ### Tooltip 文字提示 -->
<!-- ### Transfer 穿梭框 -->
Transfer 穿梭框
<!-- prettier-ignore -->
| Less variables | Component Token | Note |
| --- | --- | --- |
| `@transfer-header-height` | `headerHeight` | - |
| `@transfer-item-height` | `itemHeight` | - |
| `@transfer-disabled-bg` | `colorBgContainerDisabled` | 全局 Token |
| `@transfer-list-height` | `listHeight` | - |
| `@transfer-item-hover-bg` | `controlItemBgHover` | 全局 Token |
| `@transfer-item-selected-hover-bg` | `controlItemBgActiveHover` | 全局 Token |
| `@transfer-item-padding-vertical` | `itemPaddingBlock` | - |
| `@transfer-list-search-icon-top` | - | 已废弃 |
<!-- ### Tree 树形控件 -->

Loading…
Cancel
Save