From b8ec2ffc5d2c605d5cb72cfa0c60bdbd98b8ceb8 Mon Sep 17 00:00:00 2001 From: neverland Date: Fri, 1 Feb 2019 17:10:31 +0800 Subject: [PATCH] [improvement] Tabbar: jsx (#2663) --- packages/mixins/checkbox.js | 13 +++-- packages/tabbar-item/index.js | 54 ++++++++++++++++++ packages/tabbar-item/index.vue | 73 ------------------------- packages/tabbar/{index.vue => index.js} | 37 +++++-------- packages/utils/index.js | 2 + packages/utils/slots.js | 8 +++ 6 files changed, 85 insertions(+), 102 deletions(-) create mode 100644 packages/tabbar-item/index.js delete mode 100644 packages/tabbar-item/index.vue rename packages/tabbar/{index.vue => index.js} (66%) create mode 100644 packages/utils/slots.js diff --git a/packages/mixins/checkbox.js b/packages/mixins/checkbox.js index 94ea5dc23..f95e3d46f 100644 --- a/packages/mixins/checkbox.js +++ b/packages/mixins/checkbox.js @@ -1,6 +1,7 @@ /** * Common part of Checkbox & Radio */ +import { useSlots } from '../utils'; import Icon from '../icon'; import findParent from './find-parent'; @@ -41,18 +42,18 @@ export default (parent, bem) => ({ }, render(h) { - const CheckIcon = this.$scopedSlots.icon ? ( - this.$scopedSlots.icon({ checked: this.checked }) - ) : ( + const { checked } = this; + const slots = useSlots(this); + const CheckIcon = slots('icon', { checked }) || ( ); - const Label = this.$slots.default && ( + const Label = slots('default') && ( - {this.$slots.default} + {slots('default')} ); @@ -64,7 +65,7 @@ export default (parent, bem) => ({ }} >
{CheckIcon} diff --git a/packages/tabbar-item/index.js b/packages/tabbar-item/index.js new file mode 100644 index 000000000..0e898695e --- /dev/null +++ b/packages/tabbar-item/index.js @@ -0,0 +1,54 @@ +import { use, useSlots } from '../utils'; +import Icon from '../icon'; +import Info from '../info'; +import RouterLink from '../mixins/router-link'; + +const [sfc, bem] = use('tabbar-item'); + +export default sfc({ + mixins: [RouterLink], + + props: { + icon: String, + dot: Boolean, + info: [String, Number] + }, + + data() { + return { + active: false + }; + }, + + beforeCreate() { + this.$parent.items.push(this); + }, + + destroyed() { + this.$parent.items.splice(this.$parent.items.indexOf(this), 1); + }, + + methods: { + onClick(event) { + this.$parent.onChange(this.$parent.items.indexOf(this)); + this.$emit('click', event); + this.routerLink(); + } + }, + + render(h) { + const { icon, active } = this; + const slots = useSlots(this); + const style = active ? { color: this.$parent.activeColor } : null; + + return ( +
+
+ {slots('icon', { active }) || (icon && )} + +
+
{slots('default', { active })}
+
+ ); + } +}); diff --git a/packages/tabbar-item/index.vue b/packages/tabbar-item/index.vue deleted file mode 100644 index e93ac536d..000000000 --- a/packages/tabbar-item/index.vue +++ /dev/null @@ -1,73 +0,0 @@ - - - diff --git a/packages/tabbar/index.vue b/packages/tabbar/index.js similarity index 66% rename from packages/tabbar/index.vue rename to packages/tabbar/index.js index 4ed9c28e9..77a7a9719 100644 --- a/packages/tabbar/index.vue +++ b/packages/tabbar/index.js @@ -1,19 +1,8 @@ - +import { use } from '../utils'; - diff --git a/packages/utils/index.js b/packages/utils/index.js index c38a41e87..2c710e80f 100644 --- a/packages/utils/index.js +++ b/packages/utils/index.js @@ -1,5 +1,6 @@ import Vue from 'vue'; import use from './use'; +import { useSlots } from './slots'; const isServer = Vue.prototype.$isServer; @@ -44,6 +45,7 @@ export { isObj, isDef, isServer, + useSlots, camelize, isAndroid }; diff --git a/packages/utils/slots.js b/packages/utils/slots.js new file mode 100644 index 000000000..b324e9b35 --- /dev/null +++ b/packages/utils/slots.js @@ -0,0 +1,8 @@ +export function useSlots({ $slots, $scopedSlots }) { + return (name, props) => { + if ($scopedSlots[name]) { + return $scopedSlots[name](props); + } + return $slots[name]; + }; +}