refactor(Loading): refactor with composition api

This commit is contained in:
chenjiahan 2020-08-25 14:12:41 +08:00
parent a528023405
commit ce27bcdf9b

View File

@ -1,8 +1,21 @@
// Utils import { computed } from 'vue';
import { createNamespace, addUnit } from '../utils'; import { createNamespace, addUnit } from '../utils';
const [createComponent, bem] = createNamespace('loading'); const [createComponent, bem] = createNamespace('loading');
export default createComponent({
props: {
size: [Number, String],
color: String,
vertical: Boolean,
textSize: [Number, String],
type: {
type: String,
default: 'circular',
},
},
setup(props, { slots }) {
const SpinIcon = []; const SpinIcon = [];
for (let i = 0; i < 12; i++) { for (let i = 0; i < 12; i++) {
SpinIcon.push(<i />); SpinIcon.push(<i />);
@ -14,51 +27,46 @@ const CircularIcon = (
</svg> </svg>
); );
export default createComponent({ const renderText = () => {
props: { if (slots.default) {
color: String,
size: [Number, String],
vertical: Boolean,
textSize: [Number, String],
type: {
type: String,
default: 'circular',
},
},
methods: {
genLoadingText() {
if (this.$slots.default) {
const style = this.textSize && {
fontSize: addUnit(this.textSize),
};
return ( return (
<span class={bem('text')} style={style}> <span
{this.$slots.default()} class={bem('text')}
style={{
fontSize: addUnit(props.textSize),
}}
>
{slots.default()}
</span> </span>
); );
} }
}, };
},
render() { const spinnerStyle = computed(() => {
const { color, size, type, vertical } = this; const style = {
color: props.color,
};
const style = { color }; if (props.size) {
if (size) { const size = addUnit(props.size);
const iconSize = addUnit(size); style.width = size;
style.width = iconSize; style.height = size;
style.height = iconSize;
} }
return style;
});
return () => {
const { type, vertical } = props;
return ( return (
<div class={bem([type, { vertical }])}> <div class={bem([type, { vertical }])}>
<span class={bem('spinner', type)} style={style}> <span class={bem('spinner', type)} style={spinnerStyle.value}>
{type === 'spinner' ? SpinIcon : CircularIcon} {type === 'spinner' ? SpinIcon : CircularIcon}
</span> </span>
{this.genLoadingText()} {renderText()}
</div> </div>
); );
};
}, },
}); });