knt-shared 1.2.2 → 1.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.esm.js CHANGED
@@ -1619,6 +1619,8 @@ function useForm(options = {}) {
1619
1619
  const formModel = reactive({});
1620
1620
  const loadedRef = ref(false);
1621
1621
  const propsRef = ref({});
1622
+ const pendingValues = ref(null);
1623
+ let unwatchFormRef = null;
1622
1624
  const initFormModel = () => {
1623
1625
  var _a, _b;
1624
1626
  const schemas = unref(((_b = (_a = formRef.value) == null ? void 0 : _a.getSchema) == null ? void 0 : _b.call(_a)) || options.schemas || []);
@@ -1630,12 +1632,38 @@ function useForm(options = {}) {
1630
1632
  };
1631
1633
  const register = async (formInstance) => {
1632
1634
  await nextTick();
1635
+ if (unwatchFormRef) {
1636
+ unwatchFormRef();
1637
+ unwatchFormRef = null;
1638
+ }
1633
1639
  formRef.value = formInstance;
1634
1640
  loadedRef.value = true;
1635
1641
  if (options && Object.keys(options).length > 0) {
1636
1642
  await setProps(options);
1637
1643
  }
1638
1644
  initFormModel();
1645
+ if (formInstance.formRef) {
1646
+ unwatchFormRef = watch(
1647
+ () => formInstance.formRef.value,
1648
+ (val) => {
1649
+ if (val === null) {
1650
+ console.log("表单组件已卸载,重置 useForm 状态");
1651
+ formRef.value = null;
1652
+ loadedRef.value = false;
1653
+ if (unwatchFormRef) {
1654
+ unwatchFormRef();
1655
+ unwatchFormRef = null;
1656
+ }
1657
+ }
1658
+ },
1659
+ { flush: "sync" }
1660
+ // 同步执行,确保及时清理
1661
+ );
1662
+ }
1663
+ if (pendingValues.value) {
1664
+ await setFieldsValue(pendingValues.value);
1665
+ pendingValues.value = null;
1666
+ }
1639
1667
  };
1640
1668
  const getForm = () => {
1641
1669
  const form = unref(formRef);
@@ -1647,7 +1675,16 @@ function useForm(options = {}) {
1647
1675
  return form;
1648
1676
  };
1649
1677
  const getFormSafe = () => {
1650
- return unref(formRef);
1678
+ var _a;
1679
+ const form = unref(formRef);
1680
+ if (!form) return null;
1681
+ if (((_a = form.formRef) == null ? void 0 : _a.value) === null) {
1682
+ console.log("检测到失效的表单实例,清理引用");
1683
+ formRef.value = null;
1684
+ loadedRef.value = false;
1685
+ return null;
1686
+ }
1687
+ return form;
1651
1688
  };
1652
1689
  const getFieldsValue = () => {
1653
1690
  const form = getFormSafe();
@@ -1659,7 +1696,10 @@ function useForm(options = {}) {
1659
1696
  };
1660
1697
  const setFieldsValue = async (values) => {
1661
1698
  const form = getFormSafe();
1662
- if (!form) return;
1699
+ if (!form) {
1700
+ pendingValues.value = { ...pendingValues.value, ...values };
1701
+ return;
1702
+ }
1663
1703
  if (form.setFieldsValue) {
1664
1704
  await form.setFieldsValue(values);
1665
1705
  } else {
@@ -3731,9 +3771,7 @@ function useModal(props) {
3731
3771
  const openModal = (visible = true, data) => {
3732
3772
  const modal = getModal();
3733
3773
  if (!modal) return;
3734
- if (data !== void 0) {
3735
- modal.setModalProps({ _data: data });
3736
- }
3774
+ modal.setModalProps({ _data: data });
3737
3775
  if (visible) {
3738
3776
  modal.openModal();
3739
3777
  } else {
@@ -3744,6 +3782,9 @@ function useModal(props) {
3744
3782
  const modal = getModal();
3745
3783
  if (!modal) return;
3746
3784
  modal.closeModal();
3785
+ setTimeout(() => {
3786
+ modal.setModalProps({ _data: void 0 });
3787
+ });
3747
3788
  };
3748
3789
  const setModalProps = (modalProps) => {
3749
3790
  const modal = getModal();