refactor(Empty): use setup

This commit is contained in:
chenjiahan 2020-08-25 11:19:24 +08:00
parent 30269be5b5
commit 9627da3f8c

View File

@ -3,7 +3,7 @@ import Network from './Network';
const [createComponent, bem] = createNamespace('empty'); const [createComponent, bem] = createNamespace('empty');
const PRESETS = ['error', 'search', 'default']; const PRESET_IMAGES = ['error', 'search', 'default'];
export default createComponent({ export default createComponent({
props: { props: {
@ -14,56 +14,46 @@ export default createComponent({
}, },
}, },
methods: { setup(props, { slots }) {
genImageContent() { const renderImage = () => {
const slots = this.$slots.image?.(); if (slots.image) {
return slots.image();
if (slots) {
return slots;
} }
if (this.image === 'network') { let { image } = props;
if (image === 'network') {
return <Network />; return <Network />;
} }
let { image } = this; if (PRESET_IMAGES.indexOf(image) !== -1) {
if (PRESETS.indexOf(image) !== -1) {
image = `https://img.yzcdn.cn/vant/empty-image-${image}.png`; image = `https://img.yzcdn.cn/vant/empty-image-${image}.png`;
} }
return <img src={image} />; return <img src={image} />;
}, };
genImage() { const renderDescription = () => {
return <div class={bem('image')}>{this.genImageContent()}</div>; const description = slots.description
}, ? slots.description()
: props.description;
genDescription() {
const description = this.$slots.description
? this.slot.description()
: this.description;
if (description) { if (description) {
return <p class={bem('description')}>{description}</p>; return <p class={bem('description')}>{description}</p>;
} }
}, };
genBottom() { const renderBottom = () => {
const slot = this.$slots.default?.(); if (slots.default) {
return <div class={bem('bottom')}>{slots.default()}</div>;
if (slot) {
return <div class={bem('bottom')}>{slot}</div>;
} }
}, };
},
render() { return () => (
return (
<div class={bem()}> <div class={bem()}>
{this.genImage()} <div class={bem('image')}>{renderImage()}</div>
{this.genDescription()} {renderDescription()}
{this.genBottom()} {renderBottom()}
</div> </div>
); );
}, },