¶Ô±ÈÐÂÎļþ |
| | |
| | | <template> |
| | | <view |
| | | class="u-tabbar-item" |
| | | :style="[$u.addStyle(customStyle)]" |
| | | @tap="clickHandler" |
| | | > |
| | | <view class="u-tabbar-item__icon"> |
| | | <u-icon |
| | | v-if="icon" |
| | | :name="icon" |
| | | :color="isActive? parentData.activeColor : parentData.inactiveColor" |
| | | :size="20" |
| | | ></u-icon> |
| | | <template v-else> |
| | | <slot |
| | | v-if="isActive" |
| | | name="active-icon" |
| | | /> |
| | | <slot |
| | | v-else |
| | | name="inactive-icon" |
| | | /> |
| | | </template> |
| | | <u-badge |
| | | absolute |
| | | :offset="[0, dot ? '34rpx' : badge > 9 ? '14rpx' : '20rpx']" |
| | | :customStyle="badgeStyle" |
| | | :isDot="dot" |
| | | :value="badge || (dot ? 1 : null)" |
| | | :show="dot || badge > 0" |
| | | ></u-badge> |
| | | </view> |
| | | |
| | | <slot name="text"> |
| | | <text |
| | | class="u-tabbar-item__text" |
| | | :style="{ |
| | | color: isActive? parentData.activeColor : parentData.inactiveColor |
| | | }" |
| | | >{{ text }}</text> |
| | | </slot> |
| | | </view> |
| | | </template> |
| | | |
| | | <script> |
| | | import props from './props.js'; |
| | | /** |
| | | * TabbarItem åºé¨å¯¼èªæ åç»ä»¶ |
| | | * @description æ¤ç»ä»¶æä¾äºèªå®ä¹tabbarçè½åã |
| | | * @tutorial https://www.uviewui.com/components/tabbar.html |
| | | * @property {String | Number} name itemæ ç¾çåç§°ï¼ä½ä¸ºä¸u-tabbarçvalueåæ°å¹é
çæ è¯ç¬¦ |
| | | * @property {String} icon uViewå
ç½®å¾æ æè
ç»å¯¹è·¯å¾çå¾ç |
| | | * @property {String | Number} badge å³ä¸è§çè§æ æç¤ºä¿¡æ¯ |
| | | * @property {Boolean} dot æ¯å¦æ¾ç¤ºåç¹ï¼å°ä¼è¦çbadgeåæ°ï¼é»è®¤ false ï¼ |
| | | * @property {String} text æè¿°ææ¬ |
| | | * @property {Object | String} badgeStyle æ§å¶å¾½æ çä½ç½®ï¼å¯¹è±¡æè
å符串形å¼ï¼å¯ä»¥è®¾ç½®topåright屿§ï¼é»è®¤ 'top: 6px;right:2px;' ï¼ |
| | | * @property {Object} customStyle å®ä¹éè¦ç¨å°çå¤é¨æ ·å¼ |
| | | * |
| | | * @example <u-tabbar :value="value2" :placeholder="false" @change="name => value2 = name" :fixed="false" :safeAreaInsetBottom="false"><u-tabbar-item text="é¦é¡µ" icon="home" dot ></u-tabbar-item></u-tabbar> |
| | | */ |
| | | export default { |
| | | name: 'u-tabbar-item', |
| | | mixins: [uni.$u.mpMixin, uni.$u.mixin,props], |
| | | data() { |
| | | return { |
| | | isActive: false, // æ¯å¦å¤äºæ¿æ´»ç¶æ |
| | | parentData: { |
| | | value: null, |
| | | activeColor: '', |
| | | inactiveColor: '' |
| | | } |
| | | } |
| | | }, |
| | | created() { |
| | | this.init() |
| | | }, |
| | | methods: { |
| | | init() { |
| | | // æ¯ä»å®å°ç¨åºä¸æ¯æprovide/injectï¼æä»¥ä½¿ç¨è¿ä¸ªæ¹æ³è·åæ´ä¸ªç¶ç»ä»¶ï¼å¨createdå®ä¹ï¼é¿å
循ç¯å¼ç¨ |
| | | this.updateParentData() |
| | | if (!this.parent) { |
| | | uni.$u.error('u-tabbar-itemå¿
é¡»æé
u-tabbarç»ä»¶ä½¿ç¨') |
| | | } |
| | | // æ¬åç»ä»¶å¨u-tabbarçchildrenæ°ç»ä¸çç´¢å¼ |
| | | const index = this.parent.children.indexOf(this) |
| | | // 夿æ¬ç»ä»¶çname(å¦ææ²¡æå®ä¹nameï¼å°±ç¨indexç´¢å¼)æ¯å¦çäºç¶ç»ä»¶çvalueåæ° |
| | | this.isActive = (this.name || index) === this.parentData.value |
| | | }, |
| | | updateParentData() { |
| | | // æ¤æ¹æ³å¨mixinä¸ |
| | | this.getParentData('u-tabbar') |
| | | }, |
| | | // æ¤æ¹æ³å°ä¼è¢«ç¶ç»ä»¶u-tabbarè°ç¨ |
| | | updateFromParent() { |
| | | // éæ°åå§å |
| | | this.init() |
| | | }, |
| | | clickHandler() { |
| | | this.$nextTick(() => { |
| | | const index = this.parent.children.indexOf(this) |
| | | const name = this.name || index |
| | | // ç¹å»çitemä¸ºéæ¿æ´»çitemæååºchangeäºä»¶ |
| | | if (name !== this.parent.value) { |
| | | this.parent.$emit('change', name) |
| | | } |
| | | this.$emit('click', name) |
| | | }) |
| | | } |
| | | }, |
| | | } |
| | | </script> |
| | | |
| | | <style lang="scss" scoped> |
| | | @import "../../libs/css/components.scss"; |
| | | |
| | | .u-tabbar-item { |
| | | @include flex(column); |
| | | align-items: center; |
| | | justify-content: center; |
| | | flex: 1; |
| | | |
| | | &__icon { |
| | | @include flex; |
| | | position: relative; |
| | | width: 150rpx; |
| | | justify-content: center; |
| | | } |
| | | |
| | | &__text { |
| | | margin-top: 2px; |
| | | font-size: 12px; |
| | | color: $u-content-color; |
| | | } |
| | | } |
| | | |
| | | /* #ifdef MP */ |
| | | // ç±äºå°ç¨åºé½ä½¿ç¨shadow DOMå½¢å¼å®ç°ï¼éè¦ç»å½±å宿主设置flex: 1æè½è®©å
¶æå¼ |
| | | :host { |
| | | flex: 1 |
| | | } |
| | | /* #endif */ |
| | | </style> |