Skip to content

Commit c0e8dc7

Browse files
authored
Merge pull request #222 from yaob421123/master
fix(ProTable): 接口请求swr替换@kkt/request
2 parents ae7534e + 13f2f18 commit c0e8dc7

File tree

7 files changed

+103
-96
lines changed

7 files changed

+103
-96
lines changed

packages/components/src/ProForm/formdom.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ function FormDom({
3030
const store = useStore();
3131
const colProps = useColPropsContext();
3232

33-
console.log(8888, formType);
34-
3533
const { setFormState } = store as {
3634
setFormState: ((p: any) => void) | undefined;
3735
};

packages/components/src/ProTable/BaseTable.tsx

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ import React, {
55
useRef,
66
useMemo,
77
} from 'react';
8-
import useSWR from 'swr';
98
import { Table, Pagination, Checkbox, Radio, Empty } from 'uiw';
10-
import { request } from '@uiw-admin/utils';
119
import { useStore } from './hooks';
1210
import { Fields, BaseTableProps, FormCol } from './types';
1311
import useSelections from './useSelections';
12+
import { useReactMutation, fetchFn } from '@kkt/request';
1413

1514
const BaseTable: React.FC<BaseTableProps> = ({
1615
style,
@@ -42,8 +41,8 @@ const BaseTable: React.FC<BaseTableProps> = ({
4241
query,
4342
key,
4443
searchValues,
45-
SWRConfiguration = {},
4644
requestOptions,
45+
mutationOptions,
4746
} = store as any;
4847

4948
const { selectKey, type = 'checkbox', defaultSelected = [] } = rowSelection;
@@ -83,11 +82,8 @@ const BaseTable: React.FC<BaseTableProps> = ({
8382
// 格式化接口查询参数
8483
const formatQuery = () => {
8584
if (query) {
86-
return query(
87-
pageIndex,
88-
pgSize,
89-
isFirstMountRef.current === false ? defaultValues : searchValues,
90-
);
85+
const params = { ...defaultValues, ...searchValues };
86+
return query(pageIndex, pgSize, params);
9187
} else {
9288
// 默认传参
9389
return {
@@ -98,21 +94,30 @@ const BaseTable: React.FC<BaseTableProps> = ({
9894
};
9995

10096
const pageSize = formatQuery().pageSize || 10;
101-
// 调接口
10297

103-
const { data, isValidating, mutate } = useSWR(
104-
[key, { method: 'POST', body: formatQuery(), ...requestOptions }],
105-
request,
106-
{
107-
revalidateOnFocus: false,
108-
revalidateOnMount: false,
109-
...SWRConfiguration,
98+
// 调接口
99+
const { mutate, data, isLoading }: any = useReactMutation({
100+
mutationFn: async () => {
101+
let url = key;
102+
const query = formatQuery();
103+
const params = {
104+
method: 'POST',
105+
body: JSON.stringify(query),
106+
...requestOptions,
107+
};
108+
if (['get', 'GET'].includes(params.method)) {
109+
const searchParams = new URLSearchParams(query);
110+
url += '?' + searchParams.toString();
111+
delete params.body;
112+
}
113+
return fetchFn(url, params);
110114
},
111-
);
115+
...mutationOptions,
116+
});
112117

113118
useEffect(() => {
114119
// 第一次加载
115-
mutate(false);
120+
mutate();
116121
}, [mutate]);
117122

118123
// table数据
@@ -165,7 +170,7 @@ const BaseTable: React.FC<BaseTableProps> = ({
165170
const stores: any = {
166171
data: tableData,
167172
total,
168-
loading: isValidating,
173+
loading: isLoading,
169174
// onSearch,
170175
selection,
171176
pageIndex,
@@ -186,7 +191,7 @@ const BaseTable: React.FC<BaseTableProps> = ({
186191
}
187192
}, [
188193
JSON.stringify(tableData),
189-
isValidating,
194+
isLoading,
190195
JSON.stringify(columns),
191196
pageIndex,
192197
JSON.stringify(selection),

packages/components/src/ProTable/README.md

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -643,29 +643,29 @@ export default Demo6
643643

644644
## Props
645645

646-
| 参数 | 说明 | 类型 | 默认值 |
647-
| -------------- | ------------------------------------------------------ | ----------------------------------------------- | ------ |
648-
| columns |`uiw table` columns用法一致 必传, 如果需要表单,也在此增加`props` | FormCol[] | [] |
646+
| 参数 | 说明 | 类型 | 默认值 |
647+
| ---- | ---- | ---- | ---- |
648+
| columns |`uiw table` columns用法一致 必传, 如果需要表单,也在此增加`props` | `FormCol[]` | [] |
649649
| operateButtons | 操作栏按钮集合,属性与uiw button一致并支持自定义render | `Array<ButtonProps & { render?: JSX.Element }>` | [] |
650-
| searchBtns | 搜索栏按钮集合,属性与uiw button一致并支持自定义render | `Array<ButtonProps & { render?: JSX.Element }>` | [] |
651-
| table | useTable返回值 | Object 必传 | |
652-
| onPageChange | 分页回调 |page: number => void | - |
653-
| onBeforeSearch | 查询table前表单回调,可用于表单验证,返回true 继续查询 | ({initial, current}) => Boolean | |
654-
| rowSelection | 选择框配置 | RowSelection | - |
655-
| scroll | 设置横向滚动 | ScrollProps | - |
656-
| paginationProps| 分页属性 | 继承自[uiw Pagination](https://uiwjs.github.io/#/components/pagination) | - |
657-
| formCol | 网格中表单一行列数 | number | 5 |
658-
| tableBackgroundColor | 网格中表格的背景色 | React.CSSProperties['backgroundColor'] | - |
659-
| tableHeadHidden | 网格中表头是否显示 | boolean | false |
650+
| searchBtns | 搜索栏按钮集合,属性与uiw button一致并支持自定义render | `Array<ButtonProps & { render?: JSX.Element }>` | [] |
651+
| table | 必传。useTable返回值 | `object` | - |
652+
| onPageChange | 分页回调 | `(page: number) => void` | - |
653+
| onBeforeSearch | 查询table前表单回调,可用于表单验证,返回true 继续查询 | `({initial, current}) => Boolean` | - |
654+
| rowSelection | 选择框配置 | `RowSelection` | - |
655+
| scroll | 设置横向滚动 | `ScrollProps` | - |
656+
| paginationProps| 分页属性 | 继承自[uiw Pagination](https://uiwjs.github.io/#/components/pagination) | - |
657+
| formCol | 网格中表单一行列数 | `number` | `5`|
658+
| tableBackgroundColor | 网格中表格的背景色 | `React.CSSProperties['backgroundColor']` | - |
659+
| tableHeadHidden | 网格中表头是否显示 | `boolean` | `false` |
660660

661661
更多属性文档请参考 [uiw/Table](https://uiwjs.github.io/#/components/table)
662662

663663
### searchBtns
664664

665665
| 参数 | 说明 | 类型 | 默认值 |
666666
| --------------- | ---------------------------------------- | -------- | ------ |
667-
| label | 按钮标题 | string | - |
668-
| render | 不使用button,自定义组件 | React Component | - |
667+
| label | 按钮标题 | `string` | - |
668+
| render | 不使用button,自定义组件 | `React Component` | - |
669669

670670
更多属性文档请参考 [uiw button](https://uiwjs.github.io/#/components/button)
671671

@@ -677,9 +677,9 @@ export default Demo6
677677

678678
| 参数 | 说明 | 类型 | 默认值 |
679679
| --------------- | ---------------------------------------- | -------- | ------ |
680-
| checkbox | 选择框类型 | checkbox | radio | checkbox |
681-
| selectKey | 选择框的键名,必填,对应的column里的key。 | String | - |
682-
| defaultSelected | 选中默认值 | [] | - |
680+
| checkbox | 选择框类型 | `checkbox \| radio` | `checkbox` |
681+
| selectKey | 选择框的键名,必填,对应的column里的key。 | `string` | - |
682+
| defaultSelected | 选中默认值 | `string[]` | - |
683683

684684
### ScrollProps
685685

@@ -693,13 +693,13 @@ export default Demo6
693693

694694
配置搜索表单
695695

696-
| 参数 | 说明 | 类型 | 默认值 |
697-
| ----------- | ------------------------------------- | --------------------------------------------------------------- | ------ |
698-
| widget | 表单组件 | 支持例子中的组件, 组件名与uiw表单组件名字一致,只是首字母小写了 | - |
699-
| widgetProps | 组件属性 | 与uiw对应的组件属性一致 | - |
700-
| label | 表单标题,如果不填则继承columns title | String | - |
701-
| key | 表单name,如果不填则继承columns key | String | - |
702-
| option | 组件 是`checkbox``select``searchSelect``searchTree` 使用, 数据源统一叫option | Array | - |
696+
| 参数 | 说明 | 类型 | 默认值 |
697+
| --- | --- | --- | --- |
698+
| widget | 表单组件 | 支持例子中的组件, 组件名与uiw表单组件名字一致,只是首字母小写了 | - |
699+
| widgetProps | 组件属性 | 与uiw对应的组件属性一致 | - |
700+
| label | 表单标题,如果不填则继承columns title | `string` | - |
701+
| key | 表单name,如果不填则继承columns key | `string` | - |
702+
| option | 组件 是`checkbox``select``searchSelect``searchTree` 使用, 数据源统一叫option | `array` | - |
703703

704704
当前支持的widget组件有
705705

@@ -722,7 +722,7 @@ dateInputRange
722722
props可以是个对象属性值是以上参数,也可以是个数组方便处理筛选条件大于列表展示的情况
723723

724724
```js
725-
<!-- 对象 -->
725+
// 对象
726726
props: {
727727
widget: 'input',
728728
initialValue: 'zzz',
@@ -732,7 +732,7 @@ props: {
732732
}
733733
}
734734

735-
<!-- 数组 -->
735+
// 数组
736736
props: [
737737
{
738738
widget: 'input',
@@ -751,9 +751,9 @@ props: [
751751

752752
| 参数 | 说明 | 类型 | 默认值 |
753753
| ------- | ------------ | ------ | ------ |
754-
| pageSizeOptions | 指定每页可以显示多少条 | Number[] | {} |
755-
| pageSize | 每页条数 | Number | 10 |
756-
| onShowSizeChange | pageSize 变化的回调 | Function(current, pageSize) | - |
754+
| pageSizeOptions | 指定每页可以显示多少条 | `number[]` | - |
755+
| pageSize | 每页条数 | `number` | `10` |
756+
| onShowSizeChange | pageSize 变化的回调 | `(current, pageSize) => void` | - |
757757

758758
更多属性文档请参考 [uiw Pagination](https://uiwjs.github.io/#/components/pagination)
759759

@@ -763,48 +763,48 @@ props: [
763763

764764
| 参数 | 说明 | 类型 | 默认值 |
765765
| ------- | ------------ | ------ | ------ |
766-
| api | 接口请求地址 | string | - |
767-
| options | 配置集合 | object | {} |
766+
| api | 接口请求地址 | `string` | - |
767+
| options | 配置集合 | `object` | {} |
768768

769769
### options
770770

771-
| 参数 | 说明 | 类型 | 默认值 |
772-
| ---------------- | ----------------------------------------------------------------- | ---------------------------------------------------------------------------------------- | -------------------------- |
773-
| formatData | 格式化接口返回的数据,必须返回{total: 总数, data: 列表数据}的格式 | (data) => {total: 10, data: []} | - |
774-
| query | 格式化请求参数, 会接收到pageIndex 当前页 searchValues 表单数据 | (pageIndex: number, searchValues: any) => {page: pageIndex, pageSize: 10, searchValues} | {} |
775-
| SWRConfiguration | swr配置 | SWRConfiguration | {revalidateOnFocus: false} |
776-
| requestOptions | request参数,继承自[axios config](https://axios-http.com/docs/req_config) | object | {} |
771+
| 参数 | 说明 | 类型 | 默认值 |
772+
| --- | --- | --- | --- |
773+
| formatData | 格式化接口返回的数据,必须返回{total: 总数, data: 列表数据}的格式 | (data) => {total: 10, data: []} | - |
774+
| mutationOptions | [useReactMutation](https://uiwjs.github.io/uiw-admin/#/docs/request)配置 | ReactMutationOptions | - |
775+
| query | 格式化请求参数, 会接收到pageIndex 当前页 searchValues 表单数据 | (pageIndex: number, searchValues: any) => {page: pageIndex, pageSize: 10, searchValues} | {} |
776+
| requestOptions | request参数,继承自`fetch` | object | {} |
777777

778778
### response
779779

780780
| 参数 | 说明 | 类型 | 默认值 |
781781
| ------------ | ------------ | ------------- | ------ |
782-
| data | 接口请求数据 | Array | - |
783-
| total | 数据总数 | Number | - |
784-
| searchValues | 表单值 | Object | - |
785-
| selection | 选择框属性 | UseSelections | - |
786-
| pageIndex | 当前分页 | Number | 1 |
787-
| onRefersh | 刷新分页数据 | () => void | - |
788-
| onReset | 重置表单,查询数据 | () => void | - |
789-
| onSearch | 查询数据 | () => void | - |
790-
| form | 返回搜索表单form实例各种内部函数,可用于主动触发事件, 与[Uiw Form](https://uiwjs.github.io/#/components/form) ref 属性返回的一致 | Ref | - |
782+
| data | 接口请求数据 | `any[]` | - |
783+
| total | 数据总数 | `number` | - |
784+
| searchValues | 表单值 | `object` | - |
785+
| selection | 选择框属性 | `UseSelections` | - |
786+
| pageIndex | 当前分页 | `number` | `1` |
787+
| onRefersh | 刷新分页数据 | `() => void` | - |
788+
| onReset | 重置表单,查询数据 | `() => void` | - |
789+
| onSearch | 查询数据 | `() => void` | - |
790+
| form | 返回搜索表单form实例各种内部函数,可用于主动触发事件, 与[Uiw Form](https://uiwjs.github.io/#/components/form) ref 属性返回的一致 | `Ref` | - |
791791

792792
### selection
793793

794794
| 参数 | 说明 | 类型 | 默认值 |
795795
| ----------------- | ------------------ | --------------------- | ------ |
796-
| selected | 已经选择的元素 | array | - |
797-
| allSelected | 是否全选 | boolean | - |
798-
| noneSelected | 是否一个都没有选择 | boolean | - |
799-
| partiallySelected | 是否半选 | boolean | - |
800-
| isSelected | 是否被选择 | (value: T) => boolean | - |
801-
| setSelected | 设置选择的元素 | (value: T[]) => void | - |
802-
| select | 选择元素 | (value: T) => void | - |
803-
| unSelect | 取消选择元素 | (value: T) => void | - |
804-
| toggle | 反选元素 | (value: T) => void | - |
805-
| selectAll | 选择全部元素 | () => void | - |
806-
| unSelectAll | 取消选择全部元素 | () => void | - |
807-
| toggleAll | 反选全部元素 | () => void | - |
796+
| selected | 已经选择的元素 | `array` | - |
797+
| allSelected | 是否全选 | `boolean` | - |
798+
| noneSelected | 是否一个都没有选择 | `boolean` | - |
799+
| partiallySelected | 是否半选 | boolean | - |
800+
| isSelected | 是否被选择 | `(value: T) => boolean` | - |
801+
| setSelected | 设置选择的元素 | `(value: T[]) => void` | - |
802+
| select | 选择元素 | `(value: T) => void ` | - |
803+
| unSelect | 取消选择元素 | `(value: T) => void` | - |
804+
| toggle | 反选元素 | `(value: T) => void` | - |
805+
| selectAll | 选择全部元素 | `() => void` | - |
806+
| unSelectAll | 取消选择全部元素 | `() => void` | - |
807+
| toggleAll | 反选全部元素 | `() => void` | - |
808808

809809
## 贡献者
810810

packages/components/src/ProTable/index.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Table from './BaseTable';
55
import BaseForm from './BaseForm';
66
import { StoreCtx } from './hooks';
77
import { ProtableProps } from './types';
8+
import { QueryClientProvider, queryClient } from '@kkt/request';
89
import './index.css';
910

1011
export * from './types';
@@ -32,7 +33,6 @@ const ProTabel: React.FC<ProtableProps> = (props) => {
3233
searchValues,
3334
loading,
3435
onSearch,
35-
SWRConfiguration,
3636
selection,
3737
pageIndex,
3838
form,
@@ -52,7 +52,6 @@ const ProTabel: React.FC<ProtableProps> = (props) => {
5252
query,
5353
searchValues,
5454
onSearch,
55-
SWRConfiguration,
5655
selection,
5756
pageIndex,
5857
form,
@@ -122,4 +121,12 @@ const ProTabel: React.FC<ProtableProps> = (props) => {
122121
);
123122
};
124123

125-
export default ProTabel;
124+
const Tables: React.FC<ProtableProps> = (props) => {
125+
return (
126+
<QueryClientProvider client={queryClient}>
127+
<ProTabel {...props} />
128+
</QueryClientProvider>
129+
);
130+
};
131+
132+
export default Tables;

packages/components/src/ProTable/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
PaginationProps,
77
FormRefType,
88
} from 'uiw';
9-
import { SWRConfiguration, MutatorOptions } from 'swr';
9+
import { MutateOptions, ReactMutationOptions } from '@kkt/request';
1010
import { UseSelections } from './useSelections';
1111
import { Options } from '@uiw-admin/utils/src/request';
1212
import { MutableRefObject } from 'react';
@@ -93,7 +93,7 @@ export type Params = {
9393
[key: string]: any;
9494
};
9595
requestOptions?: Options;
96-
SWRConfiguration?: SWRConfiguration;
96+
mutationOptions?: ReactMutationOptions;
9797
};
9898

9999
export interface useTableData extends Params {
@@ -113,7 +113,7 @@ export interface useTableData extends Params {
113113
form: MutableRefObject<FormRefType>;
114114
updateForm: (p: stateParams) => void;
115115
setPageIndex: (p: number) => void;
116-
mutate: (data?: any, opts?: boolean | MutatorOptions | undefined) => void;
116+
mutate: (data?: any, opts?: boolean | MutateOptions | undefined) => void;
117117
}
118118

119119
export type stateParams = {

packages/components/src/ProTable/useTable.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Params, useTableData, stateParams } from './types';
33
import { FormRefType } from 'uiw';
44

55
const useTable = (key: string, params: Params = {}): useTableData => {
6-
const { formatData, query, SWRConfiguration, requestOptions } = params;
6+
const { formatData, query, mutationOptions, requestOptions } = params;
77

88
// 表单组件实例
99
const [form, setForm] = useState<MutableRefObject<FormRefType>>();
@@ -57,7 +57,7 @@ const useTable = (key: string, params: Params = {}): useTableData => {
5757
};
5858
// 刷新当前页数据
5959
const onRefersh = async () => {
60-
state.mutate(false);
60+
state.mutate();
6161
};
6262
// 点击查询
6363
const onSearch = async () => {
@@ -68,11 +68,11 @@ const useTable = (key: string, params: Params = {}): useTableData => {
6868
const isNoError = form.current.getError();
6969
if (Object.keys(isNoError).length === 0) {
7070
await state.setPageIndex(1);
71-
state.mutate(false);
71+
state.mutate();
7272
}
7373
} else {
7474
await state.setPageIndex(1);
75-
state.mutate(false);
75+
state.mutate();
7676
}
7777
};
7878

@@ -87,7 +87,7 @@ const useTable = (key: string, params: Params = {}): useTableData => {
8787
formatData,
8888
query,
8989
updateStore,
90-
SWRConfiguration,
90+
mutationOptions,
9191
form,
9292
requestOptions,
9393
updateForm,

0 commit comments

Comments
 (0)