-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathmodal.tsx
More file actions
123 lines (118 loc) · 4.03 KB
/
modal.tsx
File metadata and controls
123 lines (118 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { computed, defineComponent, nextTick, ref, Teleport, toRefs, Transition, watch } from 'vue';
import { modalProps, ModalProps, ModalType } from './modal-types';
import { Icon } from '@devui/shared/components/icon';
import { FixedOverlay } from '../../overlay';
import { useModal } from './composables/use-modal';
import { useDraggable } from './composables/use-draggable';
import DModalHeader from './components/header';
import DModalBody from './components/body';
import { CloseIcon } from './components/modal-icons';
import { useNamespace } from '@devui/shared';
import './modal.scss';
interface TypeList {
type: ModalType;
text: string;
icon: string;
color: string;
}
export default defineComponent({
name: 'DModal',
inheritAttrs: false,
props: modalProps,
emits: ['update:modelValue', 'close'],
setup(props: ModalProps, { slots, attrs, emit }) {
const ns = useNamespace('modal');
const { modelValue, title, showClose, showOverlay, appendToBody, closeOnClickOverlay, keepLast } = toRefs(props);
const { execClose, overlayZIndex, modalZIndex } = useModal(props, emit);
const dialogRef = ref<HTMLElement>();
const headerRef = ref<HTMLElement>();
const draggable = computed(() => props.draggable);
const { clearPosition, modalPosition } = useDraggable(dialogRef, headerRef, draggable);
watch(modelValue, (val) => {
if (val && !keepLast.value) {
clearPosition();
nextTick(() => {
const autofocus = document?.querySelector('[autofocus]');
if (autofocus) {
(autofocus as HTMLElement).focus();
}
});
}
});
const renderType = () => {
const typeList: TypeList[] = [
{
type: 'success',
text: '成功',
icon: 'right-o',
color: 'var(--devui-success)',
},
{
type: 'failed',
text: '错误',
icon: 'error-o',
color: 'var(--devui-danger)',
},
{
type: 'warning',
text: '警告',
icon: 'warning-o',
color: 'var(--devui-warning)',
},
{
type: 'info',
text: '信息',
icon: 'info-o',
color: 'var(--devui-info)',
},
];
const item = typeList.find((i) => i.type === props.type);
return (
<div style={{ cursor: props.draggable ? 'move' : 'default' }} ref={headerRef}>
<DModalHeader>
<div class="type-content">
<div class="type-content-icon">
<Icon name={item?.icon} color={item?.color}></Icon>
</div>
<div class="type-content-text">{item?.text}</div>
</div>
</DModalHeader>
</div>
);
};
return () => (
<Teleport to="body" disabled={!appendToBody.value}>
{showOverlay.value && (
<FixedOverlay
modelValue={modelValue.value}
onUpdate:modelValue={execClose}
class={ns.e('overlay')}
lock-scroll={false}
close-on-click-overlay={closeOnClickOverlay.value}
style={{ zIndex: overlayZIndex.value }}
/>
)}
<Transition name={props.showAnimation ? ns.m('wipe') : ''}>
{modelValue.value && (
<div
ref={dialogRef}
class={ns.b()}
{...attrs}
style={{ transform: modalPosition.value, zIndex: modalZIndex.value }}>
{showClose.value && <Icon class="btn-close" operable component={CloseIcon()} onClick={execClose} ></Icon>}
{props.type ? (
renderType()
) : (
<div style={{ cursor: props.draggable ? 'move' : 'default' }} ref={headerRef}>
{slots.header ? slots.header() : title.value && <DModalHeader>{title.value}</DModalHeader>}
</div>
)}
<DModalBody>{slots.default?.()}</DModalBody>
{slots.footer?.()}
</div>
)}
</Transition>
</Teleport>
);
},
});