Commit 7346988622dccd35c9a80980125e70696423d846

Authored by luocong2016
Committed by GitHub
1 parent d6fdfd9f

refactor: collapse/CollapseContainer (#2569)

src/components/Container/src/collapse/CollapseContainer.vue
1   -<template>
2   - <div :class="prefixCls">
3   - <CollapseHeader v-bind="props" :prefixCls="prefixCls" :show="show" @expand="handleExpand">
4   - <template #title>
5   - <slot name="title"></slot>
6   - </template>
7   - <template #action>
8   - <slot name="action"></slot>
9   - </template>
10   - </CollapseHeader>
11   -
12   - <div class="p-2">
13   - <CollapseTransition :enable="canExpan">
14   - <Skeleton v-if="loading" :active="loading" />
15   - <div :class="`${prefixCls}__body`" v-else v-show="show">
16   - <slot></slot>
17   - </div>
18   - </CollapseTransition>
19   - </div>
20   - <div :class="`${prefixCls}__footer`" v-if="$slots.footer">
21   - <slot name="footer"></slot>
22   - </div>
23   - </div>
24   -</template>
25   -<script lang="ts" setup>
26   - import type { PropType } from 'vue';
27   - import { ref } from 'vue';
  1 +<script lang="tsx">
  2 + import { ref, unref, defineComponent, type PropType, type ExtractPropTypes } from 'vue';
28 3 import { isNil } from 'lodash-es';
29   - // component
30 4 import { Skeleton } from 'ant-design-vue';
31 5 import { CollapseTransition } from '/@/components/Transition';
32 6 import CollapseHeader from './CollapseHeader.vue';
33 7 import { triggerWindowResize } from '/@/utils/event';
34   - // hook
35 8 import { useTimeoutFn } from '/@/hooks/core/useTimeout';
36 9 import { useDesign } from '/@/hooks/web/useDesign';
37 10  
38   - const props = defineProps({
  11 + const collapseContainerProps = {
39 12 title: { type: String, default: '' },
40 13 loading: { type: Boolean },
41 14 /**
... ... @@ -58,27 +31,60 @@
58 31 * Delayed loading time
59 32 */
60 33 lazyTime: { type: Number, default: 0 },
61   - });
  34 + };
62 35  
63   - const show = ref(true);
  36 + export type CollapseContainerProps = ExtractPropTypes<typeof collapseContainerProps>;
64 37  
65   - const { prefixCls } = useDesign('collapse-container');
  38 + export default defineComponent({
  39 + name: 'CollapseContainer',
66 40  
67   - /**
68   - * @description: Handling development events
69   - */
70   - function handleExpand(val: boolean) {
71   - show.value = isNil(val) ? !show.value : val;
72   - if (props.triggerWindowResize) {
73   - // 200 milliseconds here is because the expansion has animation,
74   - useTimeoutFn(triggerWindowResize, 200);
75   - }
76   - }
  41 + props: collapseContainerProps,
  42 +
  43 + setup(props, { expose, slots }) {
  44 + const { prefixCls } = useDesign('collapse-container');
  45 +
  46 + const show = ref(true);
  47 +
  48 + const handleExpand = (val: boolean) => {
  49 + show.value = isNil(val) ? !show.value : val;
  50 + if (props.triggerWindowResize) {
  51 + // 200 milliseconds here is because the expansion has animation,
  52 + useTimeoutFn(triggerWindowResize, 200);
  53 + }
  54 + };
77 55  
78   - defineExpose({
79   - handleExpand,
  56 + expose({ handleExpand });
  57 +
  58 + return () => (
  59 + <div class={unref(prefixCls)}>
  60 + <CollapseHeader
  61 + {...props}
  62 + prefixCls={unref(prefixCls)}
  63 + onExpand={handleExpand}
  64 + show={show.value}
  65 + v-slots={{
  66 + title: slots.title,
  67 + action: slots.action,
  68 + }}
  69 + />
  70 +
  71 + <div class="p-2">
  72 + <CollapseTransition enable={props.canExpan}>
  73 + {props.loading ? (
  74 + <Skeleton active={props.loading} />
  75 + ) : (
  76 + show.value && <div class={`${prefixCls}__body`}>{slots.default?.()}</div>
  77 + )}
  78 + </CollapseTransition>
  79 + </div>
  80 +
  81 + {slots.footer && <div class={`${prefixCls}__footer`}>{slots.footer()}</div>}
  82 + </div>
  83 + );
  84 + },
80 85 });
81 86 </script>
  87 +
82 88 <style lang="less">
83 89 @prefix-cls: ~'@{namespace}-collapse-container';
84 90  
... ...