SelectInput.jsx
8.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import React, { useState, useRef, useEffect, useCallback } from "react";
import { Input, Popup, Button, Toast, PickerView, SearchBar, Checkbox } from "antd-mobile";
import { DownOutline } from "antd-mobile-icons";
import commonConfig from "@/utils/config";
import * as commonServices from "@/services/services";
import * as commonFunc from "@/components/Common/commonFunc";
import * as commonUtils from "@/utils/utils";
import styles from "./selectInput.less";
import debounce from "lodash/debounce";
const SelectInput = props => {
const [value, setValue] = useState(props.sValue || ""); // 如果存在 name 属性,将其值赋值给 value
const [values, setValues] = useState("");
const [searchValue, setSearchValue] = useState("");
const [visible, setVisible] = useState(false);
const { sId } = props.itemDetail || {};
const { downIndex, tableName, viewRow, itemDetail, sModelsId, sValue, name, id, No, quotationData, showDropDown } = props || {};
const { masterData } = props.state;
const [bCanInput, setBCanInput] = useState(props.bCanInput);
const { bReadonly } = itemDetail || {};
const [propsType, setPropsType] = useState("");
useEffect(() => {
if (!itemDetail) return;
const type = itemDetail.sName.charAt(0);
setPropsType(type);
const viewRowNew = itemDetail.sName ? viewRow?.[itemDetail.sName] : "";
if (itemDetail.sDropDownType === "const") {
return;
}
if (tableName.includes("slaveDown")) {
if (!viewRow.sBackProcessData) return;
// const data = viewRow.sBackProcessData[downIndex][itemDetail.sName];
// setValue(data);
} else if (tableName.includes("sBackendParams")) {
const data = props.sBackProcessData.find(item => item[props.sParentFieldsName]).sBackendParams.find(item => item.sParam === itemDetail.sParam);
if (data) {
const values = data.label ? data.label : data.value;
setValue(values || "");
}
} else {
setValue(viewRowNew || ""); // 动态更新 value
}
// 处理默认数据
console.log("🚀 ~ useEffect ~ itemDetail:", itemDetail)
}, [itemDetail, itemDetail?.sName, viewRow]);
const [columns, setColumns] = useState([]);
const [coplyColumns, setCopyColumns] = useState(columns);
const getSqlDropDownData = async (searchValue, sSqlCondition) => {
const url = `${commonConfig.server_host}business/getSelectLimit/${sId}?sModelsId=${sModelsId}&sName=${""}`;
const body = {
sSqlCondition: sSqlCondition,
sKeyUpFilterName: searchValue,
pageNum: 1,
pageSize: 20,
};
commonServices.postValueService(props.app.token, body, url).then(res => {
if (res.data.code === 1 && res.data.dataset) {
const list = res.data.dataset.rows?.map((item, i) => ({
label: item.sCustomerName || item.sName || item.sProcessName || item.sColorName || item.sMaterialsName,
value: item.sId || item.sSlaveId,
...item,
}));
setColumns(list);
setCopyColumns(res.data.dataset.rows);
}
});
};
const handleConfirm = () => {
if (!values[0]) {
setVisible(false);
return;
}
// 判断是否是
const index = columns.findIndex(item => item.value === values[0]);
const data = coplyColumns[index];
data[itemDetail.sName] = data[itemDetail.sName] ? data[itemDetail.sName] : data.label || data.sName || data.sProcessName || data.sColorName;
setValue(columns[index]?.label || "");
props.onDataChange(tableName, itemDetail.sName, data, data.sId, coplyColumns);
setVisible(false);
};
const handleCancel = () => {
setVisible(false);
};
const searchData = async val => {
if (val === "" || val === null || val === undefined) {
setSearchValue("");
} else {
setSearchValue(val);
}
getSqlDropDownData(val);
};
// 处理选择器变化
const debouncedChangeOption = useCallback(
debounce(val => {
setValues(val);
}, 300), // 防抖时间设置为 300ms
[columns]
);
const changeOption = val => {
debouncedChangeOption(val);
};
useEffect(() => {
if (!visible) return;
let { sDropDownType, sVisColumnName } = itemDetail;
if (sVisColumnName && sVisColumnName === "sProcessName") {
if (sDropDownType === "sql") {
if (itemDetail.sParamDropDown) {
const { sParamDropDown = "", sName0, sName } = itemDetail;
const dropDownData = sParamDropDown.split(",");
if (dropDownData?.length) {
const list = {
dropDownData: dropDownData.map(item => ({
sId: commonUtils.createSid(),
value: commonUtils.createSid(),
sParam: itemDetail.sParam,
[sName]: item,
sParamKey: sName0,
sParamKeyNew: sName,
label: item,
})),
totalPageCount: 1,
currentPageNo: 1,
conditonValues: {},
};
setColumns(list.dropDownData);
setCopyColumns(list.dropDownData);
}
} else {
const list = JSON.parse(itemDetail.sChineseDropDown).map(x => {
return {
label: x.sProcessName,
value: x.sId,
...x,
};
});
setColumns(list);
setCopyColumns(list);
}
// getSqlDropDownData(searchValue);
} else if ((sDropDownType = "const")) {
if (!itemDetail.showDropDown) return;
const list = Object.entries(JSON.parse(itemDetail.showDropDown)).map(([key, value]) => ({
label: value,
value: key,
}));
setColumns(list);
setCopyColumns(list);
}
} else {
if (sDropDownType === "sql") {
const { sSqlCondition } = itemDetail;
if (sSqlCondition) {
const { viewRow } = props;
const keys = sSqlCondition.split(",").map(sSqlCondition => sSqlCondition.split(".")[1]);
// 构造 JSON 对象
const result = keys.reduce((acc, key) => {
acc[key] = viewRow[key];
return acc;
}, {});
getSqlDropDownData(searchValue, result);
} else {
getSqlDropDownData(searchValue);
}
} else if ((sDropDownType = "const")) {
if (!itemDetail.showDropDown) return;
const list = Object.entries(JSON.parse(itemDetail.showDropDown)).map(([key, value]) => ({
label: value,
value: key,
}));
setColumns(list);
setCopyColumns(list);
}
}
}, [visible]);
useCallback;
useEffect(() => {
if (!sValue) return;
setValue(sValue);
}, [sValue]);
useCallback;
const clickBtn = () => {
setVisible(true);
};
return (
<div>
<div className={styles.inputBox}>
{propsType === "b" ? (
<Checkbox
style={{ height: "3.125rem" }}
onChange={val => {
props.onDataChange(tableName, itemDetail.sName, { [itemDetail.sName]: val, label: val }, null, coplyColumns);
}}
/>
) : (
<div>
{" "}
<Input
placeholder=""
value={value}
onChange={val => {
setValue(val);
props.onDataChange(tableName, itemDetail.sName, { [itemDetail.sName]: val }, null, coplyColumns);
}}
onClick={() => {
if (!props.onCostomClick) return;
props.onCostomClick(itemDetail);
}}
readOnly={bReadonly}
/>
{itemDetail.sDropDownType === "sql" || itemDetail.sDropDownType === "const" ? (
<div className={styles.icons} onClick={clickBtn}>
<DownOutline />
</div>
) : null}
</div>
)}
</div>
<Popup visible={visible} onMaskClick={() => setVisible(false)} onClose={() => setVisible(false)} bodyStyle={{ height: "50vh" }}>
<div className={styles.popupHeader}>
<Button onClick={handleCancel} color="primary" fill="none">
取消
</Button>
<Button onClick={handleConfirm} color="primary" fill="none">
确认
</Button>
</div>
<div style={{ padding: "0 1.5rem", marginTop: "1rem" }}>
<SearchBar placeholder="请输入内容" showCancelButton value={searchValue} onChange={val => searchData(val)} />
</div>
<PickerView columns={[columns]} value={values} onChange={changeOption} style={{ "--height": "200px", "--item-height": "2.8rem" }} />
</Popup>
</div>
);
};
export default SelectInput;