This repository has been archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: VirtualSelectBox * add test * format code * add test * fix * virtual use pan type
- Loading branch information
1 parent
ef73e0a
commit c1b9940
Showing
20 changed files
with
1,021 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
--- | ||
title: VirtualSelectBox 滚动下拉 | ||
date: 2019-08-28 | ||
author: wangkailang | ||
--- | ||
在大量异步数据中选择需要的数据,支持滚动加载和搜索。 | ||
|
||
## 限制条件 | ||
|
||
异步 `API` 支持获取部分数据(分页),query 的格式如下: | ||
```js isShow | ||
{ | ||
// 取 10 条数据 | ||
limit: 10, | ||
// 从第 20 条数据开始取 | ||
offset: 20 | ||
} | ||
``` | ||
表示从第 20 条数据开始取10 条数据。 | ||
|
||
## 基本用法 | ||
- `fetchData` 异步数据 `API`, `Promise` 返回数据结构没有严格要求,通用模拟结构如下: | ||
```js isShow | ||
const fetchData = () => new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
resolve({ | ||
response: { | ||
resNames: [xxx], | ||
paging: { | ||
totalCount: xxx | ||
} | ||
} | ||
}); | ||
reject({ | ||
error: xxx, | ||
}); | ||
}, time); | ||
}); | ||
``` | ||
- `item` 选中项,允许为空对象 `{}` 或者 `""` | ||
|
||
具体使用: | ||
```js isShow | ||
<VirtualSelectBox | ||
item={item} | ||
fetchData={getDatas} | ||
/> | ||
``` | ||
|
||
## 代码演示 | ||
|
||
### 空数组 | ||
```jsx | ||
() => { | ||
const limit = 30; | ||
const getEmptyDatas = () => new Promise(resolve => { | ||
setTimeout(() => { | ||
resolve({ | ||
response: { | ||
resNames: [], | ||
paging: { | ||
totalCount: 0 | ||
} | ||
} | ||
}); | ||
}, 500); | ||
}); | ||
async function fetchEmptyDatas(isReloading, dQuery = {}) { | ||
const actionResult = await getEmptyDatas(dQuery); | ||
const items = actionResult.response.resNames; | ||
const totalCount = actionResult.response.paging.totalCount; | ||
const query = { | ||
...dQuery, | ||
limit, | ||
offset: 0, | ||
}; | ||
return { | ||
query, | ||
items, | ||
totalCount, | ||
} | ||
} | ||
|
||
return ( | ||
<VirtualSelectBox | ||
item={{}} | ||
fetchData={fetchEmptyDatas} | ||
/> | ||
); | ||
} | ||
``` | ||
### 有数据 | ||
```jsx | ||
() => { | ||
const limit = 30; | ||
const TOTAL = 180; | ||
const getDatas = query => new Promise(resolve => { | ||
setTimeout(() => { | ||
const { limit = 0, offset = 0 } = query; | ||
let rlt = []; | ||
if (offset <= TOTAL) { | ||
const len = Math.min(limit, TOTAL - offset); | ||
for (let i = 0; len - i > 0; i++) { | ||
rlt.push({ id: offset + i, name: `list-${offset + i}` }); | ||
} | ||
} | ||
resolve({ | ||
response: { | ||
resNames: rlt, | ||
paging: { | ||
totalCount: TOTAL | ||
} | ||
} | ||
}); | ||
}, 500); | ||
}); | ||
async function fetchDatas(isReloading, dQuery = {}) { | ||
const actionResult = await getDatas(dQuery); | ||
const items = actionResult.response.resNames; | ||
const totalCount = actionResult.response.paging.totalCount; | ||
const query = { | ||
...dQuery, | ||
limit, | ||
offset: 0, | ||
}; | ||
return { | ||
query, | ||
items, | ||
totalCount, | ||
} | ||
} | ||
const [item, setItem] = React.useState({ id: 1, name: 'list-1' }); | ||
const onSelect = React.useCallback(async (item) => { | ||
setItem(item); | ||
}, [item, setItem]); | ||
|
||
const [clear, setClear] = React.useState(true); | ||
|
||
return ( | ||
<div> | ||
<div> | ||
<Checkbox checked={clear} onChange={() => setClear(!clear)}> | ||
允许清除 | ||
</Checkbox> | ||
</div> | ||
<VirtualSelectBox | ||
onSelect={onSelect} | ||
item={item} | ||
fetchData={fetchDatas} | ||
clear={clear} | ||
/> | ||
</div> | ||
) | ||
} | ||
``` | ||
|
||
## API | ||
```jsx previewOnly | ||
<PropTable of="virtualSelectBox" /> | ||
``` | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
@import '../../style/variables.scss'; | ||
svg { | ||
&.icon { | ||
.icon { | ||
svg { | ||
display: inline-block; | ||
stroke-width: 0; | ||
stroke: currentColor; | ||
fill: currentColor; | ||
} | ||
&.primary { | ||
color: $purple-normal | ||
color: $purple-normal; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.