Skip to content

Commit 9847ee0

Browse files
authoredMar 15, 2022
feat(Transfer): 增加选项搜索功能 (#667)
1 parent 6223c70 commit 9847ee0

File tree

2 files changed

+87
-12
lines changed

2 files changed

+87
-12
lines changed
 

‎packages/react-transfer/README.md

+55-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ const options = [
7171

7272
return (
7373
<Row style={{ flexDirection:'column' }} >
74-
<span style={{margin:'10px 0px 10px 0px'}}>树形节点</span>
7574
<Transfer
7675
options={options}
7776
value={value}
@@ -86,6 +85,58 @@ const options = [
8685
ReactDOM.render(<Demo />, _mount_);
8786
```
8887

88+
## 搜索选项
89+
90+
<!--rehype:bgWhite=true&codeSandbox=true&codePen=true-->
91+
```jsx
92+
import React from 'react';
93+
import { Transfer } from 'uiw';
94+
95+
function Demo() {
96+
const options = [
97+
{
98+
label: '武汉市',
99+
key: 1,
100+
children: [
101+
{ label: '新洲区', key: 2, disabled: true },
102+
{ label: '武昌区', key: 3 },
103+
{
104+
label: '汉南区',
105+
key: 4,
106+
children: [
107+
{ label: '汉南区1', key: 5 },
108+
{ label: '汉南区2', key: 6 },
109+
{ label: '汉南区3', key: 7 },
110+
]
111+
},
112+
]
113+
}
114+
];
115+
116+
const [value,valueSet] = React.useState([
117+
{ label: '武昌区', key: 3 },
118+
{ label: '汉南区1', key: 5 },
119+
{ label: '汉南区2', key: 6 },
120+
])
121+
122+
return (
123+
<Row style={{ flexDirection:'column' }} >
124+
<Transfer
125+
options={options}
126+
showSearch={true}
127+
placeholder="搜索内容"
128+
value={value}
129+
onChange={(transfer,option)=>{
130+
valueSet(option)
131+
}}
132+
/>
133+
</Row>
134+
);
135+
}
136+
137+
ReactDOM.render(<Demo />, _mount_);
138+
```
139+
89140
## Form中使用
90141

91142
<!--rehype:bgWhite=true&codeSandbox=true&codePen=true-->
@@ -190,3 +241,6 @@ ReactDOM.render(<Demo />, _mount_);
190241
| onChange | 点击穿梭时回调 | (arrow, selectkeys) => void | - |
191242
| value | 指定当前选中的条目 | [{label, key }] | - |
192243
| options | 下拉数据源,可参考Tree下拉数据源 | [{ label, key, children: [{label, key}] }] | - |
244+
| showSearch | 启用搜索 | boolean | false |
245+
| placeholder | 搜索输入框文字 | string | - |
246+
| onSearch | 搜索时回调函数 | (arrow, searchValue) => void | - |

‎packages/react-transfer/src/index.tsx

+32-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { useEffect, useState } from 'react';
22
import { IProps } from '@uiw/utils';
33
import Card from '@uiw/react-card';
44
import Icon from '@uiw/react-icon';
5-
import Input from '@uiw/react-Input';
5+
import Input from '@uiw/react-input';
66
import TreeChecked from '@uiw/react-tree-checked';
77
import { TreeData } from '@uiw/react-tree';
88
import './style/index.less';
@@ -17,7 +17,7 @@ interface TransferProps extends IProps {
1717
bodyStyle?: React.CSSProperties;
1818

1919
onChange?: (transfer: string, selectedAll: Array<TransferOptionType>) => void;
20-
onSearch?: (seachValue: string) => void;
20+
onSearch?: (transfer: string, seachValue: string) => void;
2121
showSearch?: boolean;
2222
value?: Array<TransferOptionType>;
2323
options?: TreeData[];
@@ -44,7 +44,7 @@ function Transfer(props: TransferProps) {
4444
const [selectOption, selectOptionSet] = useState<Map<string | number, string>>(new Map());
4545
const [leftSelectedKeys, leftSelectedKeySet] = useState<Array<string | number | undefined>>([]);
4646
const [rightSelectedKeys, rightSelectedKeySet] = useState<Array<string | number | undefined>>([]);
47-
const [rightOpions, rightOpionSet] = useState<Array<TransferOptionType>>([]);
47+
const [rightOpions, rightOpionSet] = useState<Array<TreeData>>([]);
4848

4949
useEffect(() => {
5050
leftSelectedKeySet([]);
@@ -86,7 +86,7 @@ function Transfer(props: TransferProps) {
8686
const rightTreeOnSelected = (
8787
selectedKeys: Array<string | number | undefined>,
8888
_1: any,
89-
isChecked: boolean,
89+
_2: boolean,
9090
evn: TreeData,
9191
) => {
9292
rightSelectedKeySet(selectedKeys);
@@ -129,18 +129,38 @@ function Transfer(props: TransferProps) {
129129
props.onChange?.(transferType, option);
130130
};
131131

132-
const searchValueLeftChange = (vlaue: string) => {
133-
searchValueLeftSet(vlaue);
132+
const searchValueLeftChange = (searchValue: string) => {
133+
hiddenNode((child: TreeData) => {
134+
let searchIsMatch = !(child.label as string).includes(searchValue.trim());
135+
if (!searchIsMatch) {
136+
const isSekected = rightOpions.find((selected) => selected.key === child.key);
137+
searchIsMatch = !!isSekected;
138+
}
139+
return searchIsMatch;
140+
});
141+
142+
searchValueLeftSet(searchValue);
143+
144+
props.onSearch?.('left', searchValue);
134145
};
135146

136-
const searchValueRightChange = (vlaue: string) => {
137-
searchValueRightSet(vlaue);
147+
const searchValueRightChange = (searchValue: string) => {
148+
searchValueRightSet(searchValue);
149+
150+
rightOpions.forEach((option) => {
151+
const isHide = !(option.label as string).includes(searchValue.trim());
152+
option.hideNode = isHide;
153+
});
154+
console.log('rightOpions', rightOpions);
155+
rightOpionSet(rightOpions);
156+
157+
props.onSearch?.('right', searchValue);
138158
};
139159

140160
return (
141161
<div className={cls} style={{ width: 400, ...style }}>
142162
<Card
143-
bodyStyle={{ padding: '5px 0px 5px 5px' }}
163+
bodyStyle={{ padding: 5 }}
144164
title={`${leftSelectedKeys.length}/${selectedOptions.length}`}
145165
className={`${prefixCls}-card`}
146166
>
@@ -153,6 +173,7 @@ function Transfer(props: TransferProps) {
153173
)}
154174
<div className={`${prefixCls}-cheked-content`}>
155175
<TreeChecked
176+
defaultExpandAll={true}
156177
placeholder={placeholder || '搜索选项'}
157178
data={selectedOptions}
158179
selectedKeys={leftSelectedKeys}
@@ -175,13 +196,13 @@ function Transfer(props: TransferProps) {
175196
/>
176197
</div>
177198
<Card
178-
bodyStyle={{ padding: '5px 0px 5px 0px' }}
199+
bodyStyle={{ padding: 5 }}
179200
className={`${prefixCls}-card`}
180201
title={`${rightSelectedKeys.length}/${rightOpions.length}`}
181202
>
182203
{showSearch && (
183204
<Input
184-
placeholder={placeholder || '搜索选项'}
205+
placeholder={placeholder}
185206
value={searchValueRight}
186207
onChange={(e: React.ChangeEvent<HTMLInputElement>) => searchValueRightChange(e.target.value)}
187208
/>

0 commit comments

Comments
 (0)
Please sign in to comment.