BasicDrawer.tsx
7.52 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import { Drawer, Row, Col, Button } from 'ant-design-vue';
import {
defineComponent,
ref,
computed,
watchEffect,
watch,
unref,
getCurrentInstance,
nextTick,
toRaw,
} from 'vue';
import { BasicTitle } from '/@/components/Basic';
import { ScrollContainer, ScrollContainerOptions } from '/@/components/Container/index';
import { FullLoading } from '/@/components/Loading/index';
import { getSlot } from '/@/utils/helper/tsxHelper';
import { DrawerInstance, DrawerProps, DrawerType } from './types';
import { basicProps } from './props';
import { isFunction, isNumber } from '/@/utils/is';
import { LeftOutlined } from '@ant-design/icons-vue';
// import { appStore } from '/@/store/modules/app';
// import { useRouter } from 'vue-router';
import { buildUUID } from '/@/utils/uuid';
import { deepMerge } from '/@/utils';
import './index.less';
const prefixCls = 'basic-drawer';
export default defineComponent({
// inheritAttrs: false,
props: basicProps,
emits: ['visible-change', 'ok', 'close', 'register'],
setup(props, { slots, emit, attrs }) {
// const { currentRoute } = useRouter();
const scrollRef = ref<any>(null);
/**
* @description: 获取配置ScrollContainer
*/
const getScrollOptions = computed(
(): ScrollContainerOptions => {
return {
...(props.scrollOptions as any),
};
}
);
const visibleRef = ref(false);
const propsRef = ref<Partial<DrawerProps> | null>(null);
// 自定义title组件:获得title
const getMergeProps = computed((): any => {
return deepMerge(toRaw(props), unref(propsRef));
});
const getProps = computed(() => {
const opt: any = {
// @ts-ignore
placement: 'right',
...attrs,
...props,
...(unref(propsRef) as any),
visible: unref(visibleRef),
};
opt.title = undefined;
if (opt.drawerType === DrawerType.DETAIL) {
if (!opt.width) {
opt.width = '100%';
}
opt.wrapClassName = opt.wrapClassName
? `${opt.wrapClassName} ${prefixCls}__detail`
: `${prefixCls}__detail`;
// opt.maskClosable = false;
if (!opt.getContainer) {
opt.getContainer = `.default-layout__main`;
}
}
return opt;
});
watchEffect(() => {
visibleRef.value = props.visible;
});
watch(
() => visibleRef.value,
(visible) => {
// appStore.commitLockMainScrollState(visible);
nextTick(() => {
emit('visible-change', visible);
});
},
{
immediate: false,
}
);
// watch(
// () => currentRoute.value.path,
// () => {
// if (unref(visibleRef)) {
// visibleRef.value = false;
// }
// }
// );
function scrollBottom() {
const scroll = unref(scrollRef);
if (scroll) {
scroll.scrollBottom();
}
}
function scrollTo(to: number) {
const scroll = unref(scrollRef);
if (scroll) {
scroll.scrollTo(to);
}
}
function getScrollWrap() {
const scroll = unref(scrollRef);
if (scroll) {
return scroll.getScrollWrap();
}
return null;
}
// 取消事件
async function onClose(e: any) {
const { closeFunc } = unref(getProps);
emit('close', e);
if (closeFunc && isFunction(closeFunc)) {
const res = await closeFunc();
res && (visibleRef.value = false);
return;
}
visibleRef.value = false;
}
function setDrawerProps(props: Partial<DrawerProps>): void {
// 保留上一次的setDrawerProps
propsRef.value = deepMerge(unref(propsRef) || {}, props);
if (Reflect.has(props, 'visible')) {
visibleRef.value = !!props.visible;
}
}
// 底部按钮自定义实现,
const getFooterHeight = computed(() => {
const { footerHeight, showFooter }: DrawerProps = unref(getProps);
if (showFooter && footerHeight) {
return isNumber(footerHeight) ? `${footerHeight}px` : `${footerHeight.replace('px', '')}px`;
}
return 0;
});
function renderFooter() {
const {
showCancelBtn,
cancelButtonProps,
cancelText,
showOkBtn,
okType,
okText,
okButtonProps,
confirmLoading,
showFooter,
}: DrawerProps = unref(getProps);
return (
getSlot(slots, 'footer') ||
(showFooter && (
<div class={`${prefixCls}__footer`}>
{getSlot(slots, 'insertFooter')}
{showCancelBtn && (
<Button {...cancelButtonProps} onClick={onClose} class="mr-2">
{() => cancelText}
</Button>
)}
{getSlot(slots, 'centerFooter')}
{showOkBtn && (
<Button
type={okType}
{...okButtonProps}
loading={confirmLoading}
onClick={() => {
emit('ok');
}}
>
{() => okText}
</Button>
)}
{getSlot(slots, 'appendFooter')}
</div>
))
);
}
function renderHeader() {
const { title } = unref(getMergeProps);
return props.drawerType === DrawerType.DETAIL ? (
getSlot(slots, 'title') || (
<Row type="flex" align="middle" class={`${prefixCls}__detail-header`}>
{() => (
<>
{props.showDetailBack && (
<Col class="mx-2">
{() => (
<Button size="small" type="link" onClick={onClose}>
{() => <LeftOutlined />}
</Button>
)}
</Col>
)}
{title && (
<Col style="flex:1" class={[`${prefixCls}__detail-title`, 'ellipsis', 'px-2']}>
{() => title}
</Col>
)}
{getSlot(slots, 'titleToolbar')}
</>
)}
</Row>
)
) : (
<BasicTitle>{() => title || getSlot(slots, 'title')}</BasicTitle>
);
}
const currentInstance = getCurrentInstance() as any;
if (getCurrentInstance()) {
currentInstance.scrollBottom = scrollBottom;
currentInstance.scrollTo = scrollTo;
currentInstance.getScrollWrap = getScrollWrap;
}
const drawerInstance: DrawerInstance = {
setDrawerProps: setDrawerProps,
};
const uuid = buildUUID();
emit('register', drawerInstance, uuid);
return () => {
const footerHeight = unref(getFooterHeight);
return (
<Drawer
class={prefixCls}
onClose={onClose}
{...{
...attrs,
...unref(getProps),
}}
>
{{
title: () => renderHeader(),
default: () => (
<>
<FullLoading
absolute
class={[!unref(getProps).loading ? 'hidden' : '']}
tip="加载中..."
/>
<ScrollContainer
ref={scrollRef}
{...{ ...attrs, ...unref(getScrollOptions) }}
style={{
height: `calc(100% - ${footerHeight})`,
}}
>
{() => getSlot(slots, 'default')}
</ScrollContainer>
{renderFooter()}
</>
),
}}
</Drawer>
);
};
},
});