@peng_kai/kit 0.2.41 → 0.2.43

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.
@@ -5,9 +5,8 @@
5
5
 
6
6
  import type { FormInstance, FormItemProps, RuleObject } from 'ant-design-vue/es/form';
7
7
  import type { FormProps } from 'ant-design-vue';
8
- import { type MaybeRefOrGetter, computed, reactive, ref, shallowRef, toRef, watch, watchEffect } from 'vue';
9
- import { cloneDeep, mapValues } from 'lodash-es';
10
- import { reactiveComputed } from '@vueuse/core';
8
+ import { type MaybeRefOrGetter, computed, reactive, ref, toRef, watch, watchEffect } from 'vue';
9
+ import { mapValues } from 'lodash-es';
11
10
  import { GROUP_SEP, buildGroupField, getGroupIndex } from './useAntdForm.helpers';
12
11
 
13
12
  export { useAntdForm };
@@ -29,7 +28,7 @@ interface Options<S, TS> {
29
28
  function useAntdForm<S extends Record<string, unknown>, TS = S>(schemas: MaybeRefOrGetter<SchemaConfig<S>>, options?: Options<S, TS>) {
30
29
  const schemasR = toRef(schemas);
31
30
 
32
- const state = shallowRef({} as S);
31
+ const state = ref({} as S);
33
32
  const stateTF = computed<TS>(() => options?.transform?.(<any>state.value) ?? <any>state.value);
34
33
  const show = computed(() => mapValues(state.value, (_, k): boolean => schemasR.value[k].show?.(state.value) ?? true));
35
34
  const props = computed<FormProps>(() => ({ model: state.value, ref: (c: any) => (formRef.value = c) }));
@@ -50,16 +49,15 @@ function useAntdForm<S extends Record<string, unknown>, TS = S>(schemas: MaybeRe
50
49
  get scrollToField() {
51
50
  return formRef.value?.scrollToField;
52
51
  },
52
+ get validate() {
53
+ return formRef.value?.validate;
54
+ },
53
55
  get validateFields() {
54
56
  return formRef.value?.validateFields;
55
57
  },
56
58
  get clearValidate() {
57
59
  return formRef.value?.clearValidate;
58
60
  },
59
- async validate(...args: Parameters<FormInstance['validate']>) {
60
- const res = await formRef.value?.validate(...args);
61
- return cloneDeep(reactive(res ?? {}));
62
- },
63
61
  addGroup(groupName: string, groupSchemas: Record<string, ItemSchema>) {
64
62
  const i = getGroupIndex(groupName, schemasR.value) + 1;
65
63
 
@@ -98,5 +96,17 @@ function useAntdForm<S extends Record<string, unknown>, TS = S>(schemas: MaybeRe
98
96
  state.value = newState;
99
97
  }, { flush: 'pre' });
100
98
 
101
- return reactive({ state: reactiveComputed(() => state.value as S), stateTF, show, props, itemProps, $form });
99
+ // 为什么之前用 watchEffect?
100
+ // watch(schemasR, () => {
101
+ // const _state: any = {};
102
+
103
+ // for (const k in schemasR.value) {
104
+ // const item = schemasR.value[k];
105
+ // _state[k] = (k in state.value) ? toRef(state.value, k) : toRef(item, 'value');
106
+ // }
107
+
108
+ // state.value = _state;
109
+ // }, { immediate: true });
110
+
111
+ return reactive({ state, stateTF, show, props, itemProps, $form });
102
112
  }
@@ -1,12 +1,17 @@
1
1
  import { Modal as AntModal } from 'ant-design-vue';
2
- import { createVNode, defineComponent, h, isProxy, reactive, ref, toRef, toRefs, watch } from 'vue';
2
+ import { tryOnUnmounted } from '@vueuse/core';
3
+ import { createVNode, defineComponent, isProxy, reactive, toRef, toRefs } from 'vue';
3
4
  import type { Component } from 'vue';
4
5
  import type { ModalProps } from 'ant-design-vue';
5
- import type { ComponentProps } from 'vue-component-type-helpers';
6
+ import type { ComponentEmit, ComponentProps } from 'vue-component-type-helpers';
6
7
  import type { Writable } from 'type-fest';
7
8
  import { useTemplateRefs } from '../../vue';
8
9
 
9
- const defaultModalProps: ModalProps = { open: false, destroyOnClose: true, wrapClassName: 'antd-cover__basic-modal' };
10
+ interface AntdModalProps extends ModalProps {
11
+ class?: string
12
+ rejectOnClose?: boolean
13
+ [k: string]: any
14
+ }
10
15
 
11
16
  interface IComponentConfig<Comp extends Component> {
12
17
  is: Comp
@@ -14,6 +19,18 @@ interface IComponentConfig<Comp extends Component> {
14
19
  type?: 'modal' | 'body'
15
20
  }
16
21
 
22
+ type ExtractConfirmArgs<T> = T extends {
23
+ (event: 'confirm', ...args: infer U): any
24
+ } ? U extends [infer V] ? V : any : any;
25
+ type UnwrapNonNullable<T> = T extends NonNullable<infer U> ? U : T;
26
+
27
+ const defaultModalProps: AntdModalProps = {
28
+ open: false,
29
+ destroyOnClose: true,
30
+ wrapClassName: 'antd-cover__basic-modal',
31
+ rejectOnClose: false,
32
+ };
33
+
17
34
  /**
18
35
  * AntdModal 组件的辅助 Hook
19
36
  * @param comp 组件配置,也可以直接传入组件,既 comp.is 参数
@@ -31,7 +48,7 @@ export function useAntdModal<Comp extends Component>(
31
48
  const [refs, setRefs] = useTemplateRefs<{
32
49
  comp: typeof _comp.is
33
50
  }>();
34
- const _modalProps: ModalProps = reactive({
51
+ const _modalProps: AntdModalProps = reactive({
35
52
  ...defaultModalProps,
36
53
  ...isProxy(modalProps) ? toRefs(modalProps) : modalProps,
37
54
  confirmLoading: toRef(() => (refs.comp as any)?.loading),
@@ -41,15 +58,37 @@ export function useAntdModal<Comp extends Component>(
41
58
  },
42
59
  });
43
60
  const modalSlotName = _comp.type === 'body' ? 'default' : 'modalRender';
61
+ let promiseResolvers: PromiseWithResolvers<ExtractConfirmArgs<UnwrapNonNullable<ComponentEmit<Comp>>>>;
62
+
63
+ const _onClose = (e?: any) => {
64
+ if (_modalProps.rejectOnClose)
65
+ promiseResolvers.reject(e ?? new Error('Modal Cancel'));
66
+ else
67
+ promiseResolvers.resolve(undefined as any);
68
+ };
69
+
70
+ const open = (
71
+ newCompProps?: Partial<typeof compProps>,
72
+ newAntdModalProps?: Omit<Partial<ModalProps>, 'open'>,
73
+ ) => {
74
+ if (_modalProps.open)
75
+ return Promise.reject(new Error('Modal is already open'));
44
76
 
45
- const open = (newCompProps?: Partial<typeof compProps>, newAntdModalProps?: Omit<Partial<ModalProps>, 'open'>) => {
46
77
  Object.assign(_modalProps, newAntdModalProps);
47
78
  Object.assign(compProps, newCompProps);
48
79
 
80
+ promiseResolvers = Promise.withResolvers();
49
81
  _modalProps.open = true;
82
+
83
+ return promiseResolvers.promise;
50
84
  };
51
- const close = () => {
85
+ const confirm = (ev: any) => {
52
86
  _modalProps.open = false;
87
+ promiseResolvers.resolve(ev);
88
+ };
89
+ const close = (ev: any) => {
90
+ _modalProps.open = false;
91
+ _onClose(ev);
53
92
  };
54
93
 
55
94
  const PresetComponent = defineComponent({
@@ -62,9 +101,13 @@ export function useAntdModal<Comp extends Component>(
62
101
 
63
102
  _modalProps['onUpdate:open'] = (visiable) => {
64
103
  _modalProps.open = visiable;
104
+ !visiable && _onClose();
65
105
  };
66
106
  (compProps as any).ref = setRefs.comp;
67
107
  (compProps as any).onClose = close;
108
+ (compProps as any).onConfirm = confirm;
109
+
110
+ tryOnUnmounted(_onClose);
68
111
 
69
112
  return {
70
113
  PresetComponent,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@peng_kai/kit",
3
3
  "type": "module",
4
- "version": "0.2.41",
4
+ "version": "0.2.43",
5
5
  "description": "",
6
6
  "author": "",
7
7
  "license": "ISC",
@@ -50,6 +50,15 @@ function resetHanlder() {
50
50
  }
51
51
  useResizeObserver(toRef(() => $chartEle.value?.parentElement), resetHanlder);
52
52
  useEventListener(window, 'resize', resetHanlder);
53
+ useEventListener(window, 'click', (ev) => {
54
+ const inst = echartInst.value;
55
+
56
+ // 点击图表外部时,隐藏提示框
57
+ if (inst && !inst.getDom().contains(ev.target as Node)) {
58
+ inst.dispatchAction({ type: 'hideTip' });
59
+ inst.dispatchAction({ type: 'updateAxisPointer', x: -1, y: -1 }); // 移到图表外
60
+ }
61
+ });
53
62
  </script>
54
63
 
55
64
  <template>