sy-form-components 0.2.1 → 0.2.3

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.mjs CHANGED
@@ -3672,7 +3672,12 @@ var CHUNK_UPLOAD_THRESHOLD = 10 * 1024 * 1024;
3672
3672
  var trimTrailingSlash = (value) => String(value || "").replace(/\/$/, "");
3673
3673
  var getDefaultBaseUrl = () => {
3674
3674
  const globalEnv = globalThis.process?.env;
3675
- return trimTrailingSlash(globalEnv?.FORM_API_BASE_URL || globalEnv?.BASE_API_URL || "");
3675
+ const envBaseUrl = globalEnv?.FORM_API_BASE_URL || globalEnv?.BASE_API_URL;
3676
+ if (envBaseUrl) return trimTrailingSlash(envBaseUrl);
3677
+ const browserGlobal = typeof window !== "undefined" ? window : void 0;
3678
+ const windowBaseUrl = browserGlobal?.FORM_API_BASE_URL || browserGlobal?.BASE_API_URL || browserGlobal?.__FORM_API_BASE_URL__ || browserGlobal?.__LOWCODE_API_BASE_URL__;
3679
+ if (windowBaseUrl) return trimTrailingSlash(windowBaseUrl);
3680
+ return typeof window !== "undefined" ? "/service" : "";
3676
3681
  };
3677
3682
  var appendQuery = (url, params) => {
3678
3683
  if (!params) return url;
@@ -4940,6 +4945,75 @@ function useFieldPermission(options) {
4940
4945
 
4941
4946
  // src/hooks/useFormDetail.ts
4942
4947
  import { useState as useState20, useEffect as useEffect27, useCallback as useCallback7, useRef as useRef7 } from "react";
4948
+
4949
+ // src/utils/formInstanceData.ts
4950
+ var FORM_INSTANCE_METADATA_KEYS = /* @__PURE__ */ new Set([
4951
+ "appType",
4952
+ "code",
4953
+ "createdAt",
4954
+ "createdBy",
4955
+ "createdByDepartmentId",
4956
+ "createdByDepartmentName",
4957
+ "createdByName",
4958
+ "creator",
4959
+ "data",
4960
+ "error",
4961
+ "formInstId",
4962
+ "formInstanceId",
4963
+ "formUuid",
4964
+ "instanceTitle",
4965
+ "message",
4966
+ "result",
4967
+ "success",
4968
+ "title",
4969
+ "updatedAt"
4970
+ ]);
4971
+ var isPlainRecord = (value) => Boolean(value) && typeof value === "object" && !Array.isArray(value);
4972
+ var parseJsonRecord = (value) => {
4973
+ if (typeof value !== "string" || value.trim() === "") return null;
4974
+ try {
4975
+ const parsed = JSON.parse(value);
4976
+ return isPlainRecord(parsed) ? parsed : null;
4977
+ } catch {
4978
+ return null;
4979
+ }
4980
+ };
4981
+ var pickKnownFields = (source, fieldIds) => {
4982
+ if (fieldIds?.length) {
4983
+ const values = {};
4984
+ for (const fieldId of fieldIds) {
4985
+ if (Object.prototype.hasOwnProperty.call(source, fieldId)) {
4986
+ values[fieldId] = source[fieldId];
4987
+ }
4988
+ }
4989
+ return values;
4990
+ }
4991
+ return Object.fromEntries(
4992
+ Object.entries(source).filter(([key]) => !FORM_INSTANCE_METADATA_KEYS.has(key))
4993
+ );
4994
+ };
4995
+ var extractFormValues = (formInstance, fieldIds) => {
4996
+ if (!isPlainRecord(formInstance)) return null;
4997
+ const nestedCandidates = [
4998
+ formInstance.data,
4999
+ parseJsonRecord(formInstance.data),
5000
+ formInstance.formData,
5001
+ parseJsonRecord(formInstance.formData),
5002
+ formInstance.values,
5003
+ formInstance.fieldValues,
5004
+ parseJsonRecord(formInstance.formDataJson),
5005
+ parseJsonRecord(formInstance.dataJson)
5006
+ ];
5007
+ for (const candidate of nestedCandidates) {
5008
+ if (isPlainRecord(candidate)) {
5009
+ return pickKnownFields(candidate, fieldIds);
5010
+ }
5011
+ }
5012
+ const flatValues = pickKnownFields(formInstance, fieldIds);
5013
+ return Object.keys(flatValues).length > 0 ? flatValues : null;
5014
+ };
5015
+
5016
+ // src/hooks/useFormDetail.ts
4943
5017
  function buildFieldBehaviors(permissions, mode) {
4944
5018
  const behaviors = {};
4945
5019
  if (!permissions) return behaviors;
@@ -4955,7 +5029,7 @@ function buildFieldBehaviors(permissions, mode) {
4955
5029
  return behaviors;
4956
5030
  }
4957
5031
  function useFormDetail(options) {
4958
- const { formUuid, appType, formInstanceId, onPermissionDenied } = options;
5032
+ const { formUuid, appType, formInstanceId, fieldIds, onPermissionDenied } = options;
4959
5033
  const { api } = useFormContext();
4960
5034
  const request = api.request;
4961
5035
  const [loading, setLoading] = useState20(true);
@@ -4984,7 +5058,7 @@ function useFormDetail(options) {
4984
5058
  }
4985
5059
  setPermissions(permResult);
4986
5060
  setInstanceInfo(formResult);
4987
- setFormData(formResult?.data ?? null);
5061
+ setFormData(extractFormValues(formResult, fieldIds));
4988
5062
  } catch (error) {
4989
5063
  console.error("[useFormDetail] Failed to load data:", error);
4990
5064
  if (mountedRef.current) {
@@ -4997,7 +5071,7 @@ function useFormDetail(options) {
4997
5071
  setLoading(false);
4998
5072
  }
4999
5073
  }
5000
- }, [request, formUuid, appType, formInstanceId, onPermissionDenied]);
5074
+ }, [request, formUuid, appType, formInstanceId, fieldIds, onPermissionDenied]);
5001
5075
  useEffect27(() => {
5002
5076
  loadData();
5003
5077
  }, [loadData]);
@@ -5062,7 +5136,7 @@ function useFormDetail(options) {
5062
5136
  // src/hooks/useProcessDetail.ts
5063
5137
  import { useState as useState21, useEffect as useEffect28, useCallback as useCallback8, useRef as useRef8 } from "react";
5064
5138
  function useProcessDetail(options) {
5065
- const { formUuid, appType, formInstanceId } = options;
5139
+ const { formUuid, appType, formInstanceId, fieldIds } = options;
5066
5140
  const { api } = useFormContext();
5067
5141
  const request = api.request;
5068
5142
  const [loading, setLoading] = useState21(true);
@@ -5110,7 +5184,7 @@ function useProcessDetail(options) {
5110
5184
  setCanWithdraw(approvalResult?.canUndo ?? false);
5111
5185
  setPermissions(permResult);
5112
5186
  setInstanceInfo(formResult);
5113
- setFormData(formResult?.data ?? null);
5187
+ setFormData(extractFormValues(formResult, fieldIds));
5114
5188
  if (permResult?.operations?.includes("VIEW_PROCESS") || permResult?.operations?.length > 0) {
5115
5189
  try {
5116
5190
  const progress = await getProcessProgress(request, formInstanceId);
@@ -5144,7 +5218,7 @@ function useProcessDetail(options) {
5144
5218
  setLoading(false);
5145
5219
  }
5146
5220
  }
5147
- }, [request, formUuid, appType, formInstanceId]);
5221
+ }, [request, formUuid, appType, formInstanceId, fieldIds]);
5148
5222
  useEffect28(() => {
5149
5223
  loadData();
5150
5224
  }, [loadData]);
@@ -5481,6 +5555,22 @@ function useChangeRecords(options) {
5481
5555
 
5482
5556
  // src/hooks/useFormNavigation.ts
5483
5557
  import { useState as useState24, useCallback as useCallback11, useRef as useRef11, useEffect as useEffect31 } from "react";
5558
+ var normalizeBasePath = (basePath) => {
5559
+ const normalized = String(basePath || "").replace(/^\/+|\/+$/g, "");
5560
+ return normalized ? `/${normalized}` : "";
5561
+ };
5562
+ var inferBasePath = (appType) => {
5563
+ if (typeof window === "undefined") return "";
5564
+ const pathname = window.location?.pathname || "";
5565
+ const marker = `/${appType}/`;
5566
+ const markerIndex = pathname.indexOf(marker);
5567
+ if (markerIndex <= 0) return "";
5568
+ return pathname.slice(0, markerIndex);
5569
+ };
5570
+ var buildDetailUrl = (appType, formUuid, formInstId, detailType, basePath) => {
5571
+ const prefix = normalizeBasePath(basePath ?? inferBasePath(appType));
5572
+ return `${prefix}/${appType}/${detailType}/${formUuid}?formInstId=${encodeURIComponent(formInstId)}`;
5573
+ };
5484
5574
  function useFormNavigation(options) {
5485
5575
  const {
5486
5576
  appType,
@@ -5488,6 +5578,7 @@ function useFormNavigation(options) {
5488
5578
  formType = "form",
5489
5579
  mode = "redirect",
5490
5580
  redirectDelay = 3e3,
5581
+ basePath,
5491
5582
  onStay
5492
5583
  } = options;
5493
5584
  const [isRedirecting, setIsRedirecting] = useState24(false);
@@ -5507,15 +5598,21 @@ function useFormNavigation(options) {
5507
5598
  }, []);
5508
5599
  const navigateToDetail = useCallback11(
5509
5600
  (formInstId) => {
5510
- window.location.href = `/${appType}/formDetail/${formUuid}?formInstId=${formInstId}`;
5601
+ window.location.href = buildDetailUrl(appType, formUuid, formInstId, "formDetail", basePath);
5511
5602
  },
5512
- [appType, formUuid]
5603
+ [appType, basePath, formUuid]
5513
5604
  );
5514
5605
  const navigateToProcessDetail = useCallback11(
5515
5606
  (formInstId) => {
5516
- window.location.href = `/${appType}/processDetail/${formUuid}?formInstId=${formInstId}`;
5607
+ window.location.href = buildDetailUrl(
5608
+ appType,
5609
+ formUuid,
5610
+ formInstId,
5611
+ "processDetail",
5612
+ basePath
5613
+ );
5517
5614
  },
5518
- [appType, formUuid]
5615
+ [appType, basePath, formUuid]
5519
5616
  );
5520
5617
  const startRedirectCountdown = useCallback11(
5521
5618
  (targetUrl) => {
@@ -5569,10 +5666,16 @@ function useFormNavigation(options) {
5569
5666
  onStay?.(formInstId);
5570
5667
  return;
5571
5668
  }
5572
- const targetUrl = formType === "process" ? `/${appType}/processDetail/${formUuid}?formInstId=${formInstId}` : `/${appType}/formDetail/${formUuid}?formInstId=${formInstId}`;
5669
+ const targetUrl = buildDetailUrl(
5670
+ appType,
5671
+ formUuid,
5672
+ formInstId,
5673
+ formType === "process" ? "processDetail" : "formDetail",
5674
+ basePath
5675
+ );
5573
5676
  startRedirectCountdown(targetUrl);
5574
5677
  },
5575
- [mode, formType, appType, formUuid, onStay, startRedirectCountdown]
5678
+ [mode, formType, appType, formUuid, basePath, onStay, startRedirectCountdown]
5576
5679
  );
5577
5680
  return {
5578
5681
  navigateToDetail,
@@ -6250,6 +6353,12 @@ import { useState as useState29, useCallback as useCallback13 } from "react";
6250
6353
  import { Button as Button9 } from "antd";
6251
6354
  import { CheckCircleFilled } from "@ant-design/icons";
6252
6355
  import { Fragment as Fragment5, jsx as jsx82, jsxs as jsxs28 } from "react/jsx-runtime";
6356
+ var pickFormInstanceId = (value) => {
6357
+ if (!value) return void 0;
6358
+ if (typeof value === "string") return value;
6359
+ if (typeof value !== "object") return void 0;
6360
+ return value.formInstanceId || value.formInstId || value.instanceId || value.id || pickFormInstanceId(value.result) || pickFormInstanceId(value.data);
6361
+ };
6253
6362
  var SubmitSuccessCard = ({
6254
6363
  info,
6255
6364
  mode,
@@ -6299,7 +6408,7 @@ var InnerFormContent = ({
6299
6408
  renderSuccess,
6300
6409
  onSubmitSuccess
6301
6410
  }) => {
6302
- const { validateAll, getFormData: getFormData2, resetForm } = useFormContext();
6411
+ const { validateAll, getFormData: getFormData2, resetForm, api } = useFormContext();
6303
6412
  const [submitted, setSubmitted] = useState29(false);
6304
6413
  const [successInfo, setSuccessInfo] = useState29(null);
6305
6414
  const [submitting, setSubmitting] = useState29(false);
@@ -6311,7 +6420,8 @@ var InnerFormContent = ({
6311
6420
  appType: config.appType,
6312
6421
  formUuid: config.formUuid,
6313
6422
  formType,
6314
- mode: submitSuccessMode === "continue" ? "stay" : submitSuccessMode
6423
+ mode: submitSuccessMode === "continue" ? "stay" : submitSuccessMode,
6424
+ basePath: config.navigation?.basePath
6315
6425
  });
6316
6426
  const handleSubmit = useCallback13(async () => {
6317
6427
  const valid = await validateAll();
@@ -6326,9 +6436,24 @@ var InnerFormContent = ({
6326
6436
  return;
6327
6437
  }
6328
6438
  }
6329
- const formInstId = `inst_${Date.now()}`;
6439
+ const submitResponse = config.mode === "edit" && config.formInstanceId ? await api.updateFormData({
6440
+ appType: config.appType,
6441
+ formUuid: config.formUuid,
6442
+ formInstId: config.formInstanceId,
6443
+ formInstanceId: config.formInstanceId,
6444
+ updateFormDataJson: JSON.stringify(formData)
6445
+ }) : await api.submitFormData({
6446
+ appType: config.appType,
6447
+ formUuid: config.formUuid,
6448
+ data: formData
6449
+ });
6450
+ const formInstId = pickFormInstanceId(submitResponse) || config.formInstanceId || `inst_${Date.now()}`;
6330
6451
  if (config.submit?.afterSubmit) {
6331
- await config.submit.afterSubmit({ formInstanceId: formInstId, data: formData });
6452
+ await config.submit.afterSubmit({
6453
+ formInstanceId: formInstId,
6454
+ data: formData,
6455
+ response: submitResponse
6456
+ });
6332
6457
  }
6333
6458
  if (enableDraft) {
6334
6459
  clearDraft();
@@ -6350,6 +6475,7 @@ var InnerFormContent = ({
6350
6475
  validateAll,
6351
6476
  getFormData2,
6352
6477
  config,
6478
+ api,
6353
6479
  enableDraft,
6354
6480
  clearDraft,
6355
6481
  onSubmitSuccess,
@@ -6469,7 +6595,7 @@ var FormSubmitTemplate = ({
6469
6595
  };
6470
6596
 
6471
6597
  // src/templates/FormDetailTemplate.tsx
6472
- import { useCallback as useCallback14 } from "react";
6598
+ import { useCallback as useCallback14, useMemo as useMemo8 } from "react";
6473
6599
 
6474
6600
  // src/templates/PageSkeleton.tsx
6475
6601
  import { Skeleton as Skeleton3 } from "antd";
@@ -6549,6 +6675,7 @@ var InnerDetailContent = ({
6549
6675
  onDelete,
6550
6676
  onSave
6551
6677
  }) => {
6678
+ const fieldIds = useMemo8(() => schema.fields.map((field) => field.fieldId), [schema.fields]);
6552
6679
  const {
6553
6680
  loading,
6554
6681
  mode,
@@ -6562,7 +6689,7 @@ var InnerDetailContent = ({
6562
6689
  canEdit,
6563
6690
  canDelete,
6564
6691
  canViewChangeRecords
6565
- } = useFormDetail({ formUuid, appType, formInstanceId });
6692
+ } = useFormDetail({ formUuid, appType, formInstanceId, fieldIds });
6566
6693
  const {
6567
6694
  records,
6568
6695
  loading: recordsLoading,
@@ -6673,7 +6800,7 @@ var FormDetailTemplate = (props) => {
6673
6800
  };
6674
6801
 
6675
6802
  // src/templates/ProcessDetailTemplate.tsx
6676
- import { useCallback as useCallback15, useRef as useRef12 } from "react";
6803
+ import { useCallback as useCallback15, useMemo as useMemo9, useRef as useRef12 } from "react";
6677
6804
  import { Fragment as Fragment7, jsx as jsx85, jsxs as jsxs31 } from "react/jsx-runtime";
6678
6805
  function FormDataBridge({
6679
6806
  formDataRef
@@ -6695,6 +6822,7 @@ var InnerProcessContent = ({
6695
6822
  onActionComplete
6696
6823
  }) => {
6697
6824
  const formDataRef = useRef12(void 0);
6825
+ const fieldIds = useMemo9(() => schema.fields.map((field) => field.fieldId), [schema.fields]);
6698
6826
  const {
6699
6827
  loading,
6700
6828
  processInfo,
@@ -6711,7 +6839,7 @@ var InnerProcessContent = ({
6711
6839
  switchToEdit,
6712
6840
  switchToReadonly,
6713
6841
  refreshProgress
6714
- } = useProcessDetail({ formUuid, appType, formInstanceId });
6842
+ } = useProcessDetail({ formUuid, appType, formInstanceId, fieldIds });
6715
6843
  const { approve, reject, withdraw, save, resubmit } = useApprovalActions({
6716
6844
  formInstanceId,
6717
6845
  formUuid,
@@ -6910,6 +7038,7 @@ export {
6910
7038
  defineFormSchema,
6911
7039
  deleteFormData,
6912
7040
  evaluateEffects,
7041
+ extractFormValues,
6913
7042
  getChangeRecords,
6914
7043
  getFormData,
6915
7044
  getProcessBasic,