statisticsItem.vue 7.01 KB
<template>
  <div class="statistics-item-wrapper"
       :class="{
         'is-tap': beTap,
         'is-expand': expanded,
       }"
       @click="handleTapItem">
    <div class="title-wrapper">
      <div class="title-label-wrapper  ">
        <div class="main-label text-hide">
          {{ title }}
        </div>
        <slot name="sub-title" :data="data">
          <div v-if="subTitle" class="sub-label text-hide">
            {{ subTitle }}
          </div>
        </slot>
      </div>
      <div class="title-info-wrapper">
        <slot name="title-info">
          <div v-for="item in titleInfoSchema"
               :key="item.key"
               class="info-item-wrapper"
               @click.stop>
            <div class="value">
              {{ getValue(item.name) }}
            </div>
            <div class="label">
              {{ item.label }}
            </div>
          </div>
        </slot>
        <slot v-if="beTap" name="title-info-icon">
          <van-icon color="#000000E5"
                    name="arrow"
                    :size="16" />
        </slot>
      </div>
    </div>
    <SpecialItem v-if="specialSchema.length"
                 style="margin: 8px 0"
                 :data="data"
                 :schema="specialSchema">
    </SpecialItem>
    <div class="container-wrapper">
      <GridItem :data="data" :schema="defaultSchema"></GridItem>
      <div class="container" gutter="4">
        <div v-show="showMoreToggle" span="24">
          <div class="more-collapse"
               :style="{
                 height: expanded ? (`${moreHeight}px`) : '0px',
               }">
            <div class="more-inner">
              <GridItem :data="data" :schema="moreSchema"></GridItem>
            </div>
          </div>
          <!-- 用来记录高度 -->
          <div class="more-measure">
            <div ref="moreMeasureInner" class="more-inner">
              <GridItem :data="data" :schema="moreSchema"></GridItem>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div v-if="showMoreToggle"
         class="toggle-wrapper"
         :class="{ 'is-expanded': expanded }"
         @click.stop="onToggleMore">
      {{ expanded ? `收起${moreSchema.length}项售后成交指标` : `已隐藏${moreSchema.length}项售后成交指标` }}
      <svg-icon class="toggle-icon"
                name="show-more"
                size="16px" />
    </div>
  </div>
</template>

<script>
import { get } from "lodash-es";
import { Col, Divider, Icon, Row } from "vant";
import Tooltip from "@/components/tooltip/index.vue";
import GridItem from "./gridItem.vue";
import SpecialItem from "./specialItem.vue";

export default {
	name: "",
	components: {
		[Col.name]: Col,
		[Row.name]: Row,
		[Divider.name]: Divider,
		[Icon.name]: Icon,
		GridItem,
		SpecialItem,
	},
	props: {
		title: {
			type: String,
			default: "",
		},
		subTitle: {
			type: String,
			default: "",
		},
		schema: {
			type: Array,
			default: () => ([]),
		},
		data: {
			type: Object,
			default: () => ({}),
		},
		beTap: {
			type: Boolean,
			default: true,
		},
		hasMore: {
			type: Boolean,
			default: true,
		},
	},
	data() {
		return {
			expanded: false,
			moreHeight: 0,
		};
	},
	computed: {
		defaultSchema() {
			return this.schema.filter(item => (!item.isMore && !item.titleInfo && !item.special));
		},
		moreSchema() {
			return this.schema.filter(item => item.isMore);
		},
		titleInfoSchema() {
			return this.schema.filter(item => item.titleInfo);
		},
		specialSchema() {
			return this.schema.filter(item => (item.special));
		},
		showMoreToggle() {
			return this.hasMore && this.moreSchema.length > 0;
		},
	},
	watch: {
		schema: {
			handler() {
				this.updateMoreHeight();
			},
			deep: true,
			immediate: true,
		},
	},
	mounted() {
		this.updateMoreHeight();
	},
	methods: {
		getValue(path) {
			return get(this.data, path);
		},
		handleTapItem() {
			this.$emit("tap");
		},
		onToggleMore() {
			if (!this.showMoreToggle) {
				return;
			}
			this.updateMoreHeight();
			this.expanded = !this.expanded;
		},
		updateMoreHeight() {
			this.$nextTick(() => {
				const moreMeasureInner = this.$refs.moreMeasureInner;
				this.moreHeight = moreMeasureInner ? moreMeasureInner.clientHeight + 22 : 0;
			});
		},
	},
};
</script>

<style scoped lang="scss">
.text-hide {
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
}
.statistics-item-wrapper {
	background-color: white;
	padding: 8px;
	box-sizing: border-box;
	overflow: hidden;
	border-radius: 12px;
	position: relative;
	transition: background 250ms cubic-bezier(0.2, 0.8, 0.2, 1);
	padding-bottom: 14px;
	&.is-tap {
		&:active {
			background: rgb(252, 252, 252);
		}
	}
	> .title-wrapper {
		display: flex;
		align-items: center;
		padding: 0 8px 8px;
		border-bottom: 1px solid #eeeef0;
		overflow: hidden;
		.title-label-wrapper {
			flex-shrink: 0;
			flex: 1;
			font-weight: 600;
			font-size: 14px;
			line-height: 22px;
			color: rgba(0, 0, 0, 0.9);
			margin-right: 16px;
			overflow: hidden;
			.sub-label {
				font-weight: 400;
				font-size: 12px;
				line-height: 20px;
				color: #00000066;
			}
		}
		.title-info-wrapper {
			max-width: 65%;
			flex-shrink: 0;
			display: flex;
			align-items: center;
			justify-content: flex-end;
			overflow: hidden;
			.info-item-wrapper {
				text-align: center;
				margin-right: 12px;
				.value {
					font-family: numberBold;
					font-weight: 700;
					font-size: 20px;
					line-height: 20px;
					color: var(--themeColor);
				}
				.label {
					font-weight: 400;
					font-size: 10px;
					line-height: 16px;
					color: #00000042;
				}
			}

			.sub-label {
				font-weight: 400;
				font-size: 14px;
				line-height: 22px;
				text-align: right;
				flex: 1;
				flex-shrink: 0;
				color: rgba(0, 0, 0, 0.4);
			}
			.icon {
				flex-shrink: 0;
				width: 16px;
				height: 16px;
				margin-left: 8px;
				display: flex;
				align-items: center;
				justify-content: flex-end;
			}
		}
	}
	.container-wrapper {
		border-radius: 8px;
		overflow: hidden;
		width: 100%;
		> .row-wrapper {
		}
		.container {
			overflow: hidden;
			box-sizing: border-box;
			margin-top: 4px;
			.more-collapse {
				::v-deep .col-wrapper {
					&:last-child {
						border-bottom-right-radius: 8px;
					}
					&:nth-last-child(2) {
						border-bottom-left-radius: 8px;
					}
				}
			}
		}
	}
}

.toggle-wrapper {
	background-image: linear-gradient(
		to bottom,
		rgba(255, 255, 255, 0) 0%,
		rgba(255, 255, 255, 0.5) 15%,
		rgba(255, 255, 255, 0.7) 30%,
		#ffffff 100%
	);
	padding: 6px 0;
	position: absolute;
	left: 0;
	bottom: 4px;
	width: 100%;
	text-align: center;
	font-size: 12px;
	line-height: 20px;
	color: #00000066;
	display: flex;
	align-items: center;
	justify-content: center;
	.toggle-icon {
		transform: rotate(0);
		transition: transform 300ms ease;
	}
	&.is-expanded {
		.toggle-icon {
			transform: rotate(-180deg);
		}
	}
}

.more-collapse {
	overflow: hidden;
	transition: height 300ms ease;
}

.more-measure {
	position: absolute;
	left: 0;
	top: 0;
	width: 100%;
	visibility: hidden;
	z-index: -1;
	pointer-events: none;
}
</style>