Ben Lin
2024-06-18 ebbd788fbb2c0b45d4473798efc57eec8ba74a25
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
<script lang="tsx">
  import { CollapseTransition } from '@/components/Transition';
  import { useDesign } from '@/hooks/web/useDesign';
  import { triggerWindowResize } from '@/utils/event';
  import { useTimeoutFn } from '@vben/hooks';
  import { Skeleton } from 'ant-design-vue';
  import { isNil } from 'lodash-es';
  import { defineComponent, ref, unref, type ExtractPropTypes, type PropType } from 'vue';
  import CollapseHeader from './CollapseHeader.vue';
 
  const collapseContainerProps = {
    title: { type: String, default: '' },
    loading: { type: Boolean },
    /**
     *  Can it be expanded
     */
    canExpand: { type: Boolean, default: true },
    /**
     * Warm reminder on the right side of the title
     */
    helpMessage: {
      type: [Array, String] as PropType<string[] | string>,
      default: '',
    },
    /**
     * Whether to trigger window.resize when expanding and contracting,
     * Can adapt to tables and forms, when the form shrinks, the form triggers resize to adapt to the height
     */
    triggerWindowResize: { type: Boolean },
    /**
     * Delayed loading time
     */
    lazyTime: { type: Number, default: 0 },
  };
 
  export type CollapseContainerProps = ExtractPropTypes<typeof collapseContainerProps>;
 
  export default defineComponent({
    name: 'CollapseContainer',
 
    props: collapseContainerProps,
 
    setup(props, { expose, slots }) {
      const { prefixCls } = useDesign('collapse-container');
 
      const show = ref(true);
 
      const handleExpand = (val: boolean) => {
        show.value = isNil(val) ? !show.value : val;
        if (props.triggerWindowResize) {
          // 200 milliseconds here is because the expansion has animation,
          useTimeoutFn(triggerWindowResize, 200);
        }
      };
 
      expose({ handleExpand });
 
      return () => (
        <div class={unref(prefixCls)}>
          <CollapseHeader
            {...props}
            prefixCls={unref(prefixCls)}
            onExpand={handleExpand}
            show={show.value}
            v-slots={{
              title: slots.title,
              action: slots.action,
            }}
          />
 
          <div class="p-2">
            <CollapseTransition enable={props.canExpand}>
              {props.loading ? (
                <Skeleton active={props.loading} />
              ) : (
                <div class={`${prefixCls}__body`} v-show={show.value}>
                  {slots.default?.()}
                </div>
              )}
            </CollapseTransition>
          </div>
 
          {slots.footer && <div class={`${prefixCls}__footer`}>{slots.footer()}</div>}
        </div>
      );
    },
  });
</script>
 
<style lang="less">
  @prefix-cls: ~'@{namespace}-collapse-container';
 
  .@{prefix-cls} {
    transition: all 0.3s ease-in-out;
    border-radius: 2px;
    background-color: @component-background;
 
    &__header {
      display: flex;
      align-items: center;
      justify-content: space-between;
      height: 32px;
      border-bottom: 1px solid @border-color-light;
    }
 
    &__footer {
      border-top: 1px solid @border-color-light;
    }
 
    &__action {
      display: flex;
      flex: 1;
      align-items: center;
      justify-content: flex-end;
      text-align: right;
    }
  }
</style>