refactor(Tabbar): refactor with composition api

This commit is contained in:
chenjiahan 2020-08-28 15:19:16 +08:00
parent e3cf460762
commit f843e6def9
7 changed files with 117 additions and 132 deletions

View File

@ -48,6 +48,7 @@ export default createComponent({
const [visible, setVisible] = useToggle(); const [visible, setVisible] = useToggle();
const daysRef = ref(); const daysRef = ref();
const monthRef = ref(); const monthRef = ref();
const height = useHeight(monthRef);
const title = computed(() => formatMonthTitle(props.date)); const title = computed(() => formatMonthTitle(props.date));
@ -82,14 +83,6 @@ export default createComponent({
} }
}); });
let height;
const getHeight = () => {
if (!height) {
height = useHeight(monthRef);
}
return height;
};
const getDate = () => props.data; const getDate = () => props.data;
const getTitle = () => title.value; const getTitle = () => title.value;
@ -255,9 +248,9 @@ export default createComponent({
// @exposed-api // @exposed-api
const vm = getCurrentInstance().proxy; const vm = getCurrentInstance().proxy;
vm.height = height;
vm.getDate = getDate; vm.getDate = getDate;
vm.getTitle = getTitle; vm.getTitle = getTitle;
vm.getHeight = getHeight;
vm.setVisible = setVisible; vm.setVisible = setVisible;
vm.scrollIntoView = scrollIntoView; vm.scrollIntoView = scrollIntoView;

View File

@ -251,7 +251,9 @@ export default createComponent({
const { body } = this.$refs; const { body } = this.$refs;
const { months, monthRefs } = this; const { months, monthRefs } = this;
const top = getScrollTop(body); const top = getScrollTop(body);
const heights = months.map((item, index) => monthRefs[index].getHeight()); const heights = months.map(
(item, index) => monthRefs[index].height.value
);
const heightSum = heights.reduce((a, b) => a + b, 0); const heightSum = heights.reduce((a, b) => a + b, 0);
// iOS scroll bounce may exceed the range // iOS scroll bounce may exceed the range

View File

@ -1,7 +1,13 @@
import type { Ref } from 'vue'; import { Ref, ref, onMounted } from 'vue';
export const useRect = (el: Ref<Element>) => el.value.getBoundingClientRect(); export const useRect = (el: Ref<Element>) => el.value.getBoundingClientRect();
export const useWidth = (el: Ref<Element>) => useRect(el).width; export const useHeight = (el: Ref<Element>) => {
const height = ref();
export const useHeight = (el: Ref<Element>) => useRect(el).height; onMounted(() => {
height.value = useRect(el).height;
});
return height;
};

View File

@ -1,4 +1,4 @@
import { ref, onMounted } from 'vue'; import { ref } from 'vue';
// Utils // Utils
import { createNamespace } from '../utils'; import { createNamespace } from '../utils';
@ -30,14 +30,8 @@ export default createComponent({
emits: ['click-left', 'click-right'], emits: ['click-left', 'click-right'],
setup(props, { emit, slots }) { setup(props, { emit, slots }) {
const height = ref();
const navBarRef = ref(); const navBarRef = ref();
const height = useHeight(navBarRef);
onMounted(() => {
if (props.placeholder && props.fixed) {
height.value = useHeight(navBarRef);
}
});
const onClickLeft = (event) => { const onClickLeft = (event) => {
emit('click-left', event); emit('click-left', event);
@ -90,7 +84,7 @@ export default createComponent({
}; };
return () => { return () => {
if (props.placeholder && props.fixed) { if (props.fixed && props.placeholder) {
return ( return (
<div <div
class={bem('placeholder')} class={bem('placeholder')}
@ -100,7 +94,6 @@ export default createComponent({
</div> </div>
); );
} }
return renderNavBar(); return renderNavBar();
}; };
}, },

View File

@ -1,7 +1,7 @@
import { ref, reactive, nextTick, onActivated, watch } from 'vue'; import { ref, reactive, nextTick, onActivated, watch } from 'vue';
import { createNamespace, isDef } from '../utils'; import { createNamespace, isDef } from '../utils';
import { doubleRaf } from '../utils/dom/raf'; import { doubleRaf } from '../utils/dom/raf';
import { useWidth } from '../composition/use-rect'; import { useRect } from '../composition/use-rect';
import Icon from '../icon'; import Icon from '../icon';
const [createComponent, bem] = createNamespace('notice-bar'); const [createComponent, bem] = createNamespace('notice-bar');
@ -139,8 +139,8 @@ export default createComponent({
return; return;
} }
const wrapRefWidth = useWidth(wrapRef); const wrapRefWidth = useRect(wrapRef).width;
const contentRefWidth = useWidth(contentRef); const contentRefWidth = useRect(contentRef).width;
if (scrollable || contentRefWidth > wrapRefWidth) { if (scrollable || contentRefWidth > wrapRefWidth) {
doubleRaf(() => { doubleRaf(() => {

View File

@ -1,9 +1,12 @@
import { computed, getCurrentInstance } from 'vue';
import { TABBAR_KEY } from '../tabbar';
// Utils // Utils
import { createNamespace, isObject, isDef } from '../utils'; import { createNamespace, isObject, isDef } from '../utils';
import { route, routeProps } from '../composition/use-route';
// Mixins // Composition
import { ChildrenMixin } from '../mixins/relation'; import { useParent } from '../composition/use-relation';
import { routeProps, useRoute } from '../composition/use-route';
// Components // Components
import Icon from '../icon'; import Icon from '../icon';
@ -12,8 +15,6 @@ import Badge from '../badge';
const [createComponent, bem] = createNamespace('tabbar-item'); const [createComponent, bem] = createNamespace('tabbar-item');
export default createComponent({ export default createComponent({
mixins: [ChildrenMixin('vanTabbar')],
props: { props: {
...routeProps, ...routeProps,
dot: Boolean, dot: Boolean,
@ -25,57 +26,62 @@ export default createComponent({
emits: ['click'], emits: ['click'],
data() { setup(props, { emit, slots }) {
return { const route = useRoute();
active: false, const vm = getCurrentInstance().proxy;
}; const { parent, index } = useParent(TABBAR_KEY);
},
computed: { const active = computed(() => {
routeActive() { const { $route } = vm;
const { to, $route } = this; const { route, modelValue } = parent.props;
if (to && $route) {
if (route && $route) {
const { to } = props;
const config = isObject(to) ? to : { path: to }; const config = isObject(to) ? to : { path: to };
const pathMatched = config.path === $route.path; const pathMatched = config.path === $route.path;
const nameMatched = isDef(config.name) && config.name === $route.name; const nameMatched = isDef(config.name) && config.name === $route.name;
return pathMatched || nameMatched; return pathMatched || nameMatched;
} }
},
},
methods: { return (props.name || index.value) === modelValue;
onClick(event) { });
this.parent.onChange(this.name || this.index);
this.$emit('click', event);
route(this.$router, this);
},
genIcon(active) { const onClick = (event) => {
if (this.$slots.icon) { parent.setActive(props.name || index.value);
return this.$slots.icon({ active }); emit('click', event);
route();
};
const renderIcon = () => {
if (slots.icon) {
return slots.icon({ active: active.value });
} }
if (props.icon) {
if (this.icon) { return <Icon name={props.icon} classPrefix={props.iconPrefix} />;
return <Icon name={this.icon} classPrefix={this.iconPrefix} />;
} }
}, };
},
render() { return () => {
const active = this.parent.route ? this.routeActive : this.active; const { dot, badge } = props;
const color = this.parent[active ? 'activeColor' : 'inactiveColor']; const { activeColor, inactiveColor } = parent.props;
const color = active.value ? activeColor : inactiveColor;
return ( return (
<div class={bem({ active })} style={{ color }} onClick={this.onClick}> <div
class={bem({ active: active.value })}
style={{ color }}
onClick={onClick}
>
<div class={bem('icon')}> <div class={bem('icon')}>
{this.genIcon(active)} {renderIcon()}
<Badge dot={this.dot} badge={this.badge} /> <Badge dot={dot} badge={badge} />
</div> </div>
<div class={bem('text')}> <div class={bem('text')}>
{this.$slots.default ? this.$slots.default({ active }) : null} {slots.default?.({ active: active.value })}
</div> </div>
</div> </div>
); );
};
}, },
}); });

View File

@ -1,12 +1,13 @@
import { createNamespace } from '../utils'; import { ref, reactive, provide } from 'vue';
import { ParentMixin } from '../mixins/relation'; import { createNamespace, isDef } from '../utils';
import { BORDER_TOP_BOTTOM } from '../utils/constant'; import { BORDER_TOP_BOTTOM } from '../utils/constant';
import { useHeight } from '../composition/use-rect';
const [createComponent, bem] = createNamespace('tabbar'); const [createComponent, bem] = createNamespace('tabbar');
export default createComponent({ export const TABBAR_KEY = 'vanTabbar';
mixins: [ParentMixin('vanTabbar')],
export default createComponent({
props: { props: {
route: Boolean, route: Boolean,
zIndex: [Number, String], zIndex: [Number, String],
@ -33,75 +34,59 @@ export default createComponent({
emits: ['change', 'update:modelValue'], emits: ['change', 'update:modelValue'],
data() { setup(props, { emit, slots }) {
return { const tabbarRef = ref();
height: null, const height = useHeight(tabbarRef);
}; const children = reactive([]);
},
computed: { const isUnfit = () => {
fit() { if (isDef(props.safeAreaInsetBottom)) {
if (this.safeAreaInsetBottom !== null) { return !props.safeAreaInsetBottom;
return this.safeAreaInsetBottom;
} }
// enable safe-area-inset-bottom by default when fixed // enable safe-area-inset-bottom by default when fixed
return this.fixed; return !props.fixed;
}, };
},
watch: { const renderTabbar = () => {
children: 'setActiveItem', const { fixed, zIndex, border } = props;
modelValue: 'setActiveItem', const unfit = isUnfit();
},
mounted() {
if (this.placeholder && this.fixed) {
this.height = this.$refs.tabbar.getBoundingClientRect().height;
}
},
methods: {
setActiveItem() {
this.children.forEach((item, index) => {
item.active = (item.name || index) === this.modelValue;
});
},
onChange(active) {
if (active !== this.modelValue) {
this.$emit('update:modelValue', active);
this.$emit('change', active);
}
},
genTabbar() {
return ( return (
<div <div
ref="tabbar" ref={tabbarRef}
style={{ zIndex: this.zIndex }} style={{ zIndex }}
class={[ class={[bem({ unfit, fixed }), { [BORDER_TOP_BOTTOM]: border }]}
{ [BORDER_TOP_BOTTOM]: this.border },
bem({
unfit: !this.fit,
fixed: this.fixed,
}),
]}
> >
{this.$slots.default?.()} {slots.default?.()}
</div> </div>
); );
}, };
},
render() { const setActive = (active) => {
if (this.placeholder && this.fixed) { if (active !== props.modelValue) {
emit('update:modelValue', active);
emit('change', active);
}
};
provide(TABBAR_KEY, {
props,
children,
setActive,
});
return () => {
if (props.fixed && props.placeholder) {
return ( return (
<div class={bem('placeholder')} style={{ height: `${this.height}px` }}> <div
{this.genTabbar()} class={bem('placeholder')}
style={{ height: height.value ? `${height.value}px` : null }}
>
{renderTabbar()}
</div> </div>
); );
} }
return this.genTabbar(); return renderTabbar();
};
}, },
}); });