@openg2p/registry-widgets 0.2.1 → 1.0.1

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.js CHANGED
@@ -3542,41 +3542,22 @@ const SectionRenderer = ({ section, dataSourceRequestHandler: propDataSourceRequ
3542
3542
  }
3543
3543
  return section;
3544
3544
  }, [section, namespace]);
3545
- // Create namespaced schemaData if namespace is provided
3546
- // This ensures widgets can read initial values from schemaData at namespaced paths
3545
+ // Create namespaced schemaData if namespace is provided.
3546
+ // Widgets with namespaced data-paths (e.g. "rv-section-0.a1a4d25a.birth_date")
3547
+ // need a nested object at values[namespace] so getValueByPath can traverse it.
3547
3548
  const namespacedSchemaData = React.useMemo(() => {
3548
3549
  if (!namespace || !currentSchemaData) {
3549
3550
  return schemaData;
3550
3551
  }
3551
- // Create a namespaced version of schemaData by copying values to namespaced paths
3552
- const namespaced = { ...currentSchemaData };
3553
- // Copy all top-level keys to namespaced paths
3554
- Object.keys(currentSchemaData).forEach(key => {
3555
- const namespacedKey = `${namespace}.${key}`;
3556
- if (!(namespacedKey in namespaced)) {
3557
- namespaced[namespacedKey] = currentSchemaData[key];
3558
- }
3559
- });
3560
- // Also handle nested objects - copy nested values to namespaced paths
3561
- const copyNestedValues = (obj, prefix = '') => {
3562
- if (obj && typeof obj === 'object' && !Array.isArray(obj)) {
3563
- Object.keys(obj).forEach(key => {
3564
- const fullPath = prefix ? `${prefix}.${key}` : key;
3565
- const namespacedPath = `${namespace}.${fullPath}`;
3566
- if (typeof obj[key] === 'object' && obj[key] !== null && !Array.isArray(obj[key])) {
3567
- copyNestedValues(obj[key], fullPath);
3568
- // Also set the nested object at the namespaced path
3569
- setValueByPath(namespaced, namespacedPath, obj[key]);
3570
- }
3571
- else {
3572
- setValueByPath(namespaced, namespacedPath, obj[key]);
3573
- }
3574
- });
3575
- }
3576
- };
3577
- copyNestedValues(currentSchemaData);
3578
- return namespaced;
3552
+ return { ...currentSchemaData, [namespace]: currentSchemaData };
3579
3553
  }, [namespace, schemaData, currentSchemaData]);
3554
+ // Populate the store with namespaced schema data so that namespaced widgets
3555
+ // can read their initial values via getValueByPath on the namespaced paths.
3556
+ React.useEffect(() => {
3557
+ if (namespace && namespacedSchemaData) {
3558
+ dispatch(setValues(namespacedSchemaData));
3559
+ }
3560
+ }, [namespace, namespacedSchemaData, dispatch]);
3580
3561
  const crViewData = React.useMemo(() => {
3581
3562
  if (mode !== 'CRView')
3582
3563
  return null;
@@ -3938,6 +3919,8 @@ const SectionRenderer = ({ section, dataSourceRequestHandler: propDataSourceRequ
3938
3919
  const baselineSnapshotRef = React.useRef(null);
3939
3920
  // IntakeForm only: increment when baseline is updated after save - forces badge to update (refs don't trigger re-renders)
3940
3921
  const [intakeFormBaselineTrigger, setIntakeFormBaselineTrigger] = React.useState(0);
3922
+ // IntakeForm only: tracks whether the user has actually saved this section (prevents "Saved" badge on initial load)
3923
+ const [hasBeenSavedByUser, setHasBeenSavedByUser] = React.useState(false);
3941
3924
  // IntakeForm: treat as edit mode for dirty tracking when isDraft. RegistryView: use isEditMode.
3942
3925
  const effectiveEditModeForDirty = mode === 'IntakeForm' ? (isDraft !== false) : isEditMode;
3943
3926
  // Compute isDirty: compare current store state to baseline (only when in edit mode)
@@ -3980,18 +3963,68 @@ const SectionRenderer = ({ section, dataSourceRequestHandler: propDataSourceRequ
3980
3963
  const intakeFormSectionStatus = React.useMemo(() => {
3981
3964
  if (mode !== 'IntakeForm' || isDraft === false)
3982
3965
  return null;
3983
- const hasValue = (v) => v !== undefined && v !== null && (typeof v !== 'string' || v.trim().length > 0);
3984
- const currentSnapshot = buildSectionSnapshot(storeValues, namespace);
3985
- const record = currentSnapshot.records?.[0];
3986
- const hasData = record &&
3987
- typeof record === 'object' &&
3988
- Object.values(record).some((v) => hasValue(v));
3989
3966
  if (isDirty)
3990
3967
  return 'modified';
3991
- if (hasData)
3968
+ if (hasBeenSavedByUser)
3992
3969
  return 'saved';
3993
3970
  return null;
3994
- }, [mode, isDirty, storeValues, namespace, buildSectionSnapshot]);
3971
+ }, [mode, isDirty, hasBeenSavedByUser]);
3972
+ // Revert store values to the original schemaData for this section's widgets.
3973
+ // Used by both handleSave (RegistryView raises a CR, so values should not persist)
3974
+ // and handleCancel.
3975
+ const revertToOriginalValues = React.useCallback(() => {
3976
+ const sectionWidgets = collectWidgets(originalSection.panels);
3977
+ const oldSchemaData = schemaData || contextSchemaData;
3978
+ const currentStoreValues = store.getState().widget.values;
3979
+ let newStoreValues = currentStoreValues;
3980
+ sectionWidgets.forEach(widget => {
3981
+ const originalWidgetId = widget['widget-id'];
3982
+ const namespacedWidgetId = namespace ? `${namespace}__${originalWidgetId}` : originalWidgetId;
3983
+ const widgetId = namespacedWidgetId;
3984
+ const originalDataPath = widget['widget-data-path'];
3985
+ const storeDataPath = namespace && originalDataPath
3986
+ ? (typeof originalDataPath === 'string'
3987
+ ? `${namespace}.${originalDataPath}`
3988
+ : Object.fromEntries(Object.entries(originalDataPath).map(([key, path]) => [key, `${namespace}.${path}`])))
3989
+ : originalDataPath;
3990
+ if (widgetId && originalDataPath) {
3991
+ let oldValue;
3992
+ if (typeof originalDataPath === 'object') {
3993
+ oldValue = {};
3994
+ Object.entries(originalDataPath).forEach(([key, path]) => {
3995
+ if (typeof path === 'string') {
3996
+ oldValue[key] = getValueByPath(oldSchemaData, path);
3997
+ }
3998
+ });
3999
+ }
4000
+ else if (typeof originalDataPath === 'string') {
4001
+ oldValue = getValueByPath(oldSchemaData, originalDataPath);
4002
+ }
4003
+ if (oldValue !== undefined) {
4004
+ newStoreValues = setWidgetValue(newStoreValues, storeDataPath, widgetId, oldValue);
4005
+ // Also revert the widgetId-based entry — useBaseWidget.handleChange
4006
+ // sets values[widgetId] during editing, and useBaseWidget.currentValue
4007
+ // reads values[widgetId] first before falling through to the dataPath.
4008
+ newStoreValues = { ...newStoreValues, [widgetId]: oldValue };
4009
+ }
4010
+ }
4011
+ });
4012
+ if (hasSupportingDocuments) {
4013
+ const originalSupportingDocuments = originalSection['section-supporting-documents'] || [];
4014
+ originalSupportingDocuments.forEach((doc, index) => {
4015
+ const widgetId = `supporting-doc-${sectionId}-${index}`;
4016
+ const originalDataPath = doc['document-data-path'];
4017
+ const storeDataPath = namespace && originalDataPath
4018
+ ? `${namespace}.${originalDataPath}`
4019
+ : originalDataPath;
4020
+ const oldValue = getValueByPath(oldSchemaData, originalDataPath);
4021
+ newStoreValues = setWidgetValue(newStoreValues, storeDataPath, widgetId, oldValue);
4022
+ });
4023
+ }
4024
+ if (newStoreValues !== currentStoreValues) {
4025
+ dispatch(setValues(newStoreValues));
4026
+ }
4027
+ }, [originalSection, schemaData, contextSchemaData, store, namespace, hasSupportingDocuments, sectionId, dispatch]);
3995
4028
  // Handle save button click
3996
4029
  const handleSave = async () => {
3997
4030
  if (!store || !onSectionSave) {
@@ -4041,6 +4074,12 @@ const SectionRenderer = ({ section, dataSourceRequestHandler: propDataSourceRequ
4041
4074
  console.error('Section Changes Save failed', error);
4042
4075
  }
4043
4076
  }
4077
+ // In RegistryView, save raises a CR — the actual data update follows a
4078
+ // separate approval workflow, so revert the displayed values to the
4079
+ // originals so the view doesn't show unapproved edits.
4080
+ if (mode === 'RegistryView') {
4081
+ revertToOriginalValues();
4082
+ }
4044
4083
  setIsEditMode(false);
4045
4084
  onEditModeChange?.(originalSectionId, false);
4046
4085
  };
@@ -4086,6 +4125,7 @@ const SectionRenderer = ({ section, dataSourceRequestHandler: propDataSourceRequ
4086
4125
  if (mode === 'IntakeForm') {
4087
4126
  baselineSnapshotRef.current = buildSectionSnapshot(currentSchemaData, namespace);
4088
4127
  setIntakeFormBaselineTrigger((prev) => prev + 1);
4128
+ setHasBeenSavedByUser(true);
4089
4129
  }
4090
4130
  onSectionDirtyChange?.(sectionId, false);
4091
4131
  }
@@ -4094,64 +4134,7 @@ const SectionRenderer = ({ section, dataSourceRequestHandler: propDataSourceRequ
4094
4134
  }, [store, onSectionSave, onSectionSaveSuccess, sectionIndex, originalSection, schemaData, contextSchemaData, namespace, hasSupportingDocuments, dbSectionId, sectionRegisterId, dispatch, buildSectionSnapshot, sectionId, onSectionDirtyChange, mode, isDraft]);
4095
4135
  // Handle cancel button click
4096
4136
  const handleCancel = () => {
4097
- // Revert values in store to original schema data
4098
- // Use original section (without namespace) for collecting widgets
4099
- const sectionWidgets = collectWidgets(originalSection.panels);
4100
- const oldSchemaData = schemaData || contextSchemaData;
4101
- const currentStoreValues = store.getState().widget.values;
4102
- let newStoreValues = currentStoreValues;
4103
- sectionWidgets.forEach(widget => {
4104
- const originalWidgetId = widget['widget-id'];
4105
- // If namespace was used, we need to use namespaced widget ID and data path
4106
- const namespacedWidgetId = namespace ? `${namespace}__${originalWidgetId}` : originalWidgetId;
4107
- const widgetId = namespacedWidgetId;
4108
- const originalDataPath = widget['widget-data-path'];
4109
- // If namespace was used, data path in store is namespaced, but we read from original schema using original path
4110
- const storeDataPath = namespace && originalDataPath
4111
- ? (typeof originalDataPath === 'string'
4112
- ? `${namespace}.${originalDataPath}`
4113
- : Object.fromEntries(Object.entries(originalDataPath).map(([key, path]) => [key, `${namespace}.${path}`])))
4114
- : originalDataPath;
4115
- if (widgetId && originalDataPath) {
4116
- // Handle multi-path (object) or single path (string)
4117
- // Read from original schema data using original paths
4118
- let oldValue;
4119
- if (typeof originalDataPath === 'object') {
4120
- // Multi-path: get values for each path
4121
- oldValue = {};
4122
- Object.entries(originalDataPath).forEach(([key, path]) => {
4123
- if (typeof path === 'string') {
4124
- oldValue[key] = getValueByPath(oldSchemaData, path);
4125
- }
4126
- });
4127
- }
4128
- else if (typeof originalDataPath === 'string') {
4129
- oldValue = getValueByPath(oldSchemaData, originalDataPath);
4130
- }
4131
- // Set in store using namespaced data path (if namespace was used)
4132
- if (oldValue !== undefined) {
4133
- newStoreValues = setWidgetValue(newStoreValues, storeDataPath, widgetId, oldValue);
4134
- }
4135
- }
4136
- });
4137
- // Also revert supporting documents if any
4138
- if (hasSupportingDocuments) {
4139
- // Use original section's supporting documents to get original data paths
4140
- const originalSupportingDocuments = originalSection['section-supporting-documents'] || [];
4141
- originalSupportingDocuments.forEach((doc, index) => {
4142
- const widgetId = `supporting-doc-${sectionId}-${index}`;
4143
- const originalDataPath = doc['document-data-path'];
4144
- // If namespace was used, data path in store is namespaced
4145
- const storeDataPath = namespace && originalDataPath
4146
- ? `${namespace}.${originalDataPath}`
4147
- : originalDataPath;
4148
- const oldValue = getValueByPath(oldSchemaData, originalDataPath);
4149
- newStoreValues = setWidgetValue(newStoreValues, storeDataPath, widgetId, oldValue);
4150
- });
4151
- }
4152
- if (newStoreValues !== currentStoreValues) {
4153
- dispatch(setValues(newStoreValues));
4154
- }
4137
+ revertToOriginalValues();
4155
4138
  setIsEditMode(false);
4156
4139
  onEditModeChange?.(originalSectionId, false);
4157
4140
  };
@@ -4717,6 +4700,9 @@ const SectionsContainer = ({ sections, dataSourceRequestHandler: propDataSourceR
4717
4700
  }, []);
4718
4701
  const safeSections = sections ?? [];
4719
4702
  const prevSectionsLengthRef = React.useRef(safeSections.length);
4703
+ // Stable ref for namespace so formHandle useMemo doesn't depend on the (possibly inline) function identity
4704
+ const namespaceRef = React.useRef(namespace);
4705
+ namespaceRef.current = namespace;
4720
4706
  // Track dirty (unsaved changes) per section for form handle validation
4721
4707
  const sectionDirtyMapRef = React.useRef({});
4722
4708
  const handleSectionDirtyChange = React.useCallback((sectionId, isDirty) => {
@@ -4762,11 +4748,14 @@ const SectionsContainer = ({ sections, dataSourceRequestHandler: propDataSourceR
4762
4748
  // Form handle for onFormReady - allows host to validate and get all section data from its own Submit button
4763
4749
  const formHandle = React.useMemo(() => {
4764
4750
  const getValues = () => store.getState().widget?.values || {};
4765
- const getNamespace = (section, index) => namespace
4766
- ? typeof namespace === 'string'
4767
- ? namespace
4768
- : namespace(section['section-id'], index)
4769
- : undefined;
4751
+ const getNamespace = (section, index) => {
4752
+ const ns = namespaceRef.current;
4753
+ return ns
4754
+ ? typeof ns === 'string'
4755
+ ? ns
4756
+ : ns(section['section-id'], index)
4757
+ : undefined;
4758
+ };
4770
4759
  const checkNoUnsavedChanges = () => {
4771
4760
  const hasDirty = Object.values(sectionDirtyMapRef.current).some(Boolean);
4772
4761
  if (hasDirty) {
@@ -4816,7 +4805,7 @@ const SectionsContainer = ({ sections, dataSourceRequestHandler: propDataSourceR
4816
4805
  return results;
4817
4806
  },
4818
4807
  };
4819
- }, [store, dispatch, safeSections, namespace]);
4808
+ }, [store, dispatch, safeSections]);
4820
4809
  // Call onFormReady when form is ready (sections loaded)
4821
4810
  React.useEffect(() => {
4822
4811
  if (onFormReady && safeSections.length > 0) {
@@ -9105,16 +9094,6 @@ const HeaderSectionWidget = ({ config }) => {
9105
9094
  const statusColor = statusColors[String(statusValue).toLowerCase()] || '#6B7280';
9106
9095
  // ── Scoped class for CSS isolation ────────────────────────────
9107
9096
  const cls = `header-section-widget-${widgetConfig['widget-id']}`;
9108
- // ── Indicator dot component ───────────────────────────────────
9109
- const Dot = ({ color }) => (jsxRuntimeExports.jsx("span", { style: {
9110
- display: 'inline-block',
9111
- width: 8,
9112
- height: 8,
9113
- borderRadius: '50%',
9114
- backgroundColor: color,
9115
- flexShrink: 0,
9116
- marginTop: 6,
9117
- } }));
9118
9097
  // ── RENDER ────────────────────────────────────────────────────
9119
9098
  return (jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [jsxRuntimeExports.jsx("style", { children: `
9120
9099
  .${cls} {
@@ -9283,7 +9262,7 @@ const HeaderSectionWidget = ({ config }) => {
9283
9262
  .parentElement?.querySelector('.hdr-avatar-placeholder');
9284
9263
  if (placeholder)
9285
9264
  placeholder.style.display = 'flex';
9286
- } })) : null, jsxRuntimeExports.jsx("div", { className: "hdr-avatar-placeholder", style: { display: imageUrl ? 'none' : 'flex' }, children: jsxRuntimeExports.jsx("img", { src: img$f, alt: "Profile Placeholder" }) })] }), jsxRuntimeExports.jsxs("div", { className: "hdr-info", children: [displayName && jsxRuntimeExports.jsx("div", { className: "hdr-name", children: displayName }), jsxRuntimeExports.jsxs("div", { className: "hdr-field-row", children: [jsxRuntimeExports.jsx(Dot, { color: "#9CA3AF" }), jsxRuntimeExports.jsxs("span", { className: "hdr-field-label", children: [getLabel('functionalId'), " :"] }), jsxRuntimeExports.jsx("span", { className: "hdr-field-value", children: functionalId || '-' })] }), jsxRuntimeExports.jsxs("div", { className: "hdr-field-row", children: [jsxRuntimeExports.jsx(Dot, { color: isReadonly ? statusColor : '#F59E0B' }), jsxRuntimeExports.jsx("span", { className: "hdr-field-label", children: getLabel('status') }), isReadonly ? (statusLabel ? (jsxRuntimeExports.jsx("span", { className: "hdr-status-badge", style: { backgroundColor: statusColor }, children: statusLabel })) : (jsxRuntimeExports.jsx("span", { className: "hdr-field-value", children: "-" }))) : (jsxRuntimeExports.jsxs("select", { className: "hdr-select", value: statusValue, onChange: (e) => updateFieldValue('status', e.target.value), children: [jsxRuntimeExports.jsx("option", { value: "", children: getLabel('select') }), statusOptions.map((opt) => (jsxRuntimeExports.jsx("option", { value: opt.value, children: opt.label }, opt.value)))] }))] }), jsxRuntimeExports.jsxs("div", { className: "hdr-field-row", children: [jsxRuntimeExports.jsx(Dot, { color: isReadonly ? '#9CA3AF' : '#F59E0B' }), jsxRuntimeExports.jsxs("span", { className: "hdr-field-label", children: [getLabel('statusReason'), " :"] }), isReadonly ? (jsxRuntimeExports.jsx("span", { className: "hdr-field-value", children: statusReason || '-' })) : (jsxRuntimeExports.jsx("input", { type: "text", className: "hdr-input", value: statusReason, placeholder: getLabel('enterReason'), onChange: (e) => updateFieldValue('statusReason', e.target.value) }))] })] })] }), jsxRuntimeExports.jsxs("div", { className: "hdr-right", children: [jsxRuntimeExports.jsxs("div", { className: "hdr-meta-row", children: [jsxRuntimeExports.jsxs("span", { className: "hdr-meta-label", children: [getLabel('createdBy'), " :"] }), jsxRuntimeExports.jsx("span", { className: "hdr-meta-value", children: createdBy || '-' })] }), jsxRuntimeExports.jsxs("div", { className: "hdr-meta-row", children: [jsxRuntimeExports.jsxs("span", { className: "hdr-meta-label", children: [getLabel('createdAt'), " :"] }), jsxRuntimeExports.jsx("span", { className: "hdr-meta-value", children: createdAt || '-' })] }), jsxRuntimeExports.jsxs("div", { className: "hdr-meta-row", children: [jsxRuntimeExports.jsxs("span", { className: "hdr-meta-label", children: [getLabel('lastApprovedBy'), " :"] }), jsxRuntimeExports.jsx("span", { className: "hdr-meta-value", children: lastApprovedBy || '-' })] }), jsxRuntimeExports.jsxs("div", { className: "hdr-meta-row", children: [jsxRuntimeExports.jsxs("span", { className: "hdr-meta-label", children: [getLabel('lastApprovedAt'), " :"] }), jsxRuntimeExports.jsx("span", { className: "hdr-meta-value", children: lastApprovedAt || '-' })] })] })] })] }));
9265
+ } })) : null, jsxRuntimeExports.jsx("div", { className: "hdr-avatar-placeholder", style: { display: imageUrl ? 'none' : 'flex' }, children: jsxRuntimeExports.jsx("img", { src: img$f, alt: "Profile Placeholder" }) })] }), jsxRuntimeExports.jsxs("div", { className: "hdr-info", children: [displayName && jsxRuntimeExports.jsx("div", { className: "hdr-name", children: displayName }), jsxRuntimeExports.jsxs("div", { className: "hdr-field-row", children: [jsxRuntimeExports.jsxs("span", { className: "hdr-field-label", children: [getLabel('functionalId'), " :"] }), jsxRuntimeExports.jsx("span", { className: "hdr-field-value", children: functionalId || '-' })] }), jsxRuntimeExports.jsxs("div", { className: "hdr-field-row", children: [jsxRuntimeExports.jsx("span", { className: "hdr-field-label", children: getLabel('status') }), isReadonly ? (statusLabel ? (jsxRuntimeExports.jsx("span", { className: "hdr-status-badge", style: { backgroundColor: statusColor }, children: statusLabel })) : (jsxRuntimeExports.jsx("span", { className: "hdr-field-value", children: "-" }))) : (jsxRuntimeExports.jsxs("select", { className: "hdr-select", value: statusValue, onChange: (e) => updateFieldValue('status', e.target.value), children: [jsxRuntimeExports.jsx("option", { value: "", children: getLabel('select') }), statusOptions.map((opt) => (jsxRuntimeExports.jsx("option", { value: opt.value, children: opt.label }, opt.value)))] }))] }), jsxRuntimeExports.jsxs("div", { className: "hdr-field-row", children: [jsxRuntimeExports.jsxs("span", { className: "hdr-field-label", children: [getLabel('statusReason'), " :"] }), isReadonly ? (jsxRuntimeExports.jsx("span", { className: "hdr-field-value", children: statusReason || '-' })) : (jsxRuntimeExports.jsx("input", { type: "text", className: "hdr-input", value: statusReason, placeholder: getLabel('enterReason'), onChange: (e) => updateFieldValue('statusReason', e.target.value) }))] })] })] }), jsxRuntimeExports.jsxs("div", { className: "hdr-right", children: [jsxRuntimeExports.jsxs("div", { className: "hdr-meta-row", children: [jsxRuntimeExports.jsxs("span", { className: "hdr-meta-label", children: [getLabel('createdBy'), " :"] }), jsxRuntimeExports.jsx("span", { className: "hdr-meta-value", children: createdBy || '-' })] }), jsxRuntimeExports.jsxs("div", { className: "hdr-meta-row", children: [jsxRuntimeExports.jsxs("span", { className: "hdr-meta-label", children: [getLabel('createdAt'), " :"] }), jsxRuntimeExports.jsx("span", { className: "hdr-meta-value", children: createdAt || '-' })] }), jsxRuntimeExports.jsxs("div", { className: "hdr-meta-row", children: [jsxRuntimeExports.jsxs("span", { className: "hdr-meta-label", children: [getLabel('lastApprovedBy'), " :"] }), jsxRuntimeExports.jsx("span", { className: "hdr-meta-value", children: lastApprovedBy || '-' })] }), jsxRuntimeExports.jsxs("div", { className: "hdr-meta-row", children: [jsxRuntimeExports.jsxs("span", { className: "hdr-meta-label", children: [getLabel('lastApprovedAt'), " :"] }), jsxRuntimeExports.jsx("span", { className: "hdr-meta-value", children: lastApprovedAt || '-' })] })] })] })] }));
9287
9266
  };
9288
9267
 
9289
9268
  /**