@xmszm/core 0.0.4 → 0.0.6

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.
@@ -1,154 +1,160 @@
1
1
  <script setup lang="tsx">
2
- import type { DataTableProps, DataTableExpose } from '../../types/components'
3
- import { ChevronDown, ChevronUp, Code, Funnel } from '@vicons/ionicons5'
4
- import { commonDialogMethod, toArray } from 'core'
5
- import { ellipsis } from 'core/table/utils/ellipsis'
6
- import dayjs from 'dayjs'
7
- import { uniqueId } from 'lodash-es'
8
- import { NButton, NTooltip } from 'naive-ui'
9
- import { computed, onMounted, ref, unref, watch ,isProxy, getCurrentInstance} from 'vue'
10
- import { useRoute } from 'vue-router'
11
- import FilterDialog from './FilterDialog.vue'
12
- import { orderEnum } from 'core'
13
- import { registerDirectives } from '../directives/auto-register'
14
- import {NDataTable} from 'naive-ui'
2
+ import type { DataTableProps, DataTableExpose } from "../../types/components";
3
+ import { ChevronDown, ChevronUp, Code, Funnel } from "@vicons/ionicons5";
4
+ import { commonDialogMethod, toArray } from "core";
5
+ import { ellipsis } from "core/table/utils/ellipsis";
6
+ import dayjs from "dayjs";
7
+ import { uniqueId } from "lodash-es";
8
+ import { NButton, NTooltip } from "naive-ui";
9
+ import {
10
+ computed,
11
+ onMounted,
12
+ ref,
13
+ unref,
14
+ watch,
15
+ isProxy,
16
+ getCurrentInstance,
17
+ } from "vue";
18
+ import { useRoute } from "vue-router";
19
+ import FilterDialog from "./FilterDialog.vue";
20
+ import { orderEnum } from "core";
21
+ import { registerDirectives } from "../directives/auto-register";
22
+ import { NDataTable, NEmpty } from "naive-ui";
15
23
  // 自动注册指令
16
- const instance = getCurrentInstance()
24
+ const instance = getCurrentInstance();
17
25
  if (instance?.appContext?.app) {
18
- registerDirectives(instance.appContext.app)
26
+ registerDirectives(instance.appContext.app);
19
27
  }
20
28
 
21
29
  const props = withDefaults(defineProps<DataTableProps>(), {
22
30
  data: () => [],
23
31
  pagination: undefined,
24
32
  columns: () => [
25
- {
26
- title: '测试案例',
27
- },
28
- ],
33
+ {
34
+ title: "测试案例",
35
+ },
36
+ ],
29
37
  oprColumns: null,
30
38
  selectColumns: null,
31
39
  defaultColumns: () => [],
32
40
  summaryColumns: null,
33
- emptyText: '没有数据',
34
- emptyIcon: '',
41
+ emptyText: "没有数据",
42
+ emptyIcon: "",
35
43
  isFilter: false,
36
44
  isEllipsis: true,
37
45
  virtual: () => ({}),
38
46
  singleColumn: false,
39
- })
40
- const route = useRoute()
41
- const FilterKey = 'filter_key'
47
+ });
48
+ const route = useRoute();
49
+ const FilterKey = "filter_key";
42
50
  const emit = defineEmits<{
43
- sorted: []
44
- }>()
51
+ sorted: [];
52
+ }>();
45
53
 
46
54
  // 安全获取路由路径,如果没有路由上下文则使用默认值
47
55
  const getRoutePath = (): string => {
48
56
  try {
49
- return route?.fullPath || route?.path || ''
57
+ return route?.fullPath || route?.path || "";
50
58
  } catch {
51
- return ''
59
+ return "";
52
60
  }
53
- }
61
+ };
54
62
 
55
63
  const _data = computed(() => {
56
- console.log('table -data', props.data)
64
+ console.log("table -data", props.data);
57
65
 
58
- return props.data
59
- })
66
+ return props.data;
67
+ });
60
68
  function setHeadFilter(val: Record<string, any>): void {
61
- window.localStorage.setItem(FilterKey, JSON.stringify(val))
69
+ window.localStorage.setItem(FilterKey, JSON.stringify(val));
62
70
  }
63
71
 
64
72
  function getHeadFilter(): Record<string, any> {
65
- return JSON.parse(window.localStorage.getItem(FilterKey) || '{}') || {}
73
+ return JSON.parse(window.localStorage.getItem(FilterKey) || "{}") || {};
66
74
  }
67
75
 
68
- const getFilterAll = ref(getHeadFilter())
69
- const headDefault = ref([])
76
+ const getFilterAll = ref(getHeadFilter());
77
+ const headDefault = ref([]);
70
78
 
71
- const scrollX = ref(0)
79
+ const scrollX = ref(0);
72
80
 
73
81
  function _summary(pageData: any[]): Record<string, { value: any }> | undefined {
74
- if (!props.summaryColumns)
75
- return
82
+ if (!props.summaryColumns) return;
76
83
  return [
77
84
  props.selectColumns,
78
85
  ...unref(props.columns || []),
79
86
  props.oprColumns,
80
87
  ]?.reduce((o: Record<string, { value: any }>, n: any) => {
81
88
  if (n?.key)
82
- o[n.key] = props.summaryColumns?.(pageData)?.[n.key] || { value: null }
83
- else o[uniqueId('table')] = { value: null }
84
- return o
85
- }, {})
89
+ o[n.key] = props.summaryColumns?.(pageData)?.[n.key] || { value: null };
90
+ else o[uniqueId("table")] = { value: null };
91
+ return o;
92
+ }, {});
86
93
  }
87
94
 
88
- const _columns = ref([])
95
+ const _columns = ref([]);
89
96
 
90
97
  watch(
91
98
  () => unref(props.columns),
92
99
  () => {
93
- init()
100
+ init();
94
101
  },
95
102
  {
96
103
  immediate: true,
97
- },
98
- )
104
+ }
105
+ );
99
106
 
100
107
  watch(
101
108
  () => props.oprColumns,
102
109
  (v) => {
103
- console.log('oprColumns', v)
104
- },
105
- )
110
+ console.log("oprColumns", v);
111
+ }
112
+ );
106
113
 
107
114
  function init(): void {
108
- const columns = unref(props.columns || [])
109
- const routePath = getRoutePath()
110
- headDefault.value
111
- = (routePath && getFilterAll.value?.[routePath])
112
- || columns?.map((v: any, i: number) => v?.key || String(dayjs().valueOf() + i))
115
+ const columns = unref(props.columns || []);
116
+ const routePath = getRoutePath();
117
+ headDefault.value =
118
+ (routePath && getFilterAll.value?.[routePath]) ||
119
+ columns?.map(
120
+ (v: any, i: number) => v?.key || String(dayjs().valueOf() + i)
121
+ );
113
122
 
114
123
  const arr: any[] = props.isFilter
115
124
  ? columns.filter((item: any) => headDefault.value?.includes(item.key))
116
- : [...columns]
125
+ : [...columns];
117
126
  if (props.selectColumns)
118
- arr.unshift({ key: 'selectKey', ...props.selectColumns })
119
- if (props.oprColumns)
120
- arr.push(props.oprColumns)
121
- let scrollNum = 0
127
+ arr.unshift({ key: "selectKey", ...props.selectColumns });
128
+ if (props.oprColumns) arr.push(props.oprColumns);
129
+ let scrollNum = 0;
122
130
  _columns.value = arr.reduce((o: any[], obj: any) => {
123
- if (obj?.display)
124
- console.log('display', obj?.display)
125
- if (!(obj?.display ?? true))
126
- return o
131
+ if (obj?.display) console.log("display", obj?.display);
132
+ if (!(obj?.display ?? true)) return o;
127
133
  const v: any = {
128
- 'align': 'center',
129
- 'width': 120,
134
+ align: "center",
135
+ width: 120,
130
136
  ...obj,
131
- 'key': obj?.key || uniqueId('table'),
132
- 'ellipsis':
137
+ key: obj?.key || uniqueId("table"),
138
+ ellipsis:
133
139
  obj?.ellipsis || props.isEllipsis
134
140
  ? obj?.ellipsisProp
135
141
  ? obj?.ellipsisProp(ellipsis)
136
142
  : ellipsis
137
143
  : false,
138
- 'ellipsis-component': 'ellipsis',
139
- 'title': () => {
140
- const title = obj?.label || obj?.title || ''
144
+ "ellipsis-component": "ellipsis",
145
+ title: () => {
146
+ const title = obj?.label || obj?.title || "";
141
147
  return (
142
- <div style={{ width: '100%', whiteSpace: 'pre-wrap' }}>
143
- {typeof title === 'string' ? title : title?.()}
148
+ <div style={{ width: "100%", whiteSpace: "pre-wrap" }}>
149
+ {typeof title === "string" ? title : title?.()}
144
150
  </div>
145
- )
151
+ );
146
152
  },
147
- }
153
+ };
148
154
 
149
155
  if (obj?.sorter) {
150
156
  v.renderSorterIcon = ({ order }: { order: string }) => {
151
- const { Icon, title } = orderEnum[order as keyof typeof orderEnum]
157
+ const { Icon, title } = orderEnum[order as keyof typeof orderEnum];
152
158
  return (
153
159
  <NTooltip>
154
160
  {{
@@ -156,62 +162,62 @@ function init(): void {
156
162
  default: () => title,
157
163
  }}
158
164
  </NTooltip>
159
- )
160
- }
165
+ );
166
+ };
161
167
  v.sorter = {
162
168
  multiple: 1,
163
- fn: obj.sorter
164
- }
169
+ fn: obj.sorter,
170
+ };
165
171
  }
166
172
 
167
173
  scrollNum += v?.width
168
174
  ? Number.parseInt(String(v?.width))
169
- : (typeof v?.title === 'string' ? v?.title?.length * 30 : 0) || 0
175
+ : (typeof v?.title === "string" ? v?.title?.length * 30 : 0) || 0;
170
176
 
171
- o.push(v)
172
- return o
173
- }, [])
174
- scrollX.value = scrollNum
175
- console.log('计算')
177
+ o.push(v);
178
+ return o;
179
+ }, []);
180
+ scrollX.value = scrollNum;
181
+ console.log("计算");
176
182
  }
177
183
 
178
184
  function filterButton(): any {
179
185
  return (
180
186
  <NButton type="info" onClick={() => filterHandle()}>
181
187
  {{
182
- default: () => '筛选字段',
188
+ default: () => "筛选字段",
183
189
  icon: () => <Funnel />,
184
190
  }}
185
191
  </NButton>
186
- )
192
+ );
187
193
  }
188
194
 
189
195
  function filterHandle(): void {
190
196
  const { cancel } = commonDialogMethod(
191
197
  {
192
- title: '筛选字段',
198
+ title: "筛选字段",
193
199
  read: true,
194
200
  options: [
195
201
  {
196
- key: 'filter-dialog',
202
+ key: "filter-dialog",
197
203
  render: () => (
198
204
  <FilterDialog
199
205
  style={{
200
- width: '100%',
201
- margin: '0',
206
+ width: "100%",
207
+ margin: "0",
202
208
  padding: 0,
203
209
  }}
204
210
  filterItem={unref(props.columns || [])}
205
211
  selectItem={headDefault.value}
206
212
  defaultItem={props.defaultColumns}
207
213
  onSubmit={(v: string[]) => {
208
- const routePath = getRoutePath()
214
+ const routePath = getRoutePath();
209
215
  if (routePath) {
210
- getFilterAll.value[routePath] = v
211
- setHeadFilter(getFilterAll.value)
216
+ getFilterAll.value[routePath] = v;
217
+ setHeadFilter(getFilterAll.value);
212
218
  }
213
- init()
214
- cancel()
219
+ init();
220
+ cancel();
215
221
  }}
216
222
  />
217
223
  ),
@@ -221,46 +227,45 @@ function filterHandle(): void {
221
227
  {
222
228
  closable: false,
223
229
  style: {
224
- width: '500px',
230
+ width: "500px",
225
231
  },
226
- },
227
- )
232
+ }
233
+ );
228
234
  }
229
235
 
230
236
  function onSorter(e: any): void {
231
- console.log('onSorter', e)
232
- if (!e)
233
- return
234
- const sortArr = toArray(e)
237
+ console.log("onSorter", e);
238
+ if (!e) return;
239
+ const sortArr = toArray(e);
235
240
 
236
241
  sortArr.forEach((v: any) => {
237
- console.log('v', v)
238
-
242
+ console.log("v", v);
243
+
239
244
  if (v?.sorter) {
240
245
  v?.sorter?.fn?.(
241
246
  (listQuery: any, pageState: any, key: string) => {
242
- orderEnum[v.order as keyof typeof orderEnum]?.fn(listQuery, key)
243
- pageState.fetchData()
247
+ orderEnum[v.order as keyof typeof orderEnum]?.fn(listQuery, key);
248
+ pageState.fetchData();
244
249
  },
245
250
  {
246
- field: v?.columnKey,
251
+ field: v?.columnKey,
247
252
  value: orderEnum[v?.order as keyof typeof orderEnum]?.value,
248
- isClick: !isProxy(v)
253
+ isClick: !isProxy(v),
249
254
  }
250
- )
255
+ );
251
256
  }
252
- })
257
+ });
253
258
 
254
- emit('sorted')
259
+ emit("sorted");
255
260
  }
256
261
 
257
262
  defineExpose<DataTableExpose>({
258
263
  filterHandle,
259
264
  filterButton,
260
265
  initColumns: init,
261
- })
266
+ });
262
267
 
263
- onMounted(() => {})
268
+ onMounted(() => {});
264
269
  </script>
265
270
 
266
271
  <template>
@@ -282,7 +287,7 @@ onMounted(() => {})
282
287
  <!-- props.data.length > 30 -->
283
288
  <template #empty>
284
289
  <slot name="empty">
285
- <n-empty>{{ emptyText }}</n-empty>
290
+ <NEmpty>{{ emptyText }}</NEmpty>
286
291
  </slot>
287
292
  </template>
288
293
  </NDataTable>
@@ -1,11 +1,17 @@
1
1
  <script setup>
2
- import { useThemeVars } from 'naive-ui'
3
- import { ref, watchEffect } from 'vue'
2
+ import {
3
+ useThemeVars,
4
+ NCheckboxGroup,
5
+ NCheckbox,
6
+ NButton,
7
+ NEllipsis,
8
+ } from "naive-ui";
9
+ import { ref, watchEffect } from "vue";
4
10
 
5
11
  const props = defineProps({
6
12
  type: {
7
13
  type: String,
8
- default: () => 'primary',
14
+ default: () => "primary",
9
15
  },
10
16
  filterItem: {
11
17
  type: Array,
@@ -21,98 +27,97 @@ const props = defineProps({
21
27
  },
22
28
  filterButtonKey: {
23
29
  type: Array,
24
- default: () => ['all', 'selectDefault'],
30
+ default: () => ["all", "selectDefault"],
25
31
  // default: () => ['all', 'selectDefault','currentSelect']
26
32
  },
27
- })
28
- const emit = defineEmits(['submit'])
29
- const theme = useThemeVars()
30
- const keySelect = ref([])
33
+ });
34
+ const emit = defineEmits(["submit"]);
35
+ const theme = useThemeVars();
36
+ const keySelect = ref([]);
31
37
 
32
38
  watchEffect(() => {
33
- keySelect.value = props.selectItem
34
- })
35
- const filterButton = ref([])
39
+ keySelect.value = props.selectItem;
40
+ });
41
+ const filterButton = ref([]);
36
42
  const filterOprButton = [
37
43
  {
38
- key: 'all',
39
- label: '全选',
40
- onCheck: () => (keySelect.value = props.filterItem?.map(v => v.key)),
44
+ key: "all",
45
+ label: "全选",
46
+ onCheck: () => (keySelect.value = props.filterItem?.map((v) => v.key)),
41
47
  unCheck: () => (keySelect.value = []),
42
48
  },
43
49
  {
44
- key: 'selectDefault',
45
- label: '选中默认',
50
+ key: "selectDefault",
51
+ label: "选中默认",
46
52
  onCheck: () => (keySelect.value = props.defaultItem),
47
53
  unCheck: () => (keySelect.value = props.selectItem),
48
54
  },
49
55
  {
50
- key: 'currentSelect',
51
- label: '选中当前列表',
56
+ key: "currentSelect",
57
+ label: "选中当前列表",
52
58
  onCheck: () => (keySelect.value = props.selectItem),
53
59
  unCheck: () => (keySelect.value = props.selectItem),
54
60
  },
55
- ]
61
+ ];
56
62
  watchEffect(() => {
57
- filterButton.value = filterOprButton.filter(v =>
58
- props.filterButtonKey.includes(v.key),
59
- )
60
- console.log(props.filterButtonKey)
61
- })
63
+ filterButton.value = filterOprButton.filter((v) =>
64
+ props.filterButtonKey.includes(v.key)
65
+ );
66
+ console.log(props.filterButtonKey);
67
+ });
62
68
 
63
69
  function onSubmit() {
64
- emit('submit', keySelect.value)
70
+ emit("submit", keySelect.value);
65
71
  }
66
72
 
67
73
  const checkKey = ref(
68
- props.filterItem?.length === props.selectItem?.length ? ['all'] : [],
69
- )
74
+ props.filterItem?.length === props.selectItem?.length ? ["all"] : []
75
+ );
70
76
 
71
77
  function onCheckModel(string, meta) {
72
- console.log(string)
73
- console.log(meta)
74
- if (meta.actionType === 'uncheck') {
75
- filterButton.value?.find(v => v?.key === meta.value)?.unCheck()
76
- checkKey.value = []
77
- }
78
- else {
79
- filterButton.value?.find(v => v?.key === meta.value)?.onCheck()
80
- checkKey.value = [meta.value]
78
+ console.log(string);
79
+ console.log(meta);
80
+ if (meta.actionType === "uncheck") {
81
+ filterButton.value?.find((v) => v?.key === meta.value)?.unCheck();
82
+ checkKey.value = [];
83
+ } else {
84
+ filterButton.value?.find((v) => v?.key === meta.value)?.onCheck();
85
+ checkKey.value = [meta.value];
81
86
  }
82
87
  }
83
88
  </script>
84
89
 
85
90
  <template>
86
91
  <div class="filter-box">
87
- <n-checkbox-group v-model:value="keySelect" class="filter-main">
88
- <n-checkbox
92
+ <NCheckboxGroup v-model:value="keySelect" class="filter-main">
93
+ <NCheckbox
89
94
  v-for="item in filterItem"
90
95
  :key="item.key"
91
96
  :value="item.key"
92
97
  class="filter-item"
93
98
  >
94
- <n-ellipsis style="max-width: 100px">
99
+ <NEllipsis style="max-width: 100px">
95
100
  {{ item.title }}
96
- </n-ellipsis>
97
- </n-checkbox>
98
- </n-checkbox-group>
101
+ </NEllipsis>
102
+ </NCheckbox>
103
+ </NCheckboxGroup>
99
104
  <div class="filter-footer">
100
- <n-checkbox-group
105
+ <NCheckboxGroup
101
106
  :type="props.type"
102
107
  :value="checkKey"
103
108
  @update:value="onCheckModel"
104
109
  >
105
- <n-checkbox
110
+ <NCheckbox
106
111
  v-for="item in filterButton"
107
112
  :key="item.key"
108
113
  :value="item.key"
109
114
  >
110
115
  {{ item.label }}
111
- </n-checkbox>
112
- </n-checkbox-group>
113
- <n-button class="submit-btn" :type="props.type" @click="onSubmit">
116
+ </NCheckbox>
117
+ </NCheckboxGroup>
118
+ <NButton class="submit-btn" :type="props.type" @click="onSubmit">
114
119
  保存
115
- </n-button>
120
+ </NButton>
116
121
  </div>
117
122
  </div>
118
123
  </template>
@@ -14,6 +14,7 @@ interface ConfigType {
14
14
  instance: any
15
15
  inheritTheme: boolean
16
16
  themeOverrides: any
17
+ defaultOption: any
17
18
  }
18
19
  }
19
20
 
@@ -38,6 +39,8 @@ const config: ConfigType = {
38
39
  inheritTheme: true,
39
40
  // 主题色覆盖(当 inheritTheme 为 false 时使用)
40
41
  themeOverrides: null,
42
+ // Dialog 默认配置项
43
+ defaultOption: null,
41
44
  },
42
45
  }
43
46
 
@@ -50,9 +53,10 @@ const config: ConfigType = {
50
53
  * @param {Object} [options.dialog] - Dialog 配置
51
54
  * @param {boolean} [options.dialog.inheritTheme] - 是否继承外部定义的主题色(默认 true)
52
55
  * @param {Object} [options.dialog.themeOverrides] - 主题色覆盖(当 inheritTheme 为 false 时使用)
56
+ * @param {Object} [options.dialog.defaultOption] - commonDialogMethod 的默认选项配置
53
57
  * @example
54
58
  * import { setupConfig } from '@xmszm/core'
55
- *
59
+ *
56
60
  * setupConfig({
57
61
  * baseURL: 'https://api.example.com',
58
62
  * hasPermission: (permission) => {
@@ -64,6 +68,13 @@ const config: ConfigType = {
64
68
  * inheritTheme: false, // 不继承外部主题色
65
69
  * themeOverrides: {
66
70
  * // 自定义主题色
71
+ * },
72
+ * defaultOption: {
73
+ * // commonDialogMethod 的默认配置
74
+ * showIcon: false,
75
+ * autoFocus: false,
76
+ * closable: true,
77
+ * closeOnEsc: true,
67
78
  * }
68
79
  * }
69
80
  * })
@@ -100,6 +111,9 @@ export function setupConfig(options: Partial<ConfigType> = {}) {
100
111
  if (options.dialog.themeOverrides !== undefined) {
101
112
  config.dialog.themeOverrides = options.dialog.themeOverrides
102
113
  }
114
+ if (options.dialog.defaultOption !== undefined) {
115
+ config.dialog.defaultOption = options.dialog.defaultOption
116
+ }
103
117
  }
104
118
 
105
119
  return config