123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- <template>
- <view class="u-tabbar-item" :style="[addStyle(customStyle)]">
- <view v-if="isCenter" class="tabbar-center-item">
- <image class="center-image" :src="centerImage" mode="aspectFill"></image>
- </view>
- <template v-else>
- <view class="u-tabbar-item__icon">
- <image
- v-if="icon"
- :name="icon"
- :color="isActive ? parentData.activeColor : parentData.color"
- :size="20"
- ></image>
- <block v-else>
- <slot v-if="isActive" name="active-icon" />
- <slot v-else name="inactive-icon" />
- </block>
-
- </view>
- <slot name="text">
- <text
- class="u-tabbar-item__text"
- :style="{
- color: isActive ? parentData.activeColor : parentData.color,
- }"
- >
- {{ text }}
- </text>
- </slot>
- </template>
- </view>
- </template>
- <script>
-
- import { deepMerge, addStyle, sleep, $parent } from '@/sheep/helper';
- export default {
- name: 'su-tabbar-item',
- props: {
- customStyle: {
- type: [Object, String],
- default: () => ({}),
- },
- customClass: {
- type: String,
- default: '',
- },
-
- url: {
- type: String,
- default: '',
- },
-
- linkType: {
- type: String,
- default: 'navigateTo',
- },
-
- name: {
- type: [String, Number, null],
- default: '',
- },
-
- icon: {
- icon: String,
- default: '',
- },
-
- badge: {
- type: [String, Number, null],
- default: '',
- },
-
- dot: {
- type: Boolean,
- default: false,
- },
-
- text: {
- type: String,
- default: '',
- },
-
- badgeStyle: {
- type: [Object, String],
- default: '',
- },
- isCenter: {
- type: Boolean,
- default: false,
- },
- centerImage: {
- type: String,
- default: '',
- },
- },
- data() {
- return {
- isActive: false,
- addStyle,
- parentData: {
- value: null,
- activeColor: '',
- color: '',
- },
- parent: {},
- };
- },
- created() {
- this.init();
- },
- methods: {
- getParentData(parentName = '') {
-
- if (!this.parent) this.parent = {};
-
-
-
-
- this.parent = $parent.call(this, parentName);
- if (this.parent.children) {
-
- this.parent.children.indexOf(this) === -1 && this.parent.children.push(this);
- }
- if (this.parent && this.parentData) {
-
- Object.keys(this.parentData).map((key) => {
- this.parentData[key] = this.parent[key];
- });
- }
- },
- init() {
-
- this.updateParentData();
- if (!this.parent) {
- console.log('u-tabbar-item必须搭配u-tabbar组件使用');
- }
-
- const index = this.parent.children.indexOf(this);
-
- this.isActive = (this.name.split('?')[0] || index) === this.parentData.value;
- },
- updateParentData() {
-
- this.getParentData('su-tabbar');
- },
-
- updateFromParent() {
-
- this.init();
- },
- clickHandler() {
- this.$nextTick(() => {
- const index = this.parent.children.indexOf(this);
- const name = this.name || index;
-
- if (name !== this.parent.value) {
- this.parent.$emit('change', name);
- }
- this.$emit('click', name);
- });
- },
- },
- };
- </script>
- <style lang="scss" scoped>
- .tabbar-center-item {
- height: 40px;
- width: 40px;
- display: flex;
- align-items: center;
- justify-content: center;
- border-radius: 50%;
- background-color: rebeccapurple;
- transform: scale(1.3) translateY(-6px);
- position: absolute;
- z-index: 2;
- .center-image {
- width: 25px;
- height: 25px;
- }
- }
- .u-tabbar-item {
- display: flex;
- flex-direction: column;
- align-items: center;
- justify-content: center;
- flex: 1;
- position: relative;
- z-index: 1;
- &__icon {
- display: flex;
- position: relative;
- width: 150rpx;
- justify-content: center;
- }
- &__text {
- margin-top: 2px;
- font-size: 12px;
- color: var(--textSize);
- }
- }
- /* #ifdef MP */
- // 由于小程序都使用shadow DOM形式实现,需要给影子宿主设置flex: 1才能让其撑开
- :host {
- flex: 1;
- }
- /* #endif */
- </style>
|