mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
refactor(Steps): refactor with composition api
This commit is contained in:
parent
1237ee6c2c
commit
e3cf460762
@ -1,52 +1,63 @@
|
|||||||
|
import { computed } from 'vue';
|
||||||
import { createNamespace } from '../utils';
|
import { createNamespace } from '../utils';
|
||||||
import { BORDER } from '../utils/constant';
|
import { BORDER } from '../utils/constant';
|
||||||
import { ChildrenMixin } from '../mixins/relation';
|
import { STEPS_KEY } from '../steps';
|
||||||
|
import { useParent } from '../composition/use-relation';
|
||||||
import Icon from '../icon';
|
import Icon from '../icon';
|
||||||
|
|
||||||
const [createComponent, bem] = createNamespace('step');
|
const [createComponent, bem] = createNamespace('step');
|
||||||
|
|
||||||
export default createComponent({
|
export default createComponent({
|
||||||
mixins: [ChildrenMixin('vanSteps')],
|
setup(props, { slots }) {
|
||||||
|
const { parent, index } = useParent(STEPS_KEY);
|
||||||
|
|
||||||
computed: {
|
const getStatus = () => {
|
||||||
status() {
|
const active = +parent.props.active;
|
||||||
if (this.index < this.parent.active) {
|
if (index.value < active) {
|
||||||
return 'finish';
|
return 'finish';
|
||||||
}
|
}
|
||||||
if (this.index === +this.parent.active) {
|
if (index.value === active) {
|
||||||
return 'process';
|
return 'process';
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
|
|
||||||
active() {
|
const isActive = () => getStatus() === 'process';
|
||||||
return this.status === 'process';
|
|
||||||
},
|
|
||||||
|
|
||||||
lineStyle() {
|
const lineStyle = computed(() => {
|
||||||
if (this.status === 'finish') {
|
const { activeColor, inactiveColor } = parent.props;
|
||||||
return { background: this.parent.activeColor };
|
|
||||||
|
if (getStatus() === 'finish') {
|
||||||
|
return { background: activeColor };
|
||||||
}
|
}
|
||||||
return { background: this.parent.inactiveColor };
|
|
||||||
},
|
|
||||||
|
|
||||||
titleStyle() {
|
return { background: inactiveColor };
|
||||||
if (this.active) {
|
});
|
||||||
return { color: this.parent.activeColor };
|
|
||||||
|
const titleStyle = computed(() => {
|
||||||
|
const { activeColor, inactiveColor } = parent.props;
|
||||||
|
|
||||||
|
if (isActive()) {
|
||||||
|
return { color: activeColor };
|
||||||
}
|
}
|
||||||
if (!this.status) {
|
|
||||||
return { color: this.parent.inactiveColor };
|
if (!getStatus()) {
|
||||||
|
return { color: inactiveColor };
|
||||||
}
|
}
|
||||||
},
|
});
|
||||||
},
|
|
||||||
|
|
||||||
methods: {
|
const onClickStep = () => {
|
||||||
genCircle() {
|
parent.emit('click-step', index.value);
|
||||||
const { activeIcon, activeColor, inactiveIcon } = this.parent;
|
};
|
||||||
|
|
||||||
if (this.active) {
|
const renderCircle = () => {
|
||||||
return this.$slots['active-icon'] ? (
|
const { activeIcon, activeColor, inactiveIcon } = parent.props;
|
||||||
this.$slots['active-icon']()
|
|
||||||
) : (
|
if (isActive()) {
|
||||||
|
if (slots['active-icon']) {
|
||||||
|
return slots['active-icon']();
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
<Icon
|
<Icon
|
||||||
class={bem('icon', 'active')}
|
class={bem('icon', 'active')}
|
||||||
name={activeIcon}
|
name={activeIcon}
|
||||||
@ -55,40 +66,36 @@ export default createComponent({
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (inactiveIcon || this.$slots['inactive-icon']) {
|
if (slots['inactive-icon']) {
|
||||||
return this.$slots['inactive-icon'] ? (
|
return slots['inactive-icon']();
|
||||||
this.$slots['inactive-icon']()
|
|
||||||
) : (
|
|
||||||
<Icon class={bem('icon')} name={inactiveIcon} />
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return <i class={bem('circle')} style={this.lineStyle} />;
|
if (inactiveIcon) {
|
||||||
},
|
return <Icon class={bem('icon')} name={inactiveIcon} />;
|
||||||
|
}
|
||||||
|
|
||||||
onClickStep() {
|
return <i class={bem('circle')} style={lineStyle.value} />;
|
||||||
this.parent.$emit('click-step', this.index);
|
};
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
render() {
|
return () => {
|
||||||
const { status, active } = this;
|
const { direction } = parent.props;
|
||||||
const { direction } = this.parent;
|
const status = getStatus();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div class={[BORDER, bem([direction, { [status]: status }])]}>
|
<div class={[BORDER, bem([direction, { [status]: status }])]}>
|
||||||
<div
|
<div
|
||||||
class={bem('title', { active })}
|
class={bem('title', { active: isActive() })}
|
||||||
style={this.titleStyle}
|
style={titleStyle.value}
|
||||||
onClick={this.onClickStep}
|
onClick={onClickStep}
|
||||||
>
|
>
|
||||||
{this.$slots.default?.()}
|
{slots.default?.()}
|
||||||
|
</div>
|
||||||
|
<div class={bem('circle-container')} onClick={onClickStep}>
|
||||||
|
{renderCircle()}
|
||||||
|
</div>
|
||||||
|
<div class={bem('line')} style={lineStyle.value} />
|
||||||
</div>
|
</div>
|
||||||
<div class={bem('circle-container')} onClick={this.onClickStep}>
|
);
|
||||||
{this.genCircle()}
|
};
|
||||||
</div>
|
|
||||||
<div class={bem('line')} style={this.lineStyle} />
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
|
import { provide, reactive } from 'vue';
|
||||||
import { createNamespace } from '../utils';
|
import { createNamespace } from '../utils';
|
||||||
import { ParentMixin } from '../mixins/relation';
|
|
||||||
|
|
||||||
const [createComponent, bem] = createNamespace('steps');
|
const [createComponent, bem] = createNamespace('steps');
|
||||||
|
|
||||||
export default createComponent({
|
export const STEPS_KEY = 'vanSteps';
|
||||||
mixins: [ParentMixin('vanSteps')],
|
|
||||||
|
|
||||||
|
export default createComponent({
|
||||||
props: {
|
props: {
|
||||||
activeColor: String,
|
activeColor: String,
|
||||||
inactiveIcon: String,
|
inactiveIcon: String,
|
||||||
@ -26,10 +26,18 @@ export default createComponent({
|
|||||||
|
|
||||||
emits: ['click-step'],
|
emits: ['click-step'],
|
||||||
|
|
||||||
render() {
|
setup(props, { emit, slots }) {
|
||||||
return (
|
const children = reactive([]);
|
||||||
<div class={bem([this.direction])}>
|
|
||||||
<div class={bem('items')}>{this.$slots.default?.()}</div>
|
provide(STEPS_KEY, {
|
||||||
|
emit,
|
||||||
|
props,
|
||||||
|
children,
|
||||||
|
});
|
||||||
|
|
||||||
|
return () => (
|
||||||
|
<div class={bem([props.direction])}>
|
||||||
|
<div class={bem('items')}>{slots.default?.()}</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user