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

16
<script lang="uts">
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
  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,
M
mehaotian 已提交
38 39
        is_open: this.open as boolean,
        box_is_open:this.open as boolean,
DCloud-yyl's avatar
DCloud-yyl 已提交
40
        boxNode: null as UniElement | null,
M
mehaotian 已提交
41
        contentNode: null as UniElement | null,
42 43 44 45 46 47 48 49 50 51 52 53 54 55
      };
    },
    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 已提交
56 57
      this.boxNode = this.$refs['boxRef'] as UniElement;
      this.contentNode = this.$refs['contentRef'] as UniElement;
58 59 60 61 62 63 64 65 66 67 68
      // this.openCollapse(this.open)
    },
    methods: {
      // 开启或关闭折叠面板
      openCollapse(open : boolean) {
        if (this.disabled) return
        // 关闭其他已打开
        $dispatch(this, 'UniCollapse', 'cloceAll')
        this.is_open = open
        this.openOrClose(open)
      },
M
mehaotian 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
      openOrClose(open : boolean) {
        // #ifdef MP-WEIXIN
        setTimeout(()=>{
        	this.box_is_open = !open
        },10)
        // #endif
        // #ifndef MP-WEIXIN
        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(() => {
          contentNode.setProperty('transform', ani_transform);
          contentNode.setProperty('opacity', opacity);
        })
        // #endif
87 88 89
      }
    }
  }
M
mehaotian 已提交
90 91
</script>

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

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

M
mehaotian 已提交
104
  .uni-collapse-item .down_arrow {
105 106 107 108 109 110 111 112 113
    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 已提交
114

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

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

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

  .uni-collapse-item .is-disabled {
133 134
    color: #999;
  }
135

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

M
mehaotian 已提交
145
  .uni-collapse-item .uni-collapse-item__content-box {
146 147 148 149 150
    width: 100%;
    transition-property: transform, opacity;
    transition-duration: 0.2s;
    transform: translateY(-100%);
    opacity: 0;
M
mehaotian 已提交
151 152 153 154 155
  }

  .uni-collapse-item .content-open--active {
  	transform: translateY(0%);
  	opacity: 1;
156 157
  }
</style>