lu-lowcode-package-form 0.10.57 → 0.10.59
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.
- package/dist/index.cjs.js +265 -265
- package/dist/index.es.js +18151 -18151
- package/package.json +1 -1
- package/src/App.jsx +54 -0
- package/src/components/field/select/search-select.jsx +55 -50
package/package.json
CHANGED
package/src/App.jsx
CHANGED
@@ -276,12 +276,66 @@ function App() {
|
|
276
276
|
<Field.Number label="测试规则2" __id="ceshi_rule2" />
|
277
277
|
</Field.Table>
|
278
278
|
</Layout.FormRow>
|
279
|
+
|
280
|
+
<Field.WithSingleSelect
|
281
|
+
isRequired={true} ref={testRef}
|
282
|
+
request={async (params) => {
|
283
|
+
console.log("request params", params);
|
284
|
+
const { page = 1 ,pageSize = 10} = params; // 获取当前页码,默认为1
|
285
|
+
|
286
|
+
|
287
|
+
// 模拟数据集
|
288
|
+
const allData = [
|
289
|
+
{ label: '选项1', value: '1' },
|
290
|
+
{ label: '选项2', value: '2' },
|
291
|
+
{ label: '选项3', value: '3' },
|
292
|
+
{ label: '选项4', value: '4' },
|
293
|
+
{ label: '选项5', value: '5' },
|
294
|
+
{ label: '选项6', value: '6' },
|
295
|
+
{ label: '选项7', value: '7' },
|
296
|
+
{ label: '选项8', value: '8' },
|
297
|
+
{ label: '选项9', value: '9' },
|
298
|
+
{ label: '选项10', value: '10' },
|
299
|
+
{ label: '选项11', value: '11' },
|
300
|
+
{ label: '选项12', value: '12' },
|
301
|
+
{ label: '选项13', value: '13' },
|
302
|
+
{ label: '选项14', value: '14' },
|
303
|
+
{ label: '选项15', value: '15' },
|
304
|
+
{ label: '选项16', value: '16' },
|
305
|
+
{ label: '选项17', value: '17' },
|
306
|
+
{ label: '选项18', value: '18' },
|
307
|
+
{ label: '选项19', value: '19' },
|
308
|
+
{ label: '选项20', value: '20' },
|
309
|
+
{ label: '选项21', value: '21' },
|
310
|
+
{ label: '选项22', value: '22' },
|
311
|
+
{ label: '选项23', value: '23' },
|
312
|
+
{ label: '选项24', value: '24' }
|
313
|
+
];
|
314
|
+
|
315
|
+
// 根据页码和每页数据量计算当前页的数据
|
316
|
+
const startIndex = (page - 1) * pageSize;
|
317
|
+
const endIndex = startIndex + pageSize;
|
318
|
+
let pageData = []
|
319
|
+
if (startIndex < allData.length) {
|
320
|
+
pageData = allData.slice(startIndex, endIndex);
|
321
|
+
}
|
322
|
+
|
323
|
+
let result = { code: 0, data: { list: pageData } };
|
324
|
+
console.log("request result", result)
|
325
|
+
await new Promise(resolve => setTimeout(resolve, 200))
|
326
|
+
return result
|
327
|
+
}}
|
328
|
+
option_label="label"
|
329
|
+
option_value="value"
|
330
|
+
label="测试请求" __id="ceshirequest" />
|
331
|
+
|
279
332
|
<Layout.FormRow layout={'1'}>
|
280
333
|
<Field.Table label="子表格" __id="table" isAllowAdd={false} isAllowCopy={true} >
|
281
334
|
|
282
335
|
<Field.WithSingleSelect
|
283
336
|
isRequired={true} ref={testRef}
|
284
337
|
request={async (params) => {
|
338
|
+
console.log("request params", params)
|
285
339
|
return { code: 0, data: { list: [{ label: '选项1', value: '1', tianchong2: { label: '选项2', value: '2' }, tcinput1: "1111", }, { label: '选项2', value: '2' }, { label: '选项3', value: '3' }] } }
|
286
340
|
}}
|
287
341
|
option_label="label"
|
@@ -1,22 +1,23 @@
|
|
1
1
|
|
2
2
|
|
3
3
|
import { Input, Select as OriginalSelect, Spin } from 'antd';
|
4
|
-
import React, { useEffect, useState, forwardRef, useCallback
|
4
|
+
import React, { useEffect, useState, forwardRef, useCallback, useRef } from 'react';
|
5
5
|
import { BaseWrapper } from "../base"
|
6
6
|
import { debounce, isEqual } from 'lodash';
|
7
7
|
|
8
8
|
|
9
|
-
const SearchSelect = forwardRef(({ addWrapper = true, form, fieldName, fieldsValue, shouldUpdateKey,value, type, defaultValue, onChange, option_label, option_value, option_search, options, request, requestParams, callError, subRequest, sub_option_label = "label", mode = "single", sub_option_value = "id", rightIconRender, rightIcon, rightIconClick,recordFieldsChange, ...props }, ref) => {
|
9
|
+
const SearchSelect = forwardRef(({ addWrapper = true, form, fieldName, fieldsValue, shouldUpdateKey, value, type, defaultValue, onChange, option_label, option_value, option_search, options, request, requestParams, callError, subRequest, sub_option_label = "label", mode = "single", sub_option_value = "id", rightIconRender, rightIcon, rightIconClick, recordFieldsChange, ...props }, ref) => {
|
10
10
|
const [nOptions, setNOptions] = React.useState([])
|
11
11
|
const [fetching, setFetching] = useState(false);
|
12
|
-
|
12
|
+
|
13
13
|
const currentPageRef = useRef(1);
|
14
|
+
const currentPageSize = 10
|
14
15
|
const [isAllLoaded, setIsAllLoaded] = useState(false)
|
15
16
|
useEffect(() => {
|
16
17
|
// console.log("SearchSelect useEffect props", props)
|
17
18
|
if (value) {
|
18
19
|
// console.log(`SearchSelect ${fieldName} value`, value)
|
19
|
-
let item = null
|
20
|
+
let item = null
|
20
21
|
if (Array.isArray(nOptions) && nOptions.length > 0) {
|
21
22
|
item = nOptions.find(item => item.value == value?.value || item.value == value)
|
22
23
|
if (item && !isEqual(item, value)) {
|
@@ -24,9 +25,9 @@ const SearchSelect = forwardRef(({ addWrapper = true, form, fieldName, fieldsVal
|
|
24
25
|
typeof onChange === 'function' && onChange(item)
|
25
26
|
}
|
26
27
|
}
|
27
|
-
|
28
|
+
|
28
29
|
// 没有找到与value相等的选项,说明该项数据可能尚未加载,此时需要重新加载数据
|
29
|
-
if (!item) loadSelectOptions()
|
30
|
+
if (!item) loadSelectOptions()
|
30
31
|
}
|
31
32
|
}, [value])
|
32
33
|
|
@@ -38,7 +39,7 @@ const SearchSelect = forwardRef(({ addWrapper = true, form, fieldName, fieldsVal
|
|
38
39
|
console.log("SearchSelect useEffect shouldUpdateKey", shouldUpdateKey)
|
39
40
|
console.log("SearchSelect useEffect requestParams", requestParams)
|
40
41
|
initData(requestParams)
|
41
|
-
}, [shouldUpdateKey,requestParams])
|
42
|
+
}, [shouldUpdateKey, requestParams])
|
42
43
|
|
43
44
|
// useEffect(() => {
|
44
45
|
// console.log("SearchSelect useEffect requestParams", requestParams)
|
@@ -74,7 +75,7 @@ const SearchSelect = forwardRef(({ addWrapper = true, form, fieldName, fieldsVal
|
|
74
75
|
}
|
75
76
|
}
|
76
77
|
const initData = async (params) => {
|
77
|
-
|
78
|
+
|
78
79
|
let item = null
|
79
80
|
const ruleParams = {}
|
80
81
|
currentPageRef.current = 1
|
@@ -125,38 +126,45 @@ const SearchSelect = forwardRef(({ addWrapper = true, form, fieldName, fieldsVal
|
|
125
126
|
});
|
126
127
|
}, 200), [])
|
127
128
|
|
128
|
-
const
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
callError && typeof callError === 'function' && callError(response.message);
|
137
|
-
}
|
138
|
-
else list = response.data?.list || response.data
|
129
|
+
const fetchList = async (params, ruleParams) => {
|
130
|
+
let list = []
|
131
|
+
if (!(ruleParams && Object.values(ruleParams).some(value => value === "##norequest##"))) {
|
132
|
+
setFetching(true)
|
133
|
+
const response = await request({ ...params, ruleParams }, form, fieldName);
|
134
|
+
setFetching(false)
|
135
|
+
if (response.code > 0) {
|
136
|
+
callError && typeof callError === 'function' && callError(response.message);
|
139
137
|
}
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
138
|
+
else list = response.data?.list || response.data
|
139
|
+
}
|
140
|
+
return list
|
141
|
+
}
|
142
|
+
const handleOptions = async (list) => {
|
143
|
+
if (Array.isArray(list)) {
|
144
|
+
for (let index = 0; index < list.length; index++) {
|
145
|
+
const item = list[index];
|
146
|
+
if (typeof subRequest == "function") {
|
147
|
+
let subList = []
|
148
|
+
const { data } = await subRequest(item)
|
149
|
+
if (data?.list && Array.isArray(data.list)) {
|
150
|
+
subList = data.list.map(subItem => ({ label: `${item[option_label]}-${subItem[sub_option_label]}`, value: `${item[option_value]}-${subItem[sub_option_value]}` }))
|
151
151
|
}
|
152
|
-
list[index] = { ...item, label: item[option_label],
|
152
|
+
list[index] = { ...item, label: item[option_label], title: item[option_label], options: subList }
|
153
153
|
}
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
154
|
+
list[index] = { ...item, label: item[option_label], value: item[option_value] }
|
155
|
+
}
|
156
|
+
if (nOptions.length > 0 && !isEqual(nOptions, list)) {
|
157
|
+
typeof onChange === 'function' && onChange(undefined)
|
158
158
|
}
|
159
|
-
|
159
|
+
}
|
160
|
+
return list
|
161
|
+
}
|
162
|
+
|
163
|
+
const fetchOptions = async (params, ruleParams) => {
|
164
|
+
try {
|
165
|
+
let list = await fetchList(params, ruleParams)
|
166
|
+
list = await handleOptions(list)
|
167
|
+
setNOptions(Array.isArray(list) ? list : [])
|
160
168
|
return list
|
161
169
|
} catch (error) {
|
162
170
|
console.error("fetchOptions", error)
|
@@ -177,21 +185,18 @@ const SearchSelect = forwardRef(({ addWrapper = true, form, fieldName, fieldsVal
|
|
177
185
|
|
178
186
|
const handleScroll = (event) => {
|
179
187
|
const { target } = event;
|
180
|
-
if (target.scrollTop + target.clientHeight >= target.scrollHeight) {
|
188
|
+
if (target.scrollTop + target.clientHeight >= target.scrollHeight - 50) {
|
181
189
|
// 滚动到底部,加载更多数据
|
182
190
|
loadMoreOptions();
|
183
191
|
}
|
184
192
|
};
|
185
|
-
|
193
|
+
|
186
194
|
const loadMoreOptions = async () => {
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
currentPageRef.current = currentPageRef.current + 1;
|
193
|
-
}
|
194
|
-
});
|
195
|
+
const nextPageParams = { ...requestParams, page: currentPageRef.current + 1, pageSize: currentPageSize };
|
196
|
+
let newOptions = await fetchList(nextPageParams, null)
|
197
|
+
if (Array.isArray(newOptions) && newOptions.length > 0) {
|
198
|
+
setNOptions(prevOptions => [...prevOptions, ...newOptions]);
|
199
|
+
currentPageRef.current = currentPageRef.current + 1;
|
195
200
|
}
|
196
201
|
};
|
197
202
|
return addWrapper ? (
|
@@ -207,12 +212,12 @@ const SearchSelect = forwardRef(({ addWrapper = true, form, fieldName, fieldsVal
|
|
207
212
|
onChange={handleChange}
|
208
213
|
style={{ width: '100%', flex: 1 }}
|
209
214
|
options={nOptions}
|
210
|
-
onPopupScroll={typeof request === 'function' ? handleScroll : null}
|
215
|
+
onPopupScroll={typeof request === 'function' ? handleScroll : null}
|
211
216
|
mode={mode} >
|
212
217
|
|
213
218
|
</OriginalSelect>
|
214
219
|
{!props?.disabled && rightIcon}
|
215
|
-
{!props?.disabled && typeof rightIconRender === 'function' && rightIconRender({ value, onChange, form, fieldName,recordFieldsChange })}
|
220
|
+
{!props?.disabled && typeof rightIconRender === 'function' && rightIconRender({ value, onChange, form, fieldName, recordFieldsChange })}
|
216
221
|
</BaseWrapper>
|
217
222
|
) : (<>
|
218
223
|
<OriginalSelect
|
@@ -226,12 +231,12 @@ const SearchSelect = forwardRef(({ addWrapper = true, form, fieldName, fieldsVal
|
|
226
231
|
onChange={handleChange}
|
227
232
|
style={{ width: '100%' }}
|
228
233
|
options={nOptions}
|
229
|
-
onPopupScroll={typeof request === 'function' ? handleScroll : null}
|
234
|
+
onPopupScroll={typeof request === 'function' ? handleScroll : null}
|
230
235
|
mode={mode} >
|
231
236
|
|
232
237
|
</OriginalSelect>
|
233
238
|
{!props?.disabled && rightIcon}
|
234
|
-
{!props?.disabled && typeof rightIconRender === 'function' && rightIconRender({ value, onChange, form, fieldName
|
239
|
+
{!props?.disabled && typeof rightIconRender === 'function' && rightIconRender({ value, onChange, form, fieldName, recordFieldsChange })}
|
235
240
|
</>
|
236
241
|
)
|
237
242
|
|