lu-lowcode-package-form 0.10.57 → 0.10.58

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lu-lowcode-package-form",
3
- "version": "0.10.57",
3
+ "version": "0.10.58",
4
4
  "dependencies": {
5
5
  "@ant-design/icons": "^4.8.1",
6
6
  "@dnd-kit/core": "^6.1.0",
package/src/App.jsx CHANGED
@@ -276,12 +276,65 @@ 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
+ return result
326
+ }}
327
+ option_label="label"
328
+ option_value="value"
329
+ label="测试请求" __id="ceshirequest" />
330
+
279
331
  <Layout.FormRow layout={'1'}>
280
332
  <Field.Table label="子表格" __id="table" isAllowAdd={false} isAllowCopy={true} >
281
333
 
282
334
  <Field.WithSingleSelect
283
335
  isRequired={true} ref={testRef}
284
336
  request={async (params) => {
337
+ console.log("request params", params)
285
338
  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
339
  }}
287
340
  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 ,useRef} from 'react';
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,18 +126,23 @@ const SearchSelect = forwardRef(({ addWrapper = true, form, fieldName, fieldsVal
125
126
  });
126
127
  }, 200), [])
127
128
 
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);
137
+ }
138
+ else list = response.data?.list || response.data
139
+ }
140
+ return list
141
+ }
142
+
128
143
  const fetchOptions = async (params, ruleParams) => {
129
144
  try {
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);
137
- }
138
- else list = response.data?.list || response.data
139
- }
145
+ let list = await fetchList(params, ruleParams)
140
146
 
141
147
  if (Array.isArray(list)) {
142
148
  for (let index = 0; index < list.length; index++) {
@@ -177,21 +183,18 @@ const SearchSelect = forwardRef(({ addWrapper = true, form, fieldName, fieldsVal
177
183
 
178
184
  const handleScroll = (event) => {
179
185
  const { target } = event;
180
- if (target.scrollTop + target.clientHeight >= target.scrollHeight) {
186
+ if (target.scrollTop + target.clientHeight >= target.scrollHeight - 50) {
181
187
  // 滚动到底部,加载更多数据
182
188
  loadMoreOptions();
183
189
  }
184
190
  };
185
-
191
+
186
192
  const loadMoreOptions = async () => {
187
- if (request && option_value) {
188
- const nextPageParams = { ...requestParams, page: currentPageRef.current + 1, pageSize: currentPageSize };
189
- debounceFetchOptions(nextPageParams, null, (newOptions) => {
190
- if (newOptions && newOptions.length > 0) {
191
- setNOptions(prevOptions => [...prevOptions, ...newOptions]);
192
- currentPageRef.current = currentPageRef.current + 1;
193
- }
194
- });
193
+ const nextPageParams = { ...requestParams, page: currentPageRef.current + 1, pageSize: currentPageSize };
194
+ let newOptions = await fetchList(nextPageParams, null)
195
+ if (Array.isArray(newOptions) && newOptions.length > 0) {
196
+ setNOptions(prevOptions => [...prevOptions, ...newOptions]);
197
+ currentPageRef.current = currentPageRef.current + 1;
195
198
  }
196
199
  };
197
200
  return addWrapper ? (
@@ -207,12 +210,12 @@ const SearchSelect = forwardRef(({ addWrapper = true, form, fieldName, fieldsVal
207
210
  onChange={handleChange}
208
211
  style={{ width: '100%', flex: 1 }}
209
212
  options={nOptions}
210
- onPopupScroll={typeof request === 'function' ? handleScroll : null}
213
+ onPopupScroll={typeof request === 'function' ? handleScroll : null}
211
214
  mode={mode} >
212
215
 
213
216
  </OriginalSelect>
214
217
  {!props?.disabled && rightIcon}
215
- {!props?.disabled && typeof rightIconRender === 'function' && rightIconRender({ value, onChange, form, fieldName,recordFieldsChange })}
218
+ {!props?.disabled && typeof rightIconRender === 'function' && rightIconRender({ value, onChange, form, fieldName, recordFieldsChange })}
216
219
  </BaseWrapper>
217
220
  ) : (<>
218
221
  <OriginalSelect
@@ -226,12 +229,12 @@ const SearchSelect = forwardRef(({ addWrapper = true, form, fieldName, fieldsVal
226
229
  onChange={handleChange}
227
230
  style={{ width: '100%' }}
228
231
  options={nOptions}
229
- onPopupScroll={typeof request === 'function' ? handleScroll : null}
232
+ onPopupScroll={typeof request === 'function' ? handleScroll : null}
230
233
  mode={mode} >
231
234
 
232
235
  </OriginalSelect>
233
236
  {!props?.disabled && rightIcon}
234
- {!props?.disabled && typeof rightIconRender === 'function' && rightIconRender({ value, onChange, form, fieldName ,recordFieldsChange})}
237
+ {!props?.disabled && typeof rightIconRender === 'function' && rightIconRender({ value, onChange, form, fieldName, recordFieldsChange })}
235
238
  </>
236
239
  )
237
240