-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: xsahxl <[email protected]>
- Loading branch information
Showing
14 changed files
with
1,098 additions
and
1,115 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 @@ | ||
legacy-peer-deps=true |
This file was deleted.
Oops, something went wrong.
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
Large diffs are not rendered by default.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,66 @@ | ||
--- | ||
title: Mutation | ||
order: 1 | ||
--- | ||
|
||
## 何时使用 | ||
|
||
提交数据时,此组件会抛出 data,loading, error 等信息 | ||
|
||
## 基本使用 | ||
|
||
```tsx | ||
import { Button } from '@alicloud/console-components'; | ||
import '@alicloud/console-components/dist/wind.css'; | ||
import { Mutation, sleep } from '@xsahxl/ui'; | ||
import moment from 'moment'; | ||
|
||
const Demo = () => { | ||
const submit = async () => { | ||
await sleep(1000); | ||
return { time: moment().format('YYYY-MM-DD HH:mm:ss') }; | ||
}; | ||
return ( | ||
<Mutation | ||
onSubmit={submit} | ||
refetchQuery={async () => console.log('refetchQuery')} | ||
> | ||
{(create, { data, loading }) => { | ||
return ( | ||
<> | ||
<Button | ||
loading={loading} | ||
type="primary" | ||
onClick={create} | ||
style={{ marginLeft: 8 }} | ||
> | ||
创建 | ||
</Button> | ||
<pre>{JSON.stringify(data)}</pre> | ||
</> | ||
); | ||
}} | ||
</Mutation> | ||
); | ||
}; | ||
|
||
export default Demo; | ||
``` | ||
|
||
## API | ||
|
||
| 属性 | 说明 | 类型 | 默认值 | | ||
| ------------ | ----------------------------- | ------------------------------------------------------------------------------- | ------ | | ||
| children | 子节点 | (submit: () => Promise<any\>, options: [RenderProps](#renderprops) => ReactNode | - | | ||
| onSubmit | 请求数据的函数 | () => Promise<any\> | - | | ||
| onCompleted | 请求数据完成触发事件 | () => Promise<any\> | - | | ||
| onError | 请求数据错误触发事件 | () => Promise<any\> | - | | ||
| refetchQuery | onSubmit 完成后发起的数据请求 | () => Promise<any\> | - | | ||
|
||
### RenderProps | ||
|
||
| 属性 | 说明 | 类型 | 默认值 | | ||
| ------- | -------------- | ------- | ------ | | ||
| data | 请求数据的结果 | any | - | | ||
| loading | 请求数据的状态 | boolean | false | | ||
| error | 请求数据的错误 | Error | - | |
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,57 @@ | ||
import React, { FC, useState, Fragment, ReactNode } from 'react'; | ||
import { isFunction } from 'lodash'; | ||
|
||
type RenderProps = { | ||
data: any; | ||
loading: boolean; | ||
error?: Error; | ||
} | ||
|
||
type Props = { | ||
onSubmit: () => Promise<any>; | ||
children: (submit: () => Promise<any>, options: RenderProps) => ReactNode; | ||
onCompleted?: () => Promise<any>; | ||
refetchQuery?: () => Promise<any>; | ||
onError?: (err: Error) => Promise<any>; | ||
} | ||
|
||
const Mutation: FC<Props> = (props) => { | ||
const { | ||
onSubmit: onSubmitFromProps, | ||
children, | ||
onCompleted, | ||
onError, | ||
refetchQuery, | ||
} = props; | ||
const [result, setResult] = useState(); | ||
const [error, setError] = useState<Error>(); | ||
const [loading, setLoading] = useState(false); | ||
const onSubmit = async () => { | ||
try { | ||
setLoading(true); | ||
const data = await onSubmitFromProps(); | ||
setLoading(false); | ||
setResult(data); | ||
isFunction(onCompleted) && await onCompleted(); | ||
isFunction(refetchQuery) && await refetchQuery(); | ||
} catch (error) { | ||
const err = error as Error; | ||
isFunction(onCompleted) && await onCompleted(); | ||
isFunction(onError) && await onError(err); | ||
setError(err); | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
return ( | ||
<Fragment> | ||
{children(onSubmit, { | ||
data: result, | ||
loading, | ||
error, | ||
})} | ||
</Fragment> | ||
); | ||
} | ||
|
||
export default Mutation; |
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,93 @@ | ||
--- | ||
title: Query | ||
order: 0 | ||
nav: | ||
title: 组件 | ||
order: 0 | ||
--- | ||
|
||
:::warning | ||
refreshIndex 属性的初始值务必设置为 0,非 0 的 case 下会引起 fetchData 函数请求两次 | ||
::: | ||
|
||
## 何时使用 | ||
|
||
请求数据时候,此组件内置了刷新,轮询等功能,抛出 data,loading, error 等信息 | ||
|
||
## 基本使用 | ||
|
||
```tsx | ||
import { Button, Select, Switch } from '@alicloud/console-components'; | ||
import '@alicloud/console-components/dist/wind.css'; | ||
import { Query, sleep } from '@xsahxl/ui'; | ||
import { useState } from 'react'; | ||
|
||
const Demo = () => { | ||
const fetchData = async () => { | ||
console.log('触发了fetchData方法'); | ||
console.time('time'); | ||
await sleep(3000); | ||
console.timeEnd('time'); | ||
return ['a', 'b']; | ||
}; | ||
const [refreshIndex, setRefreshIndex] = useState(0); | ||
const [loop, setLoop] = useState({ enable: false, time: 3000 }); | ||
const handlePolling = (value) => { | ||
setLoop({ ...loop, enable: value }); | ||
}; | ||
|
||
return ( | ||
<Query fetchData={fetchData} refreshIndex={refreshIndex} loop={loop}> | ||
{({ data, loading }) => { | ||
return ( | ||
<> | ||
<div style={{ marginBottom: 16 }}> | ||
<span>轮询功能:</span> | ||
<Switch style={{ marginLeft: 8 }} onChange={handlePolling} /> | ||
</div> | ||
<Select dataSource={data} /> | ||
<Button | ||
loading={loading} | ||
type="primary" | ||
text | ||
onClick={() => setRefreshIndex(+new Date())} | ||
style={{ marginLeft: 8 }} | ||
> | ||
刷新 | ||
</Button> | ||
</> | ||
); | ||
}} | ||
</Query> | ||
); | ||
}; | ||
|
||
export default Demo; | ||
``` | ||
|
||
## API | ||
|
||
| 属性 | 说明 | 类型 | 默认值 | | ||
| ------------ | -------------------- | ---------------------------------------------------- | ------------------------------ | | ||
| children | 子节点 | (options: [RenderProps](#renderprops) ) => ReactNode | - | | ||
| fetchData | 请求数据的函数 | () => Promise<any\> | - | | ||
| refreshIndex | 刷新数据 | number | - | | ||
| autoFetch | 是否自动获取数据源 | boolean | true | | ||
| loop | 接口是否轮询 | Loop | { enable: false, time: 10000 } | | ||
| onCompleted | 请求数据完成触发事件 | () => Promise<any\> | - | | ||
| onError | 请求数据错误触发事件 | () => Promise<any\> | - | | ||
|
||
### Loop | ||
|
||
| 属性 | 说明 | 类型 | 默认值 | | ||
| ------ | ------------ | ------- | ------ | | ||
| enable | 是否开启轮询 | boolean | false | | ||
| time | 轮询间隔时间 | number | 10000 | | ||
|
||
### RenderProps | ||
|
||
| 属性 | 说明 | 类型 | 默认值 | | ||
| ------- | -------------- | ------- | ------ | | ||
| data | 请求数据的结果 | any | - | | ||
| loading | 请求数据的状态 | boolean | false | | ||
| error | 请求数据的错误 | Error | - | |
Oops, something went wrong.