fly-el-form 0.0.30 → 0.0.32

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.
@@ -1770,6 +1770,11 @@ function useFormValues() {
1770
1770
  const FlyElForm = defineComponent({
1771
1771
  name: "FlyElForm",
1772
1772
  props: {
1773
+ // 新增:支持 v-model
1774
+ modelValue: {
1775
+ type: Object,
1776
+ default: () => ({})
1777
+ },
1773
1778
  // 模式 默认form表单模式 search为搜索条模式
1774
1779
  model: {
1775
1780
  type: String,
@@ -1876,6 +1881,7 @@ const FlyElForm = defineComponent({
1876
1881
  default: false
1877
1882
  }
1878
1883
  },
1884
+ emits: ["update:modelValue", "submit", "reset"],
1879
1885
  setup(props, context) {
1880
1886
  const isFirstInit = ref(true);
1881
1887
  const formContent = ref({});
@@ -1937,7 +1943,6 @@ const FlyElForm = defineComponent({
1937
1943
  for (const key in newRules) {
1938
1944
  if (hasOwnPropertySafely(newRules, key)) {
1939
1945
  if (!hasOwnPropertySafely(rules.value, key) || !isEqual(rules.value[key], newRules[key])) {
1940
- console.log(key, newRules[key]);
1941
1946
  rules.value[key] = newRules[key];
1942
1947
  }
1943
1948
  }
@@ -1956,41 +1961,61 @@ const FlyElForm = defineComponent({
1956
1961
  }
1957
1962
  }
1958
1963
  }
1959
- const newFormKeyAndName = res.formKeyAndName || {};
1960
- const oldFormKeyAndNameKeys = Object.keys(formKeyAndName);
1961
- oldFormKeyAndNameKeys.forEach((key) => {
1962
- if (!hasOwnPropertySafely(newFormKeyAndName, key)) {
1963
- delete formKeyAndName[key];
1964
- }
1965
- });
1966
- for (const key in newFormKeyAndName) {
1967
- if (hasOwnPropertySafely(newFormKeyAndName, key)) {
1968
- if (formKeyAndName[key] !== newFormKeyAndName[key]) {
1969
- formKeyAndName[key] = newFormKeyAndName[key];
1970
- }
1971
- }
1972
- }
1973
- formContent.value = res.formContent;
1974
1964
  if (isFirstInit.value) {
1975
- const requests2 = Object.keys(res.requests).map((key) => {
1965
+ const mergedData = Object.assign({}, res.formData, props.modelValue);
1966
+ formValues.value = mergedData;
1967
+ context.emit("update:modelValue", { ...formValues.value });
1968
+ formInitValues.value = Object.assign({}, res.formData);
1969
+ const requestPromises = Object.keys(res.requests).map((key) => {
1976
1970
  return res.requests[key]();
1977
1971
  });
1978
- formInitValues.value = Object.assign({}, res.formData);
1979
- if (requests2 && requests2.length > 0) {
1980
- await Promise.allSettled(requests2).catch((err) => {
1981
- console.warn(err);
1972
+ if (requestPromises.length > 0) {
1973
+ await Promise.allSettled(requestPromises).catch((err) => {
1974
+ console.warn("Form source data fetch error:", err);
1982
1975
  });
1983
1976
  }
1984
- formValues.value = Object.assign({}, res.formData);
1985
- if (needOverWriteForm.value) {
1986
- overWrite(needOverWriteForm.value);
1987
- needOverWriteForm.value = {};
1977
+ if (Object.keys(needOverWriteForm.value).length > 0) {
1978
+ overWrite();
1988
1979
  }
1989
1980
  nextTick(() => {
1990
1981
  isFirstInit.value = false;
1991
1982
  });
1983
+ } else {
1984
+ Object.keys(res.formData).forEach((key) => {
1985
+ if (!hasOwnPropertySafely(formValues.value, key)) {
1986
+ formValues.value[key] = res.formData[key];
1987
+ }
1988
+ });
1992
1989
  }
1990
+ const newFormKeyAndName = res.formKeyAndName || {};
1991
+ Object.keys(formKeyAndName).forEach((key) => {
1992
+ if (!hasOwnPropertySafely(newFormKeyAndName, key)) {
1993
+ delete formKeyAndName[key];
1994
+ }
1995
+ });
1996
+ for (const key in newFormKeyAndName) {
1997
+ if (formKeyAndName[key] !== newFormKeyAndName[key]) {
1998
+ formKeyAndName[key] = newFormKeyAndName[key];
1999
+ }
2000
+ }
2001
+ formContent.value = res.formContent;
1993
2002
  };
2003
+ watch(
2004
+ () => props.modelValue,
2005
+ (newVal) => {
2006
+ if (newVal && !isEqual(newVal, formValues.value)) {
2007
+ Object.assign(formValues.value, newVal);
2008
+ }
2009
+ },
2010
+ { deep: true }
2011
+ );
2012
+ watch(
2013
+ formValues,
2014
+ (newVal) => {
2015
+ context.emit("update:modelValue", { ...newVal });
2016
+ },
2017
+ { deep: true }
2018
+ );
1994
2019
  const collectFormContent = (data = []) => {
1995
2020
  const res = {
1996
2021
  formContent: [],
@@ -2149,6 +2174,50 @@ const FlyElForm = defineComponent({
2149
2174
  };
2150
2175
  },
2151
2176
  methods: {
2177
+ /**
2178
+ * 异步提交方法,供父组件通过 ref 调用
2179
+ * @returns Promise<{ valid: boolean, formValues: any, sourceData?: any }>
2180
+ */
2181
+ async asyncSubmit() {
2182
+ const formRef = this.$refs.FlyFormRef;
2183
+ try {
2184
+ const valid = await formRef.validate();
2185
+ let returnData = {
2186
+ valid: true,
2187
+ formValues: { ...this.formValues }
2188
+ };
2189
+ if (this.needReturnSourceKeys && this.needReturnSourceKeys.length > 0) {
2190
+ let templateSourceData = {};
2191
+ this.needReturnSourceKeys.forEach((key) => {
2192
+ templateSourceData[key] = this.sourceData[key];
2193
+ });
2194
+ returnData.sourceData = templateSourceData;
2195
+ }
2196
+ return returnData;
2197
+ } catch (errors) {
2198
+ if (this.$props.model === "search") {
2199
+ const errorMsg = [];
2200
+ const errorNames = [];
2201
+ Object.keys(errors).forEach((key) => {
2202
+ const item = errors[key][0];
2203
+ errorMsg.push(item.message);
2204
+ errorNames.push(this.formKeyAndName[key]);
2205
+ });
2206
+ if (this.$props.singleStepErrorTip) {
2207
+ ElMessage.error(errorMsg[0]);
2208
+ } else {
2209
+ ElMessage.error(
2210
+ `请完善${errorNames.length > 0 ? errorNames.join("、") : "查询条件"}`
2211
+ );
2212
+ }
2213
+ }
2214
+ return {
2215
+ valid: false,
2216
+ formValues: this.formValues,
2217
+ errors
2218
+ };
2219
+ }
2220
+ },
2152
2221
  async submit() {
2153
2222
  const formRef = this.$refs.FlyFormRef;
2154
2223
  await formRef.validate((valid, errors) => {
@@ -2156,7 +2225,6 @@ const FlyElForm = defineComponent({
2156
2225
  valid,
2157
2226
  formValues: this.formValues
2158
2227
  };
2159
- console.log(this.needReturnSourceKeys);
2160
2228
  if (this.needReturnSourceKeys && this.needReturnSourceKeys.length > 0) {
2161
2229
  let templateSourceData = {};
2162
2230
  this.needReturnSourceKeys.forEach((key) => {
@@ -2196,7 +2264,6 @@ const FlyElForm = defineComponent({
2196
2264
  }
2197
2265
  },
2198
2266
  getComponentRefByKey(key) {
2199
- console.log(key);
2200
2267
  if (key && typeof key === "string") {
2201
2268
  try {
2202
2269
  return this.$refs[key];
@@ -1773,6 +1773,11 @@ usage: app.provide(ZINDEX_INJECTION_KEY, { current: 0 })`);
1773
1773
  const FlyElForm = vue.defineComponent({
1774
1774
  name: "FlyElForm",
1775
1775
  props: {
1776
+ // 新增:支持 v-model
1777
+ modelValue: {
1778
+ type: Object,
1779
+ default: () => ({})
1780
+ },
1776
1781
  // 模式 默认form表单模式 search为搜索条模式
1777
1782
  model: {
1778
1783
  type: String,
@@ -1879,6 +1884,7 @@ usage: app.provide(ZINDEX_INJECTION_KEY, { current: 0 })`);
1879
1884
  default: false
1880
1885
  }
1881
1886
  },
1887
+ emits: ["update:modelValue", "submit", "reset"],
1882
1888
  setup(props, context) {
1883
1889
  const isFirstInit = vue.ref(true);
1884
1890
  const formContent = vue.ref({});
@@ -1940,7 +1946,6 @@ usage: app.provide(ZINDEX_INJECTION_KEY, { current: 0 })`);
1940
1946
  for (const key in newRules) {
1941
1947
  if (hasOwnPropertySafely(newRules, key)) {
1942
1948
  if (!hasOwnPropertySafely(rules.value, key) || !isEqual(rules.value[key], newRules[key])) {
1943
- console.log(key, newRules[key]);
1944
1949
  rules.value[key] = newRules[key];
1945
1950
  }
1946
1951
  }
@@ -1959,41 +1964,61 @@ usage: app.provide(ZINDEX_INJECTION_KEY, { current: 0 })`);
1959
1964
  }
1960
1965
  }
1961
1966
  }
1962
- const newFormKeyAndName = res.formKeyAndName || {};
1963
- const oldFormKeyAndNameKeys = Object.keys(formKeyAndName);
1964
- oldFormKeyAndNameKeys.forEach((key) => {
1965
- if (!hasOwnPropertySafely(newFormKeyAndName, key)) {
1966
- delete formKeyAndName[key];
1967
- }
1968
- });
1969
- for (const key in newFormKeyAndName) {
1970
- if (hasOwnPropertySafely(newFormKeyAndName, key)) {
1971
- if (formKeyAndName[key] !== newFormKeyAndName[key]) {
1972
- formKeyAndName[key] = newFormKeyAndName[key];
1973
- }
1974
- }
1975
- }
1976
- formContent.value = res.formContent;
1977
1967
  if (isFirstInit.value) {
1978
- const requests2 = Object.keys(res.requests).map((key) => {
1968
+ const mergedData = Object.assign({}, res.formData, props.modelValue);
1969
+ formValues.value = mergedData;
1970
+ context.emit("update:modelValue", { ...formValues.value });
1971
+ formInitValues.value = Object.assign({}, res.formData);
1972
+ const requestPromises = Object.keys(res.requests).map((key) => {
1979
1973
  return res.requests[key]();
1980
1974
  });
1981
- formInitValues.value = Object.assign({}, res.formData);
1982
- if (requests2 && requests2.length > 0) {
1983
- await Promise.allSettled(requests2).catch((err) => {
1984
- console.warn(err);
1975
+ if (requestPromises.length > 0) {
1976
+ await Promise.allSettled(requestPromises).catch((err) => {
1977
+ console.warn("Form source data fetch error:", err);
1985
1978
  });
1986
1979
  }
1987
- formValues.value = Object.assign({}, res.formData);
1988
- if (needOverWriteForm.value) {
1989
- overWrite(needOverWriteForm.value);
1990
- needOverWriteForm.value = {};
1980
+ if (Object.keys(needOverWriteForm.value).length > 0) {
1981
+ overWrite();
1991
1982
  }
1992
1983
  vue.nextTick(() => {
1993
1984
  isFirstInit.value = false;
1994
1985
  });
1986
+ } else {
1987
+ Object.keys(res.formData).forEach((key) => {
1988
+ if (!hasOwnPropertySafely(formValues.value, key)) {
1989
+ formValues.value[key] = res.formData[key];
1990
+ }
1991
+ });
1995
1992
  }
1993
+ const newFormKeyAndName = res.formKeyAndName || {};
1994
+ Object.keys(formKeyAndName).forEach((key) => {
1995
+ if (!hasOwnPropertySafely(newFormKeyAndName, key)) {
1996
+ delete formKeyAndName[key];
1997
+ }
1998
+ });
1999
+ for (const key in newFormKeyAndName) {
2000
+ if (formKeyAndName[key] !== newFormKeyAndName[key]) {
2001
+ formKeyAndName[key] = newFormKeyAndName[key];
2002
+ }
2003
+ }
2004
+ formContent.value = res.formContent;
1996
2005
  };
2006
+ vue.watch(
2007
+ () => props.modelValue,
2008
+ (newVal) => {
2009
+ if (newVal && !isEqual(newVal, formValues.value)) {
2010
+ Object.assign(formValues.value, newVal);
2011
+ }
2012
+ },
2013
+ { deep: true }
2014
+ );
2015
+ vue.watch(
2016
+ formValues,
2017
+ (newVal) => {
2018
+ context.emit("update:modelValue", { ...newVal });
2019
+ },
2020
+ { deep: true }
2021
+ );
1997
2022
  const collectFormContent = (data = []) => {
1998
2023
  const res = {
1999
2024
  formContent: [],
@@ -2152,6 +2177,50 @@ usage: app.provide(ZINDEX_INJECTION_KEY, { current: 0 })`);
2152
2177
  };
2153
2178
  },
2154
2179
  methods: {
2180
+ /**
2181
+ * 异步提交方法,供父组件通过 ref 调用
2182
+ * @returns Promise<{ valid: boolean, formValues: any, sourceData?: any }>
2183
+ */
2184
+ async asyncSubmit() {
2185
+ const formRef = this.$refs.FlyFormRef;
2186
+ try {
2187
+ const valid = await formRef.validate();
2188
+ let returnData = {
2189
+ valid: true,
2190
+ formValues: { ...this.formValues }
2191
+ };
2192
+ if (this.needReturnSourceKeys && this.needReturnSourceKeys.length > 0) {
2193
+ let templateSourceData = {};
2194
+ this.needReturnSourceKeys.forEach((key) => {
2195
+ templateSourceData[key] = this.sourceData[key];
2196
+ });
2197
+ returnData.sourceData = templateSourceData;
2198
+ }
2199
+ return returnData;
2200
+ } catch (errors) {
2201
+ if (this.$props.model === "search") {
2202
+ const errorMsg = [];
2203
+ const errorNames = [];
2204
+ Object.keys(errors).forEach((key) => {
2205
+ const item = errors[key][0];
2206
+ errorMsg.push(item.message);
2207
+ errorNames.push(this.formKeyAndName[key]);
2208
+ });
2209
+ if (this.$props.singleStepErrorTip) {
2210
+ ElMessage.error(errorMsg[0]);
2211
+ } else {
2212
+ ElMessage.error(
2213
+ `请完善${errorNames.length > 0 ? errorNames.join("、") : "查询条件"}`
2214
+ );
2215
+ }
2216
+ }
2217
+ return {
2218
+ valid: false,
2219
+ formValues: this.formValues,
2220
+ errors
2221
+ };
2222
+ }
2223
+ },
2155
2224
  async submit() {
2156
2225
  const formRef = this.$refs.FlyFormRef;
2157
2226
  await formRef.validate((valid, errors) => {
@@ -2159,7 +2228,6 @@ usage: app.provide(ZINDEX_INJECTION_KEY, { current: 0 })`);
2159
2228
  valid,
2160
2229
  formValues: this.formValues
2161
2230
  };
2162
- console.log(this.needReturnSourceKeys);
2163
2231
  if (this.needReturnSourceKeys && this.needReturnSourceKeys.length > 0) {
2164
2232
  let templateSourceData = {};
2165
2233
  this.needReturnSourceKeys.forEach((key) => {
@@ -2199,7 +2267,6 @@ usage: app.provide(ZINDEX_INJECTION_KEY, { current: 0 })`);
2199
2267
  }
2200
2268
  },
2201
2269
  getComponentRefByKey(key) {
2202
- console.log(key);
2203
2270
  if (key && typeof key === "string") {
2204
2271
  try {
2205
2272
  return this.$refs[key];
package/package.json CHANGED
@@ -1,26 +1,12 @@
1
1
  {
2
2
  "name": "fly-el-form",
3
- "version": "0.0.30",
3
+ "version": "0.0.32",
4
4
  "private": false,
5
5
  "author": "flycat",
6
6
  "type": "module",
7
7
  "description": "A Vue 3 library for building dynamic forms with Element Plus",
8
8
  "main": "lib/fly-el-form.umd.js",
9
9
  "module": "lib/fly-el-form.es.js",
10
- "exports": {
11
- ".": {
12
- "import": "./lib/fly-el-form.es.js",
13
- "require": "./lib/fly-el-form.umd.js"
14
- },
15
- "./useFormValues": {
16
- "import": "./lib/fly-el-form.es.js",
17
- "require": "./lib/fly-el-form.umd.js"
18
- },
19
- "./style.css": {
20
- "import": "./lib/style.css",
21
- "require": "./lib/style.css"
22
- }
23
- },
24
10
  "files": [
25
11
  "lib"
26
12
  ],