@smartos-lib/components 1.7.0 → 1.8.0-beta.0

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@smartos-lib/components",
3
3
  "type": "module",
4
- "version": "1.7.0",
4
+ "version": "1.8.0-beta.0",
5
5
  "packageManager": "pnpm@8.14.3",
6
6
  "author": "Wei Zhang <https://github.com/Zhang-Wei-666>",
7
7
  "devDependencies": {
@@ -1,58 +1,5 @@
1
- import type { GlobalThemeOverrides } from 'naive-ui';
2
1
  import { darkTheme, dateZhCN, zhCN } from 'naive-ui';
3
- import { deepMerge } from 'mixte';
4
- import { colors, fontFamily } from '@/shared/unocss.theme';
5
-
6
- /** 通用主题变量覆盖 */
7
- export const commonOverrides: GlobalThemeOverrides = {
8
- common: {
9
- // 默认字体
10
- fontFamily: fontFamily.sans,
11
- // 等宽字体
12
- fontFamilyMono: fontFamily.mono,
13
-
14
- // Primary 颜色
15
- primaryColor: colors.primary,
16
- primaryColorHover: colors['primary-hover'],
17
- primaryColorPressed: colors['primary-active'],
18
- primaryColorSuppl: colors.primary,
19
- // Info 颜色
20
- infoColor: colors.info,
21
- infoColorHover: colors['info-hover'],
22
- infoColorPressed: colors['info-active'],
23
- infoColorSuppl: colors.info,
24
- // Success 颜色
25
- successColor: colors.success,
26
- successColorHover: colors['success-hover'],
27
- successColorPressed: colors['success-active'],
28
- successColorSuppl: colors.success,
29
- // Warning 颜色
30
- warningColor: colors.warning,
31
- warningColorHover: colors['warning-hover'],
32
- warningColorPressed: colors['warning-active'],
33
- warningColorSuppl: colors.warning,
34
- // Error 颜色
35
- errorColor: colors.error,
36
- errorColorHover: colors['error-hover'],
37
- errorColorPressed: colors['error-active'],
38
- errorColorSuppl: colors.error,
39
- },
40
- };
41
-
42
- /** 亮色主题变量覆盖 */
43
- export const lightThemeOverrides: GlobalThemeOverrides = deepMerge({}, commonOverrides, {
44
-
45
- });
46
- /** 深色主题变量覆盖 */
47
- export const darkThemeOverrides: GlobalThemeOverrides = deepMerge({}, commonOverrides, {
48
- Button: {
49
- textColorPrimary: '#FFF',
50
- textColorHoverPrimary: '#FFF',
51
- textColorPressedPrimary: '#FFF',
52
- textColorFocusPrimary: '#FFF',
53
- textColorDisabledPrimary: '#FFF',
54
- },
55
- });
2
+ import { darkThemeOverrides, lightThemeOverrides } from '@/shared/naiveThemeOverrides';
56
3
 
57
4
  export const useNaiveTheme = createSharedComposable(() => {
58
5
  const themeStore = useThemeStore();
@@ -0,0 +1,54 @@
1
+ import type { GlobalThemeOverrides } from 'naive-ui';
2
+ import { deepMerge } from 'mixte';
3
+ import { colors, fontFamily } from './unocss.theme';
4
+
5
+ /** 通用主题变量覆盖 */
6
+ export const commonOverrides: GlobalThemeOverrides = {
7
+ common: {
8
+ // 默认字体
9
+ fontFamily: fontFamily.sans,
10
+ // 等宽字体
11
+ fontFamilyMono: fontFamily.mono,
12
+
13
+ // Primary 颜色
14
+ primaryColor: colors.primary,
15
+ primaryColorHover: colors['primary-hover'],
16
+ primaryColorPressed: colors['primary-active'],
17
+ primaryColorSuppl: colors.primary,
18
+ // Info 颜色
19
+ infoColor: colors.info,
20
+ infoColorHover: colors['info-hover'],
21
+ infoColorPressed: colors['info-active'],
22
+ infoColorSuppl: colors.info,
23
+ // Success 颜色
24
+ successColor: colors.success,
25
+ successColorHover: colors['success-hover'],
26
+ successColorPressed: colors['success-active'],
27
+ successColorSuppl: colors.success,
28
+ // Warning 颜色
29
+ warningColor: colors.warning,
30
+ warningColorHover: colors['warning-hover'],
31
+ warningColorPressed: colors['warning-active'],
32
+ warningColorSuppl: colors.warning,
33
+ // Error 颜色
34
+ errorColor: colors.error,
35
+ errorColorHover: colors['error-hover'],
36
+ errorColorPressed: colors['error-active'],
37
+ errorColorSuppl: colors.error,
38
+ },
39
+ };
40
+
41
+ /** 亮色主题变量覆盖 */
42
+ export const lightThemeOverrides: GlobalThemeOverrides = deepMerge({}, commonOverrides, {
43
+
44
+ });
45
+ /** 深色主题变量覆盖 */
46
+ export const darkThemeOverrides: GlobalThemeOverrides = deepMerge({}, commonOverrides, {
47
+ Button: {
48
+ textColorPrimary: '#FFF',
49
+ textColorHoverPrimary: '#FFF',
50
+ textColorPressedPrimary: '#FFF',
51
+ textColorFocusPrimary: '#FFF',
52
+ textColorDisabledPrimary: '#FFF',
53
+ },
54
+ });
@@ -1,5 +1,38 @@
1
+ import type { NUpload, UploadFileInfo } from 'naive-ui';
2
+ import { defineCustomElement } from 'vue';
1
3
  import VueComponent from './index.vue';
2
4
  import { tag } from './info';
3
5
  import { defineCustomElements } from '@/utils/defineCustomElements';
6
+ import { getCustomElementExposed } from '@/utils/callCustomElementExposed';
4
7
 
5
- defineCustomElements(tag, VueComponent);
8
+ type UploadRef = InstanceType<typeof NUpload>;
9
+
10
+ // 组件转为 Vue 自定义元素
11
+ const VueCustomElement = defineCustomElement(VueComponent);
12
+
13
+ // 暴露组件属性/方法
14
+ Object.assign(VueCustomElement.prototype, {
15
+ getUploadRef() {
16
+ return (getCustomElementExposed(this, 'uploadRef') as Ref<UploadRef>).value;
17
+ },
18
+ getFileList() {
19
+ return (getCustomElementExposed(this, 'fileList') as Ref<UploadFileInfo[]>).value;
20
+ },
21
+ submit() {
22
+ return this.getUploadRef().submit();
23
+ },
24
+ clear() {
25
+ return this.getUploadRef().clear();
26
+ },
27
+ });
28
+
29
+ // 注册组件
30
+ defineCustomElements(tag, VueCustomElement);
31
+
32
+ // 暴露组件类型
33
+ export type SmartUploadElement = typeof VueCustomElement & {
34
+ getUploadRef: () => UploadRef
35
+ getFileList: () => UploadFileInfo[]
36
+ submit: UploadRef['submit']
37
+ clear: UploadRef['clear']
38
+ };
@@ -1,7 +1,8 @@
1
1
  <template>
2
2
  <NConfigProvider :cls-prefix="classPrefix" :theme-overrides="commonOverrides" :locale="zhCN" abstract>
3
3
  <NUpload
4
- v-bind="props"
4
+ ref="uploadRef"
5
+ v-bind="bindProps"
5
6
  v-model:file-list="fileList"
6
7
  :custom-request="customRequest"
7
8
  @remove="onRemoved"
@@ -19,14 +20,21 @@
19
20
  import { NButton, NConfigProvider, NUpload, zhCN } from 'naive-ui';
20
21
  import { getConfigProvider } from '@smartos-lib/core';
21
22
  import type { Props } from './types';
23
+ import { commonOverrides } from '@/shared/naiveThemeOverrides';
22
24
 
23
25
  const props = defineProps<Props>();
24
26
  const list = defineModel<UploadFieldValueItem[]>('fileList', {
25
27
  default: () => [],
26
28
  });
27
29
 
30
+ const uploadRef = ref<InstanceType<typeof NUpload>>();
31
+
28
32
  const { classPrefix } = useSmart();
29
33
 
34
+ const bindProps = reactiveOmit(props, [
35
+ 'beforeUpload',
36
+ ]);
37
+
30
38
  /** 内部维护的文件列表 */
31
39
  const fileList = ref<UploadFileInfo[]>(
32
40
  list.value.map(({ fileName: url }) => ({ id: url, name: url, url, status: 'finished' })),
@@ -35,11 +43,12 @@
35
43
  /** 自定义上传 */
36
44
  function customRequest({ file, onFinish, onError, onProgress }: UploadCustomRequestOptions) {
37
45
  const originFile = file.file as File;
46
+ const beforeUpload = props.beforeUpload ?? (c => c);
38
47
 
39
48
  const form = new FormData();
40
49
  form.set('file', originFile);
41
50
 
42
- getConfigProvider('request')!({
51
+ const config = beforeUpload({
43
52
  method: 'post',
44
53
  url: '/admin/sys-file/upload',
45
54
  data: form,
@@ -49,14 +58,21 @@
49
58
  onUploadProgress({ loaded, total }) {
50
59
  onProgress({ percent: total ? Math.ceil((loaded / total) * 100) : 0 });
51
60
  },
52
- })
61
+ });
62
+
63
+ getConfigProvider('request')!(config)
53
64
  .then((res) => {
54
65
  const data = res.data.data;
55
66
 
56
67
  file.url = data.url;
68
+ file.status = 'finished';
57
69
  onFinish();
58
70
 
59
71
  nextTick(() => {
72
+ // fix: 手动上传完成时, fileList 中的文件信息不会更新
73
+ if (!props.defaultUpload)
74
+ fileList.value.splice(fileList.value.findIndex(({ id }) => id === file.id), 1, file);
75
+
60
76
  list.value.includes(data.url) || list.value.push({ fileName: data.url, originalFileName: originFile.name });
61
77
  });
62
78
  })
@@ -98,4 +114,9 @@
98
114
  i++;
99
115
  }
100
116
  });
117
+
118
+ defineExpose({
119
+ uploadRef,
120
+ fileList,
121
+ });
101
122
  </script>
@@ -1,6 +1,12 @@
1
+ import type { AxiosRequestConfig } from 'axios';
1
2
  import type { UploadProps } from 'naive-ui';
2
3
 
3
4
  export interface Props {
5
+ /** 发起请求前的钩子, 用于修改请求配置 */
6
+ beforeUpload?: <T extends AxiosRequestConfig>(config: T) => T
7
+
8
+ /* ------------------ 以下为 NaiveUI 上传组件原有 Props ------------------ */
9
+
4
10
  accept?: UploadProps['accept']
5
11
  defaultFileList?: UploadProps['defaultFileList']
6
12
  defaultUpload?: UploadProps['defaultUpload']
@@ -25,4 +31,6 @@ export interface Props {
25
31
  showTrigger?: UploadProps['showTrigger']
26
32
  triggerClass?: UploadProps['triggerClass']
27
33
  triggerStyle?: UploadProps['triggerStyle']
34
+ onError?: UploadProps['onError']
35
+ onFinish?: UploadProps['onFinish']
28
36
  };
@@ -8,7 +8,6 @@ declare global {
8
8
  const EffectScope: typeof import('vue')['EffectScope']
9
9
  const asyncComputed: typeof import('@vueuse/core')['asyncComputed']
10
10
  const autoResetRef: typeof import('@vueuse/core')['autoResetRef']
11
- const commonOverrides: typeof import('../src/composables/useNaiveTheme')['commonOverrides']
12
11
  const computed: typeof import('vue')['computed']
13
12
  const computedAsync: typeof import('@vueuse/core')['computedAsync']
14
13
  const computedEager: typeof import('@vueuse/core')['computedEager']
@@ -29,7 +28,6 @@ declare global {
29
28
  const createTemplatePromise: typeof import('@vueuse/core')['createTemplatePromise']
30
29
  const createUnrefFn: typeof import('@vueuse/core')['createUnrefFn']
31
30
  const customRef: typeof import('vue')['customRef']
32
- const darkThemeOverrides: typeof import('../src/composables/useNaiveTheme')['darkThemeOverrides']
33
31
  const debouncedRef: typeof import('@vueuse/core')['debouncedRef']
34
32
  const debouncedWatch: typeof import('@vueuse/core')['debouncedWatch']
35
33
  const deepUnref: typeof import('@mixte/use')['deepUnref']
@@ -49,7 +47,6 @@ declare global {
49
47
  const isReactive: typeof import('vue')['isReactive']
50
48
  const isReadonly: typeof import('vue')['isReadonly']
51
49
  const isRef: typeof import('vue')['isRef']
52
- const lightThemeOverrides: typeof import('../src/composables/useNaiveTheme')['lightThemeOverrides']
53
50
  const logicAnd: typeof import('@vueuse/math')['logicAnd']
54
51
  const logicNot: typeof import('@vueuse/math')['logicNot']
55
52
  const logicOr: typeof import('@vueuse/math')['logicOr']
@@ -336,7 +333,6 @@ declare module 'vue' {
336
333
  readonly EffectScope: UnwrapRef<typeof import('vue')['EffectScope']>
337
334
  readonly asyncComputed: UnwrapRef<typeof import('@vueuse/core')['asyncComputed']>
338
335
  readonly autoResetRef: UnwrapRef<typeof import('@vueuse/core')['autoResetRef']>
339
- readonly commonOverrides: UnwrapRef<typeof import('../src/composables/useNaiveTheme')['commonOverrides']>
340
336
  readonly computed: UnwrapRef<typeof import('vue')['computed']>
341
337
  readonly computedAsync: UnwrapRef<typeof import('@vueuse/core')['computedAsync']>
342
338
  readonly computedEager: UnwrapRef<typeof import('@vueuse/core')['computedEager']>
@@ -357,7 +353,6 @@ declare module 'vue' {
357
353
  readonly createTemplatePromise: UnwrapRef<typeof import('@vueuse/core')['createTemplatePromise']>
358
354
  readonly createUnrefFn: UnwrapRef<typeof import('@vueuse/core')['createUnrefFn']>
359
355
  readonly customRef: UnwrapRef<typeof import('vue')['customRef']>
360
- readonly darkThemeOverrides: UnwrapRef<typeof import('../src/composables/useNaiveTheme')['darkThemeOverrides']>
361
356
  readonly debouncedRef: UnwrapRef<typeof import('@vueuse/core')['debouncedRef']>
362
357
  readonly debouncedWatch: UnwrapRef<typeof import('@vueuse/core')['debouncedWatch']>
363
358
  readonly deepUnref: UnwrapRef<typeof import('@mixte/use')['deepUnref']>
@@ -377,7 +372,6 @@ declare module 'vue' {
377
372
  readonly isReactive: UnwrapRef<typeof import('vue')['isReactive']>
378
373
  readonly isReadonly: UnwrapRef<typeof import('vue')['isReadonly']>
379
374
  readonly isRef: UnwrapRef<typeof import('vue')['isRef']>
380
- readonly lightThemeOverrides: UnwrapRef<typeof import('../src/composables/useNaiveTheme')['lightThemeOverrides']>
381
375
  readonly logicAnd: UnwrapRef<typeof import('@vueuse/math')['logicAnd']>
382
376
  readonly logicNot: UnwrapRef<typeof import('@vueuse/math')['logicNot']>
383
377
  readonly logicOr: UnwrapRef<typeof import('@vueuse/math')['logicOr']>
@@ -657,7 +651,6 @@ declare module '@vue/runtime-core' {
657
651
  readonly EffectScope: UnwrapRef<typeof import('vue')['EffectScope']>
658
652
  readonly asyncComputed: UnwrapRef<typeof import('@vueuse/core')['asyncComputed']>
659
653
  readonly autoResetRef: UnwrapRef<typeof import('@vueuse/core')['autoResetRef']>
660
- readonly commonOverrides: UnwrapRef<typeof import('../src/composables/useNaiveTheme')['commonOverrides']>
661
654
  readonly computed: UnwrapRef<typeof import('vue')['computed']>
662
655
  readonly computedAsync: UnwrapRef<typeof import('@vueuse/core')['computedAsync']>
663
656
  readonly computedEager: UnwrapRef<typeof import('@vueuse/core')['computedEager']>
@@ -678,7 +671,6 @@ declare module '@vue/runtime-core' {
678
671
  readonly createTemplatePromise: UnwrapRef<typeof import('@vueuse/core')['createTemplatePromise']>
679
672
  readonly createUnrefFn: UnwrapRef<typeof import('@vueuse/core')['createUnrefFn']>
680
673
  readonly customRef: UnwrapRef<typeof import('vue')['customRef']>
681
- readonly darkThemeOverrides: UnwrapRef<typeof import('../src/composables/useNaiveTheme')['darkThemeOverrides']>
682
674
  readonly debouncedRef: UnwrapRef<typeof import('@vueuse/core')['debouncedRef']>
683
675
  readonly debouncedWatch: UnwrapRef<typeof import('@vueuse/core')['debouncedWatch']>
684
676
  readonly deepUnref: UnwrapRef<typeof import('@mixte/use')['deepUnref']>
@@ -698,7 +690,6 @@ declare module '@vue/runtime-core' {
698
690
  readonly isReactive: UnwrapRef<typeof import('vue')['isReactive']>
699
691
  readonly isReadonly: UnwrapRef<typeof import('vue')['isReadonly']>
700
692
  readonly isRef: UnwrapRef<typeof import('vue')['isRef']>
701
- readonly lightThemeOverrides: UnwrapRef<typeof import('../src/composables/useNaiveTheme')['lightThemeOverrides']>
702
693
  readonly logicAnd: UnwrapRef<typeof import('@vueuse/math')['logicAnd']>
703
694
  readonly logicNot: UnwrapRef<typeof import('@vueuse/math')['logicNot']>
704
695
  readonly logicOr: UnwrapRef<typeof import('@vueuse/math')['logicOr']>