Skip to content

feat(modal): add useModal #6517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
chore(modal): update modal type
  • Loading branch information
CCherry07 committed Apr 29, 2023
commit fd582496a623a92fffd12da3a5865bcb3908138b
2 changes: 1 addition & 1 deletion components/modal/confirm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { getConfirmLocale } from './locale';
import destroyFns from './destroyFns';

type ConfigUpdate = ModalFuncProps | ((prevConfig: ModalFuncProps) => ModalFuncProps);
export type ModalStaticFunctions = Record<NonNullable<ModalFuncProps['type']>, ModalFunc>;
export type ModalStaticFunctions<T = ModalFunc> = Record<NonNullable<ModalFuncProps['type']>, T>;

export type ModalFunc = (props: ModalFuncProps) => {
destroy: () => void;
Expand Down
32 changes: 21 additions & 11 deletions components/modal/useModal/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ComputedRef } from 'vue';
import { unref, computed, defineComponent, ref, watch } from 'vue';
import type { ComputedRef, Ref } from 'vue';
import { isRef, unref, computed, defineComponent, ref, watch } from 'vue';
import type { VueNode } from '../../_util/type';
import type { ModalFuncProps } from '../Modal';
import type { HookModalRef } from './HookModal';
Expand Down Expand Up @@ -33,8 +33,15 @@ const ElementsHolder = defineComponent({
};
},
});

function useModal(): readonly [Omit<ModalStaticFunctions, 'warn'>, () => VueNode] {
export type ModalFuncWithRef = (props: Ref<ModalFuncProps> | ModalFuncProps) => {
destroy: () => void;
update: (configUpdate: ModalFuncProps) => void;
};

function useModal(): readonly [
Omit<ModalStaticFunctions<ModalFuncWithRef>, 'warn'>,
() => VueNode,
] {
const holderRef = ref<ElementsHolderRef>(null);
// ========================== Effect ==========================
const actionQueue = ref([]);
Expand All @@ -56,18 +63,21 @@ function useModal(): readonly [Omit<ModalStaticFunctions, 'warn'>, () => VueNode

// =========================== Hook ===========================
const getConfirmFunc = (withFunc: (config: ModalFuncProps) => ModalFuncProps) =>
function hookConfirm(config: ModalFuncProps) {
function hookConfirm(config: Ref<ModalFuncProps> | ModalFuncProps) {
uuid += 1;
const open = ref(true);
const modalRef = ref<HookModalRef>(null);
const configRef = ref(unref(config));
const updateConfig = ref({});
watch(config, val => {
updateAction({
...val,
...updateConfig.value,
});
});
watch(
() => config,
val => {
updateAction({
...(isRef(val) ? val.value : val),
...updateConfig.value,
});
},
);
// eslint-disable-next-line prefer-const
let closeFunc: Function | undefined;
const modal = computed(() => (
Expand Down