diff --git a/src/nav-bar/index.js b/src/nav-bar/index.js index 76b73d12f..4ce995fb1 100644 --- a/src/nav-bar/index.js +++ b/src/nav-bar/index.js @@ -1,3 +1,5 @@ +import { ref, onMounted } from 'vue'; + // Utils import { createNamespace } from '../utils'; import { BORDER_BOTTOM } from '../utils/constant'; @@ -24,82 +26,79 @@ export default createComponent({ emits: ['click-left', 'click-right'], - data() { - return { - height: null, + setup(props, { emit, slots }) { + const navBar = ref(null); + const height = ref(null); + + onMounted(() => { + if (props.placeholder && props.fixed) { + height.value = navBar.value.getBoundingClientRect().height; + } + }); + + const onClickLeft = (event) => { + emit('click-left', event); }; - }, - mounted() { - if (this.placeholder && this.fixed) { - this.height = this.$refs.navBar.getBoundingClientRect().height; - } - }, + const onClickRight = (event) => { + emit('click-right', event); + }; - methods: { - genLeft() { - const leftSlot = this.$slots.left?.(); - - if (leftSlot) { - return leftSlot; + const renderLeft = () => { + if (slots.left) { + return slots.left(); } return [ - this.leftArrow && , - this.leftText && {this.leftText}, + props.leftArrow && , + props.leftText && {props.leftText}, ]; - }, + }; - genRight() { - const rightSlot = this.$slots.right?.(); - - if (rightSlot) { - return rightSlot; + const renderRight = () => { + if (slots.right) { + return slots.right(); } - if (this.rightText) { - return {this.rightText}; + if (props.rightText) { + return {props.rightText}; } - }, + }; - genNavBar() { + const renderNavBar = () => { + const { title, fixed, border, zIndex } = props; return (
-
- {this.genLeft()} +
+ {renderLeft()}
- {this.$slots.title ? this.$slots.title() : this.title} + {slots.title ? slots.title() : title}
-
- {this.genRight()} +
+ {renderRight()}
); - }, + }; - onClickLeft(event) { - this.$emit('click-left', event); - }, + return () => { + if (props.placeholder && props.fixed) { + return ( +
+ {renderNavBar()} +
+ ); + } - onClickRight(event) { - this.$emit('click-right', event); - }, - }, - - render() { - if (this.placeholder && this.fixed) { - return ( -
- {this.genNavBar()} -
- ); - } - - return this.genNavBar(); + return renderNavBar(); + }; }, });