p-pc-ui 1.3.12 → 1.3.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -60,7 +60,7 @@ const btnClick = useDebounceFn(async () => {
60
60
 
61
61
  <a-tooltip color="var(--secondary-color-light)" :overlayInnerStyle="{ color: '#fff' }" :title="tooltip"
62
62
  v-bind="$attrs">
63
- <a-button @click="btnClick" @keydown.enter.prevent="btnClick" :loading="loading || builtInLoading" class="font-semibold"
63
+ <a-button @click="btnClick" :loading="loading || builtInLoading" class="font-semibold"
64
64
  :class="disabled ? 'cursor-not-allowed' : 'hover:opacity-80 active:opacity-100'" :style="{
65
65
  background: disabled ? '#b5b5b5' : bgColor,
66
66
  color: disabled ? '#fff' : color,
@@ -0,0 +1,167 @@
1
+ import type { FormDataItem } from "./index";
2
+ import * as _ from "../../utils/dataUtils";
3
+
4
+ export type FormRenderItem = FormDataItem & {
5
+ rawKey: string;
6
+ rules: any[];
7
+ optionList?: any[];
8
+ };
9
+
10
+ export const toFormKey = (key: string) => key.replaceAll(".", "__");
11
+
12
+ export const toRawKey = (key: string) => key.replaceAll("__", ".");
13
+
14
+ export const createFormRules = (renderItem: FormDataItem) => {
15
+ let { fieldType, fieldRules, label } = renderItem;
16
+ const rules: any[] = [];
17
+ const operateType = ["select", "treeSelect", "date", "datePicker"].includes(renderItem.type) ? "选择" : "输入";
18
+
19
+ for (const fieldRule of fieldRules || []) {
20
+ const { msg, required, min, max, regex } = fieldRule;
21
+
22
+ if (
23
+ (renderItem.type === "uploadOss" || renderItem.type === "uploadTos" || renderItem.type === "upload") &&
24
+ renderItem.fieldType === "string"
25
+ ) {
26
+ fieldType = "array";
27
+ }
28
+
29
+ if (required) {
30
+ rules.push({
31
+ type: renderItem.type === "uploadOss" || renderItem.type === "uploadTos" ? "array" : renderItem.fieldType,
32
+ required: true,
33
+ message: msg || `请${operateType}${label}`,
34
+ trigger: ["blur", "change"],
35
+ });
36
+ }
37
+
38
+ if (min) {
39
+ rules.push({
40
+ type: fieldType,
41
+ min,
42
+ message: msg || `${label}需大于等于${min}${fieldType === "string" ? "个字符" : ""}`,
43
+ trigger: ["blur", "change"],
44
+ });
45
+ }
46
+
47
+ if (max) {
48
+ rules.push({
49
+ type: fieldType,
50
+ max,
51
+ message: `${label}需小于等于${max}${fieldType === "string" ? "个字符" : ""}`,
52
+ trigger: ["blur", "change"],
53
+ });
54
+ }
55
+
56
+ if (regex) {
57
+ rules.push({
58
+ type: fieldType,
59
+ pattern: regex,
60
+ message: msg || `${label}格式错误`,
61
+ trigger: ["blur", "change"],
62
+ });
63
+ }
64
+ }
65
+
66
+ return rules;
67
+ };
68
+
69
+ export const getMapOptionList = (mapInfo: any, mapPath?: string[]) => {
70
+ if (!mapPath) return undefined;
71
+ const mapItem = _.get(mapInfo || {}, mapPath);
72
+ if (!mapItem) return undefined;
73
+
74
+ return Object.keys(mapItem).map((key) => ({
75
+ label: mapItem[key],
76
+ value: key,
77
+ }));
78
+ };
79
+
80
+ export const normalizeFormRenderData = (renderData: FormDataItem[], mapInfo: any = {}) => {
81
+ return renderData.map((item) => {
82
+ const rawKey = item.key;
83
+ const optionList = "optionList" in item && Array.isArray(item.optionList) ? [...item.optionList] : undefined;
84
+ const mapOptionList =
85
+ item.type === "select" || item.type === "treeSelect" || item.type === "radio"
86
+ ? getMapOptionList(mapInfo, item.mapPath)
87
+ : undefined;
88
+
89
+ return {
90
+ ...item,
91
+ rawKey,
92
+ key: toFormKey(rawKey),
93
+ rules: createFormRules(item),
94
+ ...(optionList || mapOptionList ? { optionList: mapOptionList || optionList || [] } : {}),
95
+ } as FormRenderItem;
96
+ });
97
+ };
98
+
99
+ export const getInitialFormValue = (renderItem: FormRenderItem, initFormData: Record<string, any> = {}) => {
100
+ if (renderItem.default !== undefined) {
101
+ return renderItem.default;
102
+ }
103
+
104
+ let initValue = _.get(initFormData, renderItem.rawKey);
105
+
106
+ if (initValue === undefined) {
107
+ switch (renderItem.fieldType) {
108
+ case "array":
109
+ initValue = [];
110
+ break;
111
+ case "string":
112
+ initValue =
113
+ renderItem.type === "uploadOss" || renderItem.type === "uploadTos" || renderItem.type === "upload" ? [] : "";
114
+ break;
115
+ case "number":
116
+ initValue = 0;
117
+ break;
118
+ case "date":
119
+ initValue = "";
120
+ break;
121
+ default:
122
+ initValue = undefined;
123
+ break;
124
+ }
125
+ }
126
+
127
+ if (renderItem.type === "select" || renderItem.type === "treeSelect" || renderItem.type === "radio") {
128
+ if (!Array.isArray(renderItem.optionList)) {
129
+ renderItem.optionList = [];
130
+ }
131
+
132
+ if (renderItem.type === "select" && renderItem.isMultiple && !Array.isArray(initValue)) {
133
+ initValue = [];
134
+ }
135
+
136
+ if ((initValue === undefined || initValue === "") && renderItem.optionList.length > 0) {
137
+ if (!(renderItem.type === "select" && renderItem.isMultiple)) {
138
+ const valueKey = renderItem.fieldNames?.value || (renderItem.type === "treeSelect" ? "id" : "value");
139
+ initValue = renderItem.optionList[0]?.[valueKey];
140
+ }
141
+ }
142
+ } else if (renderItem.type === "uploadOss") {
143
+ const values = Array.isArray(initValue) ? initValue : initValue ? [initValue] : [];
144
+ initValue = values.map((val) => ({
145
+ key: val,
146
+ url: renderItem.baseOssUrl + val,
147
+ }));
148
+ } else if (renderItem.type === "uploadTos") {
149
+ const values = Array.isArray(initValue) ? initValue : initValue ? [initValue] : [];
150
+ initValue = values.map((val) => ({
151
+ key: val,
152
+ url: renderItem.baseTosUrl + val,
153
+ }));
154
+ }
155
+
156
+ return initValue;
157
+ };
158
+
159
+ export const initFormStateFromRenderData = (renderData: FormRenderItem[], initFormData: Record<string, any> = {}) => {
160
+ const formState: Record<string, any> = {};
161
+
162
+ for (const renderItem of renderData) {
163
+ formState[renderItem.key] = getInitialFormValue(renderItem, initFormData);
164
+ }
165
+
166
+ return formState;
167
+ };