Skip to content

Commit 2850171

Browse files
committed
feat: code optimization (#14)
* feat: 代码优化 * feat: update FormList * feat: update * feat: FormItem 优化 * feat: 类型优化 * fix: formItemProps
1 parent d8db319 commit 2850171

File tree

5 files changed

+60
-34
lines changed

5 files changed

+60
-34
lines changed

src/components/FormList/index.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Button, Form, FormInstance, FormProps } from "antd";
33

44
import { getFormElement } from "../tools";
55

6-
export interface FormListProps extends Pick<FormProps, "form" | "colon" | "className" | "style" | "layout" | "labelAlign" | "onValuesChange"> {
6+
export interface FormListProps extends Pick<FormProps, "form" | "colon" | "className" | "style" | "layout" | "labelAlign" | "onValuesChange" | "disabled"> {
77
labelCol?: number;
88
wrapperCol?: number;
99

@@ -71,6 +71,7 @@ const FormList = (props: FormListProps) => {
7171
// 该值变化后会触发表单值更新
7272
useEffect(() => {
7373
if (stageValues && realForm) {
74+
realForm.resetFields();
7475
realForm.setFieldsValue(stageValues);
7576
}
7677

@@ -112,9 +113,10 @@ const FormList = (props: FormListProps) => {
112113
rules={[e.rule]}
113114
hidden={!(e.visible ?? true)}
114115
name={e.name ? e.name : undefined}
115-
valuePropName={e.type === "switch" ? "checked" : "value"}
116116
key={typeof e.name === "string" ? e.name || idx : e.name.join("_")}
117+
valuePropName={e.valuePropName ? e.valuePropName : e.type === "switch" ? "checked" : "value"}
117118
initialValue={e.type === "radio" ? e.options?.[0].value : e.type === "switch" ? true : undefined}
119+
{...e.formItemProps}
118120
>
119121
{getFormElement(e.type, e)}
120122
</Form.Item>

src/components/tools.tsx

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,15 @@ const { RangePicker } = DatePicker;
55
export const getFormElement = (type: FormItem["type"], info: FormItem) => {
66
switch (type) {
77
case "radio":
8-
return <Radio.Group options={info.options || []} optionType={info.optionType || "button"} buttonStyle="outline" disabled={info.disable} {...info.otherOptions} />;
8+
return (
9+
<Radio.Group
10+
options={info.options || []}
11+
optionType={info.otherOptions?.optionType || "button"}
12+
buttonStyle="outline"
13+
disabled={info.disable}
14+
{...info.otherOptions}
15+
/>
16+
);
917

1018
case "checkbox":
1119
return <Checkbox.Group style={{ width: "100%" }} options={info.options || []} {...info.otherOptions} />;
@@ -27,7 +35,7 @@ export const getFormElement = (type: FormItem["type"], info: FormItem) => {
2735
<Select
2836
showSearch
2937
allowClear
30-
mode={info.mode}
38+
mode={info.otherOptions?.mode}
3139
placeholder={info.placeholder}
3240
disabled={info.disable}
3341
options={info.options as { value: string }[]}
@@ -40,17 +48,36 @@ export const getFormElement = (type: FormItem["type"], info: FormItem) => {
4048
);
4149

4250
case "rangePick":
43-
return <RangePicker disabled={info.disable} showTime={info.showTime} placeholder={["开始时间", "结束时间"]} {...info.otherOptions} />;
51+
return <RangePicker disabled={info.disable} showTime={info.otherOptions?.showTime} placeholder={["开始时间", "结束时间"]} {...info.otherOptions} />;
4452

4553
case "datePick":
46-
return <DatePicker disabled={info.disable} showTime={info.showTime} {...info.otherOptions} />;
54+
return <DatePicker disabled={info.disable} showTime={info.otherOptions?.showTime} {...info.otherOptions} />;
4755

4856
case "textArea":
4957
return (
50-
<Input.TextArea placeholder={info.placeholder} disabled={info.disable} showCount maxLength={info.maxLength || 500} className="mb-6 h-32" {...info.otherOptions} />
58+
<Input.TextArea
59+
showCount
60+
disabled={info.disable}
61+
placeholder={info.placeholder}
62+
maxLength={info.otherOptions?.maxLength || 500}
63+
classNames={{
64+
textarea: "h-40 resize-none",
65+
}}
66+
{...info.otherOptions}
67+
/>
5168
);
5269

5370
case "treeSelect":
54-
return <TreeSelect allowClear placeholder={info.placeholder} disabled={info.disable} treeDefaultExpandAll className="w-full" {...info.otherOptions} />;
71+
return (
72+
<TreeSelect
73+
allowClear
74+
placeholder={info.placeholder}
75+
treeData={info.options}
76+
disabled={info.disable}
77+
treeDefaultExpandAll
78+
className="w-full"
79+
{...info.otherOptions}
80+
/>
81+
);
5582
}
5683
};

src/formIten.d.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
declare interface FormItem {
2+
name: string | (number | string)[];
3+
label?: string | React.ReactNode;
4+
placeholder?: string;
5+
initialValue?: unknown;
6+
rule?: any;
7+
hide?: boolean;
8+
visible?: boolean;
9+
disable?: boolean;
10+
extra?: string | React.ReactNode;
11+
type: "input" | "numberInput" | "select" | "treeSelect" | "datePick" | "rangePick" | "radio" | "checkbox" | "textArea" | "switch" | "blockNode" | "node";
12+
valuePropName?: string;
13+
formItemProps?: import("antd").FormItemProps;
14+
// 特有属性
15+
rightNode?: React.ReactNode; // node
16+
options?: { label: string | React.ReactNode; value: any }[];
17+
otherOptions?: Record<string, any>; // 组件额外属性
18+
}

src/hooks/useSwrData.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ export function useSwrData<R, P = any>(props: UseSwrDataProps<P, R> & { paging?:
4949
* @param {UseSwrDataProps<P, R>} props - Hook 的属性。
5050
* @param {string | any[]} props.reqKey - SWR 请求的 key,用于缓存。
5151
* @param {(params: P) => Promise<R>} props.req - 用于获取数据的请求函数。
52-
* @param {P} [props.params] - 请求函数的参数。
52+
* @param {P} [props.params] - 请求函数的参数 受控参数
5353
* @param {boolean} [props.ready=true] - 标志请求是否准备好发送。
5454
* @param {boolean} [props.paging=false] - 标志是否启用分页。
55-
* @param {Partial<P>} [props.defaultSearch={}] - 默认的搜索参数。
55+
* @param {Partial<P>} [props.defaultSearch={}] - 默认的搜索参数 初始参数
5656
* @param {PageInfo} [props.defaultPage=defaultPageInfo] - 默认的分页信息。
5757
* @param {SWRConfiguration} [props.swrConfig] - SWR 配置选项。
5858
*

src/index.d.ts

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,38 +23,17 @@ interface IndexRoute {
2323
}
2424
type Route = PathRoute | IndexRoute;
2525
interface PageProps {
26-
children: ReactNode;
26+
children: React.ReactNode;
2727
title: string;
2828
}
2929

3030
interface MenuItem {
3131
key: string | number;
3232
path: string;
33-
label: string | ReactNode;
34-
icon?: ReactNode;
33+
label: string | React.ReactNode;
34+
icon?: React.ReactNode;
3535
children?: MenuItem[];
3636
}
37-
interface FormItem {
38-
name: string | (number | string)[];
39-
label?: string | ReactNode;
40-
placeholder?: string;
41-
initialValue?: unknown;
42-
rule?: any;
43-
hide?: boolean;
44-
visible?: boolean;
45-
disable?: boolean;
46-
extra?: string | ReactNode;
47-
type: "input" | "numberInput" | "select" | "treeSelect" | "datePick" | "rangePick" | "radio" | "checkbox" | "textArea" | "switch" | "blockNode" | "node";
48-
valuePropName?: string;
49-
// 特有属性
50-
rightNode?: ReactNode; // node
51-
mode?: "multiple";
52-
optionType?: "default" | "button";
53-
maxLength?: number;
54-
showTime?: boolean;
55-
options?: { label: string | ReactNode; value: any }[];
56-
otherOptions?: Record<string, any>; // 组件额外属性
57-
}
5837

5938
interface AxiosRes<T> {
6039
code: number;

0 commit comments

Comments
 (0)