<template>
  <div class="bgo-expandable" :class="{'bgo-expandable--expand': isExpand}">
    <div
      class="bgo-expandable__content"
      ref="expandableContent"
      :style="{ height: height }"
    >
      <slot></slot>
    </div>
    <div class="bgo-expandable__expand-button">
      <van-button
        plain
        block
        type="primary"
        size="small"
        :icon="isExpand ? 'arrow-up' : 'arrow-down'"
        icon-position="right"
        class="van-button--no-border van-button--transparent"
        @click.stop="handleClickExpand"
        v-if="isShowExpandButton"
        >{{ isExpand ? "收起" : "展开" }}</van-button
      >
    </div>
  </div>
</template>

<script>
// 展开按钮高度

export default {
  name: "BgoExpandable",
  props: {
    // Height Of expandable Content, Unit Pixel
    maxHeight: {
      type: Number,
      default: 64,
    },
  },
  data() {
    return {
      isShowExpandButton: true,
      isExpand: false,
      height: "auto",
    };
  },
  mounted() {
    this.$nextTick(() => {
      const clientRect = this.$refs.expandableContent.getBoundingClientRect();
      if (clientRect.height > this.$props.maxHeight) {
        this.height = this.$props.maxHeight + "px";
        this.isShowExpandButton = true;
      } else {
        this.isShowExpandButton = false;
      }
    });
  },
  methods: {
    handleClickExpand() {
      this.height = this.isExpand ? this.$props.maxHeight + "px" : "auto";
      this.isExpand = !this.isExpand;
    },
  },
};
</script>

<style lang="scss">
@import "./index";
@import "../../style/vant-extend";
</style>
