mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
refactor(NavBar): refactor with composition api
This commit is contained in:
parent
ce27bcdf9b
commit
796ddca896
@ -1,3 +1,5 @@
|
|||||||
|
import { ref, onMounted } from 'vue';
|
||||||
|
|
||||||
// Utils
|
// Utils
|
||||||
import { createNamespace } from '../utils';
|
import { createNamespace } from '../utils';
|
||||||
import { BORDER_BOTTOM } from '../utils/constant';
|
import { BORDER_BOTTOM } from '../utils/constant';
|
||||||
@ -24,82 +26,79 @@ export default createComponent({
|
|||||||
|
|
||||||
emits: ['click-left', 'click-right'],
|
emits: ['click-left', 'click-right'],
|
||||||
|
|
||||||
data() {
|
setup(props, { emit, slots }) {
|
||||||
return {
|
const navBar = ref(null);
|
||||||
height: null,
|
const height = ref(null);
|
||||||
};
|
|
||||||
},
|
|
||||||
|
|
||||||
mounted() {
|
onMounted(() => {
|
||||||
if (this.placeholder && this.fixed) {
|
if (props.placeholder && props.fixed) {
|
||||||
this.height = this.$refs.navBar.getBoundingClientRect().height;
|
height.value = navBar.value.getBoundingClientRect().height;
|
||||||
}
|
}
|
||||||
},
|
});
|
||||||
|
|
||||||
methods: {
|
const onClickLeft = (event) => {
|
||||||
genLeft() {
|
emit('click-left', event);
|
||||||
const leftSlot = this.$slots.left?.();
|
};
|
||||||
|
|
||||||
if (leftSlot) {
|
const onClickRight = (event) => {
|
||||||
return leftSlot;
|
emit('click-right', event);
|
||||||
|
};
|
||||||
|
|
||||||
|
const renderLeft = () => {
|
||||||
|
if (slots.left) {
|
||||||
|
return slots.left();
|
||||||
}
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
this.leftArrow && <Icon class={bem('arrow')} name="arrow-left" />,
|
props.leftArrow && <Icon class={bem('arrow')} name="arrow-left" />,
|
||||||
this.leftText && <span class={bem('text')}>{this.leftText}</span>,
|
props.leftText && <span class={bem('text')}>{props.leftText}</span>,
|
||||||
];
|
];
|
||||||
},
|
};
|
||||||
|
|
||||||
genRight() {
|
const renderRight = () => {
|
||||||
const rightSlot = this.$slots.right?.();
|
if (slots.right) {
|
||||||
|
return slots.right();
|
||||||
if (rightSlot) {
|
|
||||||
return rightSlot;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.rightText) {
|
if (props.rightText) {
|
||||||
return <span class={bem('text')}>{this.rightText}</span>;
|
return <span class={bem('text')}>{props.rightText}</span>;
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
|
|
||||||
genNavBar() {
|
const renderNavBar = () => {
|
||||||
|
const { title, fixed, border, zIndex } = props;
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
ref="navBar"
|
ref={navBar}
|
||||||
style={{ zIndex: this.zIndex }}
|
style={{ zIndex }}
|
||||||
class={[bem({ fixed: this.fixed }), { [BORDER_BOTTOM]: this.border }]}
|
class={[bem({ fixed }), { [BORDER_BOTTOM]: border }]}
|
||||||
>
|
>
|
||||||
<div class={bem('left')} onClick={this.onClickLeft}>
|
<div class={bem('left')} onClick={onClickLeft}>
|
||||||
{this.genLeft()}
|
{renderLeft()}
|
||||||
</div>
|
</div>
|
||||||
<div class={[bem('title'), 'van-ellipsis']}>
|
<div class={[bem('title'), 'van-ellipsis']}>
|
||||||
{this.$slots.title ? this.$slots.title() : this.title}
|
{slots.title ? slots.title() : title}
|
||||||
</div>
|
</div>
|
||||||
<div class={bem('right')} onClick={this.onClickRight}>
|
<div class={bem('right')} onClick={onClickRight}>
|
||||||
{this.genRight()}
|
{renderRight()}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
},
|
};
|
||||||
|
|
||||||
onClickLeft(event) {
|
return () => {
|
||||||
this.$emit('click-left', event);
|
if (props.placeholder && props.fixed) {
|
||||||
},
|
|
||||||
|
|
||||||
onClickRight(event) {
|
|
||||||
this.$emit('click-right', event);
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
render() {
|
|
||||||
if (this.placeholder && this.fixed) {
|
|
||||||
return (
|
return (
|
||||||
<div class={bem('placeholder')} style={{ height: `${this.height}px` }}>
|
<div
|
||||||
{this.genNavBar()}
|
class={bem('placeholder')}
|
||||||
|
style={{ height: `${height.value}px` }}
|
||||||
|
>
|
||||||
|
{renderNavBar()}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.genNavBar();
|
return renderNavBar();
|
||||||
|
};
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user