uni-collapse-item.vue 4.1 KB
Newer Older
M
mehaotian 已提交
1
<template>
2 3
  <view class="uni-collapse-item">
    <view class="uni-collapse-item__title" @click="openCollapse(!is_open)">
4
      <text class="uni-collapse-item__title-text" :class="{'is-disabled':disabled,'open--active':is_open}">{{title}}</text>
5 6
      <view class="down_arrow" :class="{'down_arrow--active': is_open}"></view>
    </view>
M
mehaotian 已提交
7
    <view ref="boxRef" class="uni-collapse-item__content" :class="{'box-open--active':is_open}">
8 9
      <view ref="contentRef" class="uni-collapse-item__content-box" :class="{'content-open--active':box_is_open}">
       <slot></slot>
10 11 12
      </view>
    </view>
  </view>
M
mehaotian 已提交
13 14
</template>

15
<script lang="uts">
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
  import { $dispatch } from './util.uts'
  export default {
    name: "UniCollapseItem",
    props: {
      // 列表标题
      title: {
        type: String,
        default: ''
      },
      open: {
        type: Boolean,
        default: false
      },
      disabled: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        height: 0,
37 38
        is_open: this.open as boolean,
        box_is_open: this.open as boolean,
DCloud-yyl's avatar
DCloud-yyl 已提交
39
        boxNode: null as UniElement | null,
40
        contentNode: null as UniElement | null,
41 42 43 44 45 46 47 48 49 50 51 52 53 54
      };
    },
    watch: {
      open(value : boolean) {
        // this.is_open = value
        if (this.boxNode != null) {
          this.openCollapse(value)
        }
      }
    },
    created() {
      $dispatch(this, 'UniCollapse', 'init', this)
    },
    mounted() {
DCloud-yyl's avatar
DCloud-yyl 已提交
55 56
      this.boxNode = this.$refs['boxRef'] as UniElement;
      this.contentNode = this.$refs['contentRef'] as UniElement;
57 58 59 60 61 62 63 64 65 66 67
      // this.openCollapse(this.open)
    },
    methods: {
      // 开启或关闭折叠面板
      openCollapse(open : boolean) {
        if (this.disabled) return
        // 关闭其他已打开
        $dispatch(this, 'UniCollapse', 'cloceAll')
        this.is_open = open
        this.openOrClose(open)
      },
68 69 70 71 72 73
      openOrClose(open : boolean) {
        // #ifdef MP-WEIXIN
        setTimeout(() => {
          this.box_is_open = open
        }, 10)
        // #endif
M
mehaotian 已提交
74
        // #ifndef MP-WEIXIN
75 76 77 78 79 80 81 82 83 84 85 86
        const boxNode = this.boxNode?.style!;
        const contentNode = this.contentNode?.style!;
        let hide = open ? 'flex' : 'none';
        const opacity = open ? "1" : "0"
        let ani_transform = open ? 'translateY(0)' : 'translateY(-100%)';
        boxNode.setProperty('display', hide);
        this.$nextTick(() => {
          setTimeout(() => {
            contentNode.setProperty('transform', ani_transform);
            contentNode.setProperty('opacity', opacity);
          }, 10)
        })
M
mehaotian 已提交
87
        // #endif
88 89 90
      }
    }
  }
M
mehaotian 已提交
91 92
</script>

M
mehaotian 已提交
93
<style>
94 95 96
  .uni-collapse-item {
    background-color: #fff;
  }
M
mehaotian 已提交
97

M
mehaotian 已提交
98
  .uni-collapse-item .uni-collapse-item__title {
99 100 101 102 103
    flex-direction: row;
    align-items: center;
    padding: 12px;
    background-color: #fff;
  }
104

M
mehaotian 已提交
105
  .uni-collapse-item .down_arrow {
106 107 108 109 110 111 112 113 114
    width: 8px;
    height: 8px;
    transform: rotate(45deg);
    border-right: 1px #999 solid;
    border-bottom: 1px #999 solid;
    margin-top: -3px;
    transition-property: transform;
    transition-duration: 0.2s;
  }
M
mehaotian 已提交
115

M
mehaotian 已提交
116
  .uni-collapse-item .down_arrow--active {
117 118 119
    transform: rotate(-135deg);
    margin-top: 0px;
  }
M
mehaotian 已提交
120

M
mehaotian 已提交
121
  .uni-collapse-item .uni-collapse-item__title-text {
122 123 124 125 126
    flex: 1;
    color: #000;
    font-size: 14px;
    font-weight: 400;
  }
M
mehaotian 已提交
127

M
mehaotian 已提交
128
  .uni-collapse-item .open--active {
129 130
    /* background-color: #f0f0f0; */
    color: #bbb;
131 132
  }

M
mehaotian 已提交
133
  .uni-collapse-item .is-disabled {
134 135
    color: #999;
  }
136

M
mehaotian 已提交
137
  .uni-collapse-item .uni-collapse-item__content {
138
    display: none;
139
    position: relative;
M
mehaotian 已提交
140
    overflow: hidden;
141 142 143 144
  }

  .uni-collapse-item .box-open--active {
    display: flex;
145
  }
M
mehaotian 已提交
146

M
mehaotian 已提交
147
  .uni-collapse-item .uni-collapse-item__content-box {
148 149 150 151 152 153
    width: 100%;
    transition-property: transform, opacity;
    transition-duration: 0.2s;
    transform: translateY(-100%);
    opacity: 0;
  }
154 155 156 157 158 159
  /* #ifdef MP-WEIXIN */
  .uni-collapse-item .content-open--active {
    transform: translateY(0%);
    opacity: 1;
  }
  /* #endif */
160
</style>