refactor(Button): use setup

This commit is contained in:
chenjiahan 2020-08-24 10:52:53 +08:00
parent 2080071a82
commit 4d9b4b3e23
2 changed files with 103 additions and 83 deletions

View File

@ -46,61 +46,56 @@ export default createComponent({
emits: ['click'], emits: ['click'],
methods: { setup(props, { emit, slots }) {
onClick() { const renderLoadingIcon = () => {
if (!this.loading && !this.disabled) { if (slots.loading) {
this.$emit('click', event); return slots.loading();
route(this.$router, this);
} }
},
genContent() { return (
const Content = [];
if (this.loading) {
Content.push(
this.$slots.loading ? (
this.$slots.loading()
) : (
<Loading <Loading
class={bem('loading')} class={bem('loading')}
size={this.loadingSize} size={props.loadingSize}
type={this.loadingType} type={props.loadingType}
color="currentColor" color="currentColor"
/> />
)
); );
} else if (this.icon) { };
Content.push(
const renderIcon = () => {
if (props.loading) {
return renderLoadingIcon();
}
if (props.icon) {
return (
<Icon <Icon
name={this.icon} name={props.icon}
class={bem('icon')} class={bem('icon')}
classPrefix={this.iconPrefix} classPrefix={props.iconPrefix}
/> />
); );
} }
};
const renderText = () => {
let text; let text;
if (this.loading) { if (props.loading) {
text = this.loadingText; text = props.loadingText;
} else { } else {
text = this.$slots.default ? this.$slots.default() : this.text; text = slots.default ? slots.default() : props.text;
} }
if (text) { if (text) {
Content.push(<span class={bem('text')}>{text}</span>); return <span class={bem('text')}>{text}</span>;
} }
};
return Content; const getStyle = () => {
}, const { color, plain } = props;
}, if (color) {
render() {
const { tag, type, color, plain, disabled, loading, hairline } = this;
const style = {}; const style = {};
if (color) {
style.color = plain ? color : WHITE; style.color = plain ? color : WHITE;
if (!plain) { if (!plain) {
@ -114,20 +109,45 @@ export default createComponent({
} else { } else {
style.borderColor = color; style.borderColor = color;
} }
return style;
} }
};
return (vm) => {
const {
tag,
type,
size,
block,
round,
plain,
square,
loading,
disabled,
hairline,
nativeType,
} = props;
const onClick = () => {
if (!loading && !disabled) {
emit('click', event);
route(vm.$router, vm);
}
};
const classes = [ const classes = [
bem([ bem([
type, type,
this.size, size,
{ {
plain, plain,
block,
round,
square,
loading, loading,
disabled, disabled,
hairline, hairline,
block: this.block,
round: this.round,
square: this.square,
}, },
]), ]),
{ [BORDER_SURROUND]: hairline }, { [BORDER_SURROUND]: hairline },
@ -135,14 +155,18 @@ export default createComponent({
return ( return (
<tag <tag
style={style} type={nativeType}
class={classes} class={classes}
type={this.nativeType} style={getStyle()}
disabled={disabled} disabled={disabled}
onClick={this.onClick} onClick={onClick}
> >
<div class={bem('content')}>{this.genContent()}</div> <div class={bem('content')}>
{renderIcon()}
{renderText()}
</div>
</tag> </tag>
); );
};
}, },
}); });

View File

@ -14,14 +14,14 @@ const [createComponent, bem, t] = createNamespace('picker');
export default createComponent({ export default createComponent({
props: { props: {
...pickerProps, ...pickerProps,
defaultIndex: {
type: [Number, String],
default: 0,
},
columns: { columns: {
type: Array, type: Array,
default: () => [], default: () => [],
}, },
defaultIndex: {
type: [Number, String],
default: 0,
},
toolbarPosition: { toolbarPosition: {
type: String, type: String,
default: 'top', default: 'top',
@ -135,11 +135,7 @@ export default createComponent({
} }
if (this.dataType === 'text') { if (this.dataType === 'text') {
this.$emit( this.$emit('change', this.getColumnValue(0), this.getColumnIndex(0));
'change',
this.getColumnValue(0),
this.getColumnIndex(0)
);
} else { } else {
this.$emit('change', this.getValues(), columnIndex); this.$emit('change', this.getValues(), columnIndex);
} }