提交 a3a903bc 编写于 作者: V vben

feat(modal): exporse redoModalHeight

上级 33b2365f
## Wip ## Wip
### ✨ Features
- useModal 新增返回值函数 `redoModalHeight`,用于在 modal 内为动态内容时刷新 modal 高度
### 🐛 Bug Fixes ### 🐛 Bug Fixes
- 修复 Upload 组件 maxNumber 失效问题 - 修复 Upload 组件 maxNumber 失效问题
......
...@@ -55,6 +55,7 @@ ...@@ -55,6 +55,7 @@
watchEffect, watchEffect,
toRef, toRef,
getCurrentInstance, getCurrentInstance,
nextTick,
} from 'vue'; } from 'vue';
import Modal from './components/Modal'; import Modal from './components/Modal';
...@@ -68,6 +69,7 @@ ...@@ -68,6 +69,7 @@
import { basicProps } from './props'; import { basicProps } from './props';
import { useFullScreen } from './hooks/useModalFullScreen'; import { useFullScreen } from './hooks/useModalFullScreen';
import { omit } from 'lodash-es'; import { omit } from 'lodash-es';
export default defineComponent({ export default defineComponent({
name: 'BasicModal', name: 'BasicModal',
...@@ -79,12 +81,21 @@ ...@@ -79,12 +81,21 @@
const visibleRef = ref(false); const visibleRef = ref(false);
const propsRef = ref<Partial<ModalProps> | null>(null); const propsRef = ref<Partial<ModalProps> | null>(null);
const modalWrapperRef = ref<ComponentRef>(null); const modalWrapperRef = ref<ComponentRef>(null);
// modal Bottom and top height // modal Bottom and top height
const extHeightRef = ref(0); const extHeightRef = ref(0);
const modalMethods: ModalMethods = { const modalMethods: ModalMethods = {
setModalProps, setModalProps,
emitVisible: undefined, emitVisible: undefined,
redoModalHeight: () => {
nextTick(() => {
if (unref(modalWrapperRef)) {
(unref(modalWrapperRef) as any).setModalHeight();
}
});
},
}; };
const instance = getCurrentInstance(); const instance = getCurrentInstance();
if (instance) { if (instance) {
emit('register', modalMethods, instance.uid); emit('register', modalMethods, instance.uid);
...@@ -135,6 +146,11 @@ ...@@ -135,6 +146,11 @@
(v) => { (v) => {
emit('visible-change', v); emit('visible-change', v);
instance && modalMethods.emitVisible?.(v, instance.uid); instance && modalMethods.emitVisible?.(v, instance.uid);
nextTick(() => {
if (props.scrollTop && v && unref(modalWrapperRef)) {
(unref(modalWrapperRef) as any).scrollTop();
}
});
}, },
{ {
immediate: false, immediate: false,
......
...@@ -55,7 +55,7 @@ ...@@ -55,7 +55,7 @@
let stopElResizeFn: Fn = () => {}; let stopElResizeFn: Fn = () => {};
useWindowSizeFn(setModalHeight); useWindowSizeFn(setModalHeight.bind(null, false));
createModalContext({ createModalContext({
redoModalHeight: setModalHeight, redoModalHeight: setModalHeight,
...@@ -97,12 +97,21 @@ ...@@ -97,12 +97,21 @@
stopElResizeFn && stopElResizeFn(); stopElResizeFn && stopElResizeFn();
}); });
async function scrollTop() {
nextTick(() => {
const wrapperRefDom = unref(wrapperRef);
if (!wrapperRefDom) return;
(wrapperRefDom as any)?.scrollTo?.(0);
});
}
async function setModalHeight() { async function setModalHeight() {
// 解决在弹窗关闭的时候监听还存在,导致再次打开弹窗没有高度 // 解决在弹窗关闭的时候监听还存在,导致再次打开弹窗没有高度
// 加上这个,就必须在使用的时候传递父级的visible // 加上这个,就必须在使用的时候传递父级的visible
if (!props.visible) return; if (!props.visible) return;
const wrapperRefDom = unref(wrapperRef); const wrapperRefDom = unref(wrapperRef);
if (!wrapperRefDom) return; if (!wrapperRefDom) return;
const bodyDom = wrapperRefDom.$el.parentElement; const bodyDom = wrapperRefDom.$el.parentElement;
if (!bodyDom) return; if (!bodyDom) return;
bodyDom.style.padding = '0'; bodyDom.style.padding = '0';
...@@ -150,7 +159,7 @@ ...@@ -150,7 +159,7 @@
} }
} }
return { wrapperRef, spinRef, spinStyle }; return { wrapperRef, spinRef, spinStyle, scrollTop, setModalHeight };
}, },
}); });
</script> </script>
...@@ -64,10 +64,15 @@ export function useModal(): UseModalReturnType { ...@@ -64,10 +64,15 @@ export function useModal(): UseModalReturnType {
setModalProps: (props: Partial<ModalProps>): void => { setModalProps: (props: Partial<ModalProps>): void => {
getInstance()?.setModalProps(props); getInstance()?.setModalProps(props);
}, },
getVisible: computed((): boolean => { getVisible: computed((): boolean => {
return visibleData[~~unref(uidRef)]; return visibleData[~~unref(uidRef)];
}), }),
redoModalHeight: () => {
getInstance()?.redoModalHeight?.();
},
openModal: <T = any>(visible = true, data?: T, openOnSet = true): void => { openModal: <T = any>(visible = true, data?: T, openOnSet = true): void => {
getInstance()?.setModalProps({ getInstance()?.setModalProps({
visible: visible, visible: visible,
......
...@@ -8,6 +8,7 @@ const { t } = useI18n(); ...@@ -8,6 +8,7 @@ const { t } = useI18n();
export const modalProps = { export const modalProps = {
visible: propTypes.bool, visible: propTypes.bool,
scrollTop: propTypes.bool.def(true),
height: propTypes.number, height: propTypes.number,
minHeight: propTypes.number, minHeight: propTypes.number,
// open drag // open drag
......
...@@ -6,6 +6,7 @@ import type { CSSProperties, VNodeChild, ComputedRef } from 'vue'; ...@@ -6,6 +6,7 @@ import type { CSSProperties, VNodeChild, ComputedRef } from 'vue';
export interface ModalMethods { export interface ModalMethods {
setModalProps: (props: Partial<ModalProps>) => void; setModalProps: (props: Partial<ModalProps>) => void;
emitVisible?: (visible: boolean, uid: number) => void; emitVisible?: (visible: boolean, uid: number) => void;
redoModalHeight?: () => void;
} }
export type RegisterFn = (modalMethods: ModalMethods, uuid?: string) => void; export type RegisterFn = (modalMethods: ModalMethods, uuid?: string) => void;
...@@ -32,6 +33,7 @@ export interface ModalProps { ...@@ -32,6 +33,7 @@ export interface ModalProps {
// 启用wrapper后 底部可以适当增加高度 // 启用wrapper后 底部可以适当增加高度
wrapperFooterOffset?: number; wrapperFooterOffset?: number;
draggable?: boolean; draggable?: boolean;
scrollTop?: boolean;
// 是否可以进行全屏 // 是否可以进行全屏
canFullscreen?: boolean; canFullscreen?: boolean;
......
...@@ -205,6 +205,7 @@ ...@@ -205,6 +205,7 @@
if (slots.expandedRowRender) { if (slots.expandedRowRender) {
propsData = omit(propsData, 'scroll'); propsData = omit(propsData, 'scroll');
} }
return propsData; return propsData;
}); });
......
...@@ -98,6 +98,7 @@ ...@@ -98,6 +98,7 @@
dept: '', dept: '',
editable: true, editable: true,
isNew: true, isNew: true,
key: `${Date.now()}`,
}; };
data.push(addRow); data.push(addRow);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册