seeder-st2110-components 1.0.3 → 1.0.5

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.esm.js CHANGED
@@ -2268,10 +2268,12 @@ const NetworkFieldGroup = _ref => {
2268
2268
  }), mergedFieldConfig.ip.enabled && /*#__PURE__*/jsx(Form.Item, {
2269
2269
  label: mergedFieldConfig.ip.label,
2270
2270
  name: [prefix, index, "ip_address"],
2271
+ initialValue: iface.ip_address,
2271
2272
  children: /*#__PURE__*/jsx(Input, {})
2272
2273
  }), mergedFieldConfig.netmask.enabled && /*#__PURE__*/jsx(Form.Item, {
2273
2274
  label: mergedFieldConfig.netmask.label,
2274
2275
  name: [prefix, index, "netmask"],
2276
+ initialValue: iface.netmask,
2275
2277
  children: /*#__PURE__*/jsx(Input, {})
2276
2278
  }), index < interfaces.length - 1 && /*#__PURE__*/jsx(Divider, {
2277
2279
  style: {
@@ -2303,59 +2305,79 @@ const NetworkSettingsModal = _ref2 => {
2303
2305
  const [st2110Interfaces, setSt2110Interfaces] = useState([]);
2304
2306
  const [lanConfigs, setLanConfigs] = useState([]);
2305
2307
  const [submitLoading, setSubmitLoading] = useState(false);
2306
- useEffect(() => {
2307
- const fetchInitialData = async () => {
2308
- try {
2309
- const promises = [];
2310
- if (sections.includes('LAN')) {
2311
- promises.push(getLanConfig());
2312
- }
2313
- if (sections.includes('QSFP')) {
2314
- promises.push(getSysConfig());
2315
- }
2316
- const results = await Promise.allSettled(promises);
2317
- if (sections.includes('LAN')) {
2318
- const lanResult = results[sections.indexOf('LAN')];
2319
- if (lanResult.status === 'fulfilled') {
2320
- setLanConfigs(lanResult.value);
2321
- }
2308
+ const [isInitialized, setIsInitialized] = useState(false);
2309
+ const fetchData = useCallback(async () => {
2310
+ if (!open) return;
2311
+ try {
2312
+ const promises = [];
2313
+ if (sections.includes('LAN')) {
2314
+ promises.push(getLanConfig());
2315
+ }
2316
+ if (sections.includes('QSFP')) {
2317
+ promises.push(getSysConfig());
2318
+ }
2319
+ const results = await Promise.allSettled(promises);
2320
+ if (sections.includes('LAN')) {
2321
+ const lanResult = results[0];
2322
+ if (lanResult.status === 'fulfilled') {
2323
+ setLanConfigs(lanResult.value || []);
2322
2324
  }
2323
- if (sections.includes('QSFP')) {
2324
- const sysResult = results[sections.indexOf('QSFP')];
2325
- if (sysResult.status === 'fulfilled' && sysResult.value) {
2326
- setSt2110Interfaces(sysResult.value.st2110_interfaces || []);
2327
- }
2325
+ }
2326
+ if (sections.includes('QSFP')) {
2327
+ const sysResult = sections.includes('LAN') ? results[1] : results[0];
2328
+ if (sysResult.status === 'fulfilled' && sysResult.value) {
2329
+ setSt2110Interfaces(sysResult.value.st2110_interfaces || []);
2328
2330
  }
2329
- } catch (error) {
2330
- console.error('Failed to fetch data:', error);
2331
2331
  }
2332
- };
2333
- if (open) {
2334
- fetchInitialData();
2332
+ setIsInitialized(true);
2333
+ } catch (error) {
2334
+ console.error('Failed to fetch data:', error);
2335
2335
  }
2336
2336
  }, [open, getLanConfig, getSysConfig, sections]);
2337
+ useEffect(() => {
2338
+ if (open) {
2339
+ fetchData();
2340
+ }
2341
+ }, [open, fetchData]);
2342
+
2343
+ // 当模态框关闭时重置状态
2344
+ useEffect(() => {
2345
+ if (!open) {
2346
+ setIsInitialized(false);
2347
+ setLanConfigs([]);
2348
+ setSt2110Interfaces([]);
2349
+ form.resetFields();
2350
+ }
2351
+ }, [open, form]);
2337
2352
 
2338
2353
  // 动态初始值配置
2339
2354
  const initialValues = useMemo(() => {
2340
2355
  const values = {};
2341
- if (sections.includes('LAN')) {
2356
+ if (sections.includes('LAN') && lanConfigs.length > 0) {
2342
2357
  values.LAN = lanConfigs.map(config => ({
2343
2358
  connection_id: config.connection_id,
2344
2359
  display_name: config.display_name,
2345
2360
  ip_address: config.ip_address,
2346
2361
  netmask: config.netmask
2347
- })) ?? [];
2362
+ }));
2348
2363
  }
2349
- if (sections.includes('QSFP')) {
2350
- values.QSFP = st2110Interfaces?.map(iface => ({
2364
+ if (sections.includes('QSFP') && st2110Interfaces.length > 0) {
2365
+ values.QSFP = st2110Interfaces.map(iface => ({
2351
2366
  id: iface.id,
2352
2367
  display_name: iface.display_name,
2353
2368
  ip_address: iface.ip_address
2354
- })) ?? [];
2369
+ }));
2355
2370
  }
2356
2371
  return values;
2357
2372
  }, [lanConfigs, st2110Interfaces, sections]);
2358
- const handleSuccess = async function () {
2373
+
2374
+ // 当初始值准备好后设置表单值
2375
+ useEffect(() => {
2376
+ if (isInitialized && Object.keys(initialValues).length > 0) {
2377
+ form.setFieldsValue(initialValues);
2378
+ }
2379
+ }, [initialValues, isInitialized, form]);
2380
+ const handleSuccess = useCallback(async function () {
2359
2381
  let isPopup = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
2360
2382
  let messageText = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'Success';
2361
2383
  message.success(messageText);
@@ -2376,8 +2398,8 @@ const NetworkSettingsModal = _ref2 => {
2376
2398
  }
2377
2399
  }
2378
2400
  onClose();
2379
- };
2380
- const handleSubmit = async () => {
2401
+ }, [message, modal, getSysConfig, restart, onClose]);
2402
+ const handleSubmit = useCallback(async () => {
2381
2403
  setSubmitLoading(true);
2382
2404
  try {
2383
2405
  const values = await form.validateFields();
@@ -2419,7 +2441,7 @@ const NetworkSettingsModal = _ref2 => {
2419
2441
  } finally {
2420
2442
  setSubmitLoading(false);
2421
2443
  }
2422
- };
2444
+ }, [form, sections, lanConfigs, st2110Interfaces, updateLanConfig, updateSysConfig, handleSuccess, message]);
2423
2445
 
2424
2446
  // 合并默认模态框属性和传入的属性
2425
2447
  const mergedModalProps = {
@@ -2443,7 +2465,6 @@ const NetworkSettingsModal = _ref2 => {
2443
2465
  // 合并默认表单属性和传入的属性
2444
2466
  const mergedFormProps = {
2445
2467
  form: form,
2446
- initialValues: initialValues,
2447
2468
  labelCol: {
2448
2469
  span: 6
2449
2470
  },
@@ -2457,15 +2478,15 @@ const NetworkSettingsModal = _ref2 => {
2457
2478
  ...mergedModalProps,
2458
2479
  children: /*#__PURE__*/jsxs(Form, {
2459
2480
  ...mergedFormProps,
2460
- children: [sections.includes('LAN') && initialValues.LAN && initialValues.LAN.length > 0 && /*#__PURE__*/jsxs(Fragment, {
2481
+ children: [sections.includes('LAN') && lanConfigs.length > 0 && /*#__PURE__*/jsxs(Fragment, {
2461
2482
  children: [/*#__PURE__*/jsx(NetworkFieldGroup, {
2462
2483
  prefix: "LAN",
2463
- interfaces: initialValues.LAN,
2484
+ interfaces: lanConfigs,
2464
2485
  fieldConfig: fieldConfig.LAN
2465
- }), sections.includes('QSFP') && /*#__PURE__*/jsx(Divider, {})]
2466
- }), sections.includes('QSFP') && initialValues.QSFP && initialValues.QSFP.length > 0 && /*#__PURE__*/jsx(NetworkFieldGroup, {
2486
+ }), sections.includes('QSFP') && st2110Interfaces.length > 0 && /*#__PURE__*/jsx(Divider, {})]
2487
+ }), sections.includes('QSFP') && st2110Interfaces.length > 0 && /*#__PURE__*/jsx(NetworkFieldGroup, {
2467
2488
  prefix: "QSFP",
2468
- interfaces: initialValues.QSFP,
2489
+ interfaces: st2110Interfaces,
2469
2490
  fieldConfig: fieldConfig.QSFP
2470
2491
  })]
2471
2492
  })