@gx-design-vue/pro-table 0.2.0-alpha.18 → 0.2.0-alpha.19

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/ProTable.js CHANGED
@@ -140,9 +140,10 @@ const ForwardProTable = /* @__PURE__ */ defineComponent((props, { attrs, slots,
140
140
  if (props.form === false) return void 0;
141
141
  return props.form?.actions;
142
142
  });
143
- const { formColumns, defaultFormSearch, formState, getFormSearch, resetFormState } = useTableForm({
143
+ const { formColumns, formParamKeys, defaultFormSearch, formState, getFormSearch, resetFormState } = useTableForm({
144
144
  searchMap: computed(() => props.searchMap ?? []),
145
- columns: computed(() => props.columns ?? [])
145
+ columns: computed(() => props.columns ?? []),
146
+ params: computed(() => props.params)
146
147
  });
147
148
  const { dataSource, setTableDataList, operateTableDataRow, handleTableChange, reload, run } = useFetchData({
148
149
  rowKey: computed(() => props.rowKey),
@@ -157,6 +158,8 @@ const ForwardProTable = /* @__PURE__ */ defineComponent((props, { attrs, slots,
157
158
  debounceTime: computed(() => props.debounceTime ?? 10),
158
159
  params: computed(() => props.params),
159
160
  defaultFormSearch,
161
+ formParamKeys,
162
+ getFormSearch,
160
163
  onLoadingChange: props.onLoadingChange,
161
164
  onRequestError: props.onRequestError,
162
165
  onBeforeSearchSubmit: props.onBeforeSearchSubmit
@@ -111,6 +111,8 @@ declare const ProTableSearchForm: import("vue").DefineComponent<import("vue").Ex
111
111
  }>> & Readonly<{}>, {
112
112
  onReset: (params: Record<string, any>) => void;
113
113
  loading: any;
114
+ proClasses: Record<string, string>;
115
+ proStyles: Record<string, any>;
114
116
  formColumns: ProSearchMap<"text", string>[];
115
117
  formConfig: FormConfig;
116
118
  formAutoRequest: boolean;
@@ -121,8 +123,6 @@ declare const ProTableSearchForm: import("vue").DefineComponent<import("vue").Ex
121
123
  resetText?: string;
122
124
  };
123
125
  cardBordered: boolean;
124
- proClasses: Record<string, string>;
125
- proStyles: Record<string, any>;
126
126
  }, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
127
127
  //#endregion
128
128
  export { ProTableSearchForm as default };
@@ -91,19 +91,19 @@ declare const ListToolBar: import("vue").DefineComponent<import("vue").ExtractPr
91
91
  default: () => {};
92
92
  };
93
93
  }>> & Readonly<{}>, {
94
- actions: VNode<import("vue").RendererNode, import("vue").RendererElement, {
95
- [key: string]: any;
96
- }>[];
97
- proClasses: Record<string, any>;
98
- proStyles: Record<string, any>;
99
- titleTipText: string;
100
- actionsPlacement: ToolbarPlacement;
101
94
  headerTitle: any;
102
- listToolAfter: any;
103
95
  headerTitleTip: any;
96
+ titleTipText: string;
104
97
  settings: VNode<import("vue").RendererNode, import("vue").RendererElement, {
105
98
  [key: string]: any;
106
99
  }>[];
100
+ actions: VNode<import("vue").RendererNode, import("vue").RendererElement, {
101
+ [key: string]: any;
102
+ }>[];
103
+ actionsPlacement: ToolbarPlacement;
104
+ listToolAfter: any;
105
+ proClasses: Record<string, any>;
106
+ proStyles: Record<string, any>;
107
107
  }, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
108
108
  //#endregion
109
109
  export { ListToolBar, ListToolBarSetting };
@@ -85,17 +85,17 @@ declare const Toolbar: import("vue").DefineComponent<import("vue").ExtractPropTy
85
85
  default: () => {};
86
86
  };
87
87
  }>> & Readonly<{}>, {
88
+ headerTitle: any;
89
+ headerTitleTip: any;
90
+ titleTipText: string;
88
91
  actions: VNode<import("vue").RendererNode, import("vue").RendererElement, {
89
92
  [key: string]: any;
90
93
  }>[];
94
+ actionsPlacement: ToolbarPlacement;
95
+ listToolAfter: any;
91
96
  proClasses: Record<string, any>;
92
97
  proStyles: Record<string, any>;
93
98
  options: any;
94
- titleTipText: string;
95
- actionsPlacement: ToolbarPlacement;
96
- headerTitle: any;
97
- listToolAfter: any;
98
- headerTitleTip: any;
99
99
  }, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
100
100
  //#endregion
101
101
  export { Toolbar, Toolbar as default };
@@ -16,6 +16,8 @@ interface FetchDataConfig<T = any, R = any> {
16
16
  debounceTime: ComputedRef<number>;
17
17
  params: ComputedRef<R | undefined>;
18
18
  defaultFormSearch: Ref<Record<string, any>>;
19
+ formParamKeys: ComputedRef<string[]>;
20
+ getFormSearch: () => Record<string, any>;
19
21
  onLoadingChange: ((loading: boolean) => any) | undefined;
20
22
  onRequestError: ((e: Error) => void) | undefined;
21
23
  onBeforeSearchSubmit: BeforeSearchSubmit<R> | undefined;
@@ -13,7 +13,7 @@ const EMPTY_ARRAY = [];
13
13
  * 通过 requestId + AbortController 双重保证旧响应不回写。
14
14
  */
15
15
  function useFetchData(config, action) {
16
- const { rowKey, polling, pollingTime, request, postData, dataSource, immediate, waitRequest, autoRequest, debounceTime, params, defaultFormSearch, onLoadingChange, onRequestError, onBeforeSearchSubmit } = config;
16
+ const { rowKey, polling, pollingTime, request, postData, dataSource, immediate, waitRequest, autoRequest, debounceTime, params, defaultFormSearch, formParamKeys, getFormSearch, onLoadingChange, onRequestError, onBeforeSearchSubmit } = config;
17
17
  const { pageInfo, paginationInfo, setPagination, setLoading, setColumns, removeRowKeys, syncSelectedRows, columns } = action;
18
18
  const tableDataList = ref([]);
19
19
  const unmounted = ref(false);
@@ -48,6 +48,13 @@ function useFetchData(config, action) {
48
48
  abortController.value?.abort();
49
49
  abortController.value = null;
50
50
  }
51
+ function omitFormParams(source) {
52
+ if (!source) return {};
53
+ const omittedKeys = new Set(formParamKeys.value);
54
+ const result = {};
55
+ for (const key of Object.keys(source)) if (!omittedKeys.has(key)) result[key] = source[key];
56
+ return result;
57
+ }
51
58
  /** 写入表格数据并同步 sortIndex,无 request 时同步 dataSource total */
52
59
  function setTableDataList(list) {
53
60
  tableDataList.value = getSortIndex(toRaw(list), toRaw(pageInfo));
@@ -81,7 +88,8 @@ function useFetchData(config, action) {
81
88
  current: handleCurrentPage(toRaw(pageInfo), removeKeys.length)
82
89
  },
83
90
  ...toRaw(defaultFormSearch.value),
84
- ...toRaw(params.value),
91
+ ...getFormSearch(),
92
+ ...omitFormParams(toRaw(params.value)),
85
93
  ...fetchParams.params || {}
86
94
  };
87
95
  if (onBeforeSearchSubmit && isFunction(onBeforeSearchSubmit)) actionParams = await onBeforeSearchSubmit({
@@ -161,13 +169,6 @@ function useFetchData(config, action) {
161
169
  dispatchFetch({ isPolling: false });
162
170
  }
163
171
  }, { deep: true });
164
- watch(() => defaultFormSearch.value, (current, previous) => {
165
- if (waitRequest.value) return;
166
- if (current !== previous) {
167
- setPagination({ current: 1 });
168
- dispatchFetch({ isPolling: false });
169
- }
170
- }, { deep: true });
171
172
  watch(() => polling.value, () => {
172
173
  if (waitRequest.value) return;
173
174
  if (!polling.value) stopPolling();
@@ -5,9 +5,11 @@ import { ComputedRef, Ref } from "vue";
5
5
  interface UseTableFormOptions {
6
6
  searchMap: ComputedRef<ProSearchMap[]>;
7
7
  columns: ComputedRef<ProColumnType[]>;
8
+ params: ComputedRef<Record<string, any> | undefined>;
8
9
  }
9
10
  interface UseTableFormReturn {
10
11
  formColumns: ComputedRef<ProSearchMap[]>;
12
+ formParamKeys: ComputedRef<string[]>;
11
13
  defaultFormSearch: Ref<Record<string, any>>;
12
14
  formState: Ref<Record<string, any>>;
13
15
  getFormSearch: () => Record<string, any>;
@@ -1,6 +1,9 @@
1
1
  import { computed, ref, toRaw, watch } from "vue";
2
2
  import { cloneDeep } from "@gx-design-vue/pro-utils";
3
3
  //#region src/hooks/useTableForm.ts
4
+ function hasOwnValue(target, key) {
5
+ return !!target && Object.hasOwn(target, key);
6
+ }
4
7
  function sortFormItemsByOrder(items) {
5
8
  let maxOrder = items.reduce((max, item) => {
6
9
  return item.order !== void 0 ? Math.max(max, item.order) : max;
@@ -18,46 +21,97 @@ function sortFormItemsByOrder(items) {
18
21
  sorted.sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
19
22
  return sorted;
20
23
  }
21
- function computeDefaultFormValues(columns) {
24
+ function computeDefaultFormValues(columns, params) {
22
25
  const defaults = {};
23
26
  for (const col of columns) {
24
27
  const valueType = col.valueType ?? "text";
25
- if (col.initialValue !== void 0) defaults[col.name] = col.initialValue;
28
+ const name = col.name;
29
+ if (valueType === "dateRange" && (col.rangeStartName || col.rangeEndName)) {
30
+ const startKey = col.rangeStartName || "start";
31
+ const endKey = col.rangeEndName || "end";
32
+ defaults[startKey] = hasOwnValue(params, startKey) ? params[startKey] : void 0;
33
+ defaults[endKey] = hasOwnValue(params, endKey) ? params[endKey] : void 0;
34
+ } else if (hasOwnValue(params, name)) defaults[name] = params[name];
35
+ else if (col.initialValue !== void 0) defaults[col.name] = col.initialValue;
26
36
  else if (valueType === "numberRange") defaults[col.name] = [];
27
37
  else if (valueType === "treeSelect" && (col.field?.treeCheckable || col.field?.multiple)) defaults[col.name] = [];
28
38
  else if (valueType === "cascader" && col.field?.multiple) defaults[col.name] = [];
29
- else if (valueType === "dateRange") if (col.rangeStartName || col.rangeEndName) {
30
- defaults[col.rangeStartName || "start"] = void 0;
31
- defaults[col.rangeEndName || "end"] = void 0;
32
- } else defaults[col.name] = void 0;
39
+ else if (valueType === "dateRange") defaults[col.name] = void 0;
33
40
  }
34
41
  return defaults;
35
42
  }
43
+ function buildFormStateFromDefaults(defaults, columns) {
44
+ const state = cloneDeep(toRaw(defaults));
45
+ for (const record of columns) if (record.valueType === "dateRange" && (record.rangeStartName || record.rangeEndName)) {
46
+ const startKey = record.rangeStartName || "start";
47
+ const endKey = record.rangeEndName || "end";
48
+ const startValue = defaults[startKey];
49
+ const endValue = defaults[endKey];
50
+ if (startValue !== void 0 || endValue !== void 0) state[record.name] = [startValue, endValue];
51
+ }
52
+ return state;
53
+ }
36
54
  function useTableForm(options) {
37
- const { searchMap, columns } = options;
55
+ const { searchMap, columns, params } = options;
38
56
  const formColumns = computed(() => {
39
- const colsFromSearchMap = searchMap.value;
57
+ const colsFromSearchMap = searchMap.value ?? [];
58
+ const searchMapNames = new Set(colsFromSearchMap.map((item) => item.name));
40
59
  const colsFromColumns = columns.value.filter((col) => col.searchConfig).map((col) => ({
41
60
  ...col.searchConfig,
42
61
  order: col.searchConfig.order ?? col.order ?? 0
43
- }));
62
+ })).filter((col) => !searchMapNames.has(col.name));
44
63
  return sortFormItemsByOrder([...colsFromSearchMap, ...colsFromColumns]);
45
64
  });
65
+ const formParamKeys = computed(() => {
66
+ const keys = /* @__PURE__ */ new Set();
67
+ for (const record of formColumns.value) {
68
+ if (record.name !== void 0) keys.add(record.name);
69
+ if (record.valueType === "dateRange" && (record.rangeStartName || record.rangeEndName)) {
70
+ keys.add(record.rangeStartName || "start");
71
+ keys.add(record.rangeEndName || "end");
72
+ }
73
+ }
74
+ return [...keys];
75
+ });
46
76
  const defaultFormSearch = computed(() => {
47
- return computeDefaultFormValues(formColumns.value);
77
+ return computeDefaultFormValues(formColumns.value, params.value);
48
78
  });
49
79
  const formState = ref({});
50
80
  watch(() => defaultFormSearch.value, (newDefaults) => {
51
- formState.value = cloneDeep(toRaw(newDefaults));
81
+ const nextState = { ...formState.value };
82
+ const defaultState = buildFormStateFromDefaults(newDefaults, formColumns.value);
83
+ for (const key of Object.keys(defaultState)) if (!hasOwnValue(nextState, key)) nextState[key] = defaultState[key];
84
+ formState.value = nextState;
85
+ }, {
86
+ deep: true,
87
+ immediate: true
88
+ });
89
+ watch(() => params.value, (newParams) => {
90
+ if (!newParams) return;
91
+ const nextState = { ...formState.value };
92
+ for (const record of formColumns.value) {
93
+ const name = record.name;
94
+ if (record.valueType === "dateRange" && (record.rangeStartName || record.rangeEndName)) {
95
+ const startKey = record.rangeStartName || "start";
96
+ const endKey = record.rangeEndName || "end";
97
+ if (hasOwnValue(newParams, startKey) || hasOwnValue(newParams, endKey)) {
98
+ const startValue = newParams[startKey];
99
+ const endValue = newParams[endKey];
100
+ nextState[name] = startValue !== void 0 || endValue !== void 0 ? [startValue, endValue] : void 0;
101
+ }
102
+ } else if (hasOwnValue(newParams, name)) nextState[name] = newParams[name];
103
+ }
104
+ formState.value = nextState;
52
105
  }, {
53
106
  deep: true,
54
107
  immediate: true
55
108
  });
56
109
  function getFormSearch() {
57
- const searchParams = cloneDeep(toRaw(formState.value));
110
+ const searchParams = {};
58
111
  for (const record of formColumns.value) {
59
112
  const valueType = record.valueType ?? "text";
60
113
  const name = record.name;
114
+ searchParams[name] = cloneDeep(toRaw(formState.value[name]));
61
115
  if (valueType === "text") searchParams[name] = formState.value[name] ?? record.initialValue;
62
116
  if (valueType === "dateRange" && (record.rangeStartName || record.rangeEndName)) {
63
117
  const rangeValue = searchParams[name];
@@ -81,21 +135,14 @@ function useTableForm(options) {
81
135
  return searchParams;
82
136
  }
83
137
  function resetFormState() {
84
- const defaults = cloneDeep(toRaw(defaultFormSearch.value));
85
- formState.value = defaults;
86
- for (const record of formColumns.value) if (record.valueType === "dateRange" && (record.rangeStartName || record.rangeEndName)) {
87
- const startKey = record.rangeStartName || "start";
88
- const endKey = record.rangeEndName || "end";
89
- const startValue = defaults[startKey];
90
- const endValue = defaults[endKey];
91
- if (startValue !== void 0 || endValue !== void 0) formState.value[record.name] = [startValue, endValue];
92
- }
138
+ formState.value = buildFormStateFromDefaults(cloneDeep(toRaw(defaultFormSearch.value)), formColumns.value);
93
139
  }
94
140
  function changeFormState(key, value) {
95
141
  if (key) formState.value[key] = value;
96
142
  }
97
143
  return {
98
144
  formColumns,
145
+ formParamKeys,
99
146
  defaultFormSearch,
100
147
  formState,
101
148
  getFormSearch,