types(Steps): use tsx (#8123)

This commit is contained in:
neverland 2021-02-10 20:37:00 +08:00 committed by GitHub
parent 5b352d0339
commit 3e08b2471a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 53 additions and 20 deletions

View File

@ -1,15 +1,29 @@
import { computed } from 'vue'; import { computed } from 'vue';
// Utils
import { createNamespace } from '../utils'; import { createNamespace } from '../utils';
import { BORDER } from '../utils/constant'; import { BORDER } from '../utils/constant';
import { STEPS_KEY } from '../steps'; import { STEPS_KEY, StepsProvide } from '../steps';
// Composition
import { useParent } from '@vant/use'; import { useParent } from '@vant/use';
// Components
import Icon from '../icon'; import Icon from '../icon';
const [createComponent, bem] = createNamespace('step'); const [createComponent, bem] = createNamespace('step');
export default createComponent({ export default createComponent({
setup(props, { slots }) { setup(props, { slots }) {
const { parent, index } = useParent(STEPS_KEY); const { parent, index } = useParent<StepsProvide>(STEPS_KEY);
if (!parent) {
if (process.env.NODE_ENV !== 'production') {
console.error('[Vant] Step must be a child component of Steps.');
}
return;
}
const parentProps = parent.props; const parentProps = parent.props;
const getStatus = () => { const getStatus = () => {
@ -17,9 +31,7 @@ export default createComponent({
if (index.value < active) { if (index.value < active) {
return 'finish'; return 'finish';
} }
if (index.value === active) { return index.value === active ? 'process' : 'waiting';
return 'process';
}
}; };
const isActive = () => getStatus() === 'process'; const isActive = () => getStatus() === 'process';
@ -41,7 +53,7 @@ export default createComponent({
}); });
const onClickStep = () => { const onClickStep = () => {
parent.emit('click-step', index.value); parent.onClickStep(index.value);
}; };
const renderCircle = () => { const renderCircle = () => {

View File

@ -1,3 +1,4 @@
import { PropType } from 'vue';
import { createNamespace } from '../utils'; import { createNamespace } from '../utils';
import { useChildren } from '@vant/use'; import { useChildren } from '@vant/use';
@ -5,6 +6,21 @@ const [createComponent, bem] = createNamespace('steps');
export const STEPS_KEY = 'vanSteps'; export const STEPS_KEY = 'vanSteps';
export type StepsDirection = 'horizontal' | 'vertical';
export type StepsProvide = {
props: {
active: number | string;
direction: StepsDirection;
activeIcon: string;
finishIcon?: string;
activeColor?: string;
inactiveIcon?: string;
inactiveColor?: string;
};
onClickStep: (index: number) => void;
};
export default createComponent({ export default createComponent({
props: { props: {
finishIcon: String, finishIcon: String,
@ -16,7 +32,7 @@ export default createComponent({
default: 0, default: 0,
}, },
direction: { direction: {
type: String, type: String as PropType<StepsDirection>,
default: 'horizontal', default: 'horizontal',
}, },
activeIcon: { activeIcon: {
@ -30,7 +46,14 @@ export default createComponent({
setup(props, { emit, slots }) { setup(props, { emit, slots }) {
const { linkChildren } = useChildren(STEPS_KEY); const { linkChildren } = useChildren(STEPS_KEY);
linkChildren({ emit, props }); const onClickStep = (index: number) => {
emit('click-step', index);
};
linkChildren({
props,
onClickStep,
});
return () => ( return () => (
<div class={bem([props.direction])}> <div class={bem([props.direction])}>

View File

@ -26,7 +26,7 @@ exports[`should render demo and match snapshot 1`] = `
<div class="van-step__line"> <div class="van-step__line">
</div> </div>
</div> </div>
<div class="van-hairline van-step van-step--horizontal"> <div class="van-hairline van-step van-step--horizontal van-step--waiting">
<div class="van-step__title"> <div class="van-step__title">
Step3 Step3
</div> </div>
@ -37,7 +37,7 @@ exports[`should render demo and match snapshot 1`] = `
<div class="van-step__line"> <div class="van-step__line">
</div> </div>
</div> </div>
<div class="van-hairline van-step van-step--horizontal"> <div class="van-hairline van-step van-step--horizontal van-step--waiting">
<div class="van-step__title"> <div class="van-step__title">
Step4 Step4
</div> </div>
@ -91,7 +91,7 @@ exports[`should render demo and match snapshot 1`] = `
<div class="van-step__line"> <div class="van-step__line">
</div> </div>
</div> </div>
<div class="van-hairline van-step van-step--horizontal"> <div class="van-hairline van-step van-step--horizontal van-step--waiting">
<div class="van-step__title"> <div class="van-step__title">
Step3 Step3
</div> </div>
@ -102,7 +102,7 @@ exports[`should render demo and match snapshot 1`] = `
<div class="van-step__line"> <div class="van-step__line">
</div> </div>
</div> </div>
<div class="van-hairline van-step van-step--horizontal"> <div class="van-hairline van-step van-step--horizontal van-step--waiting">
<div class="van-step__title"> <div class="van-step__title">
Step4 Step4
</div> </div>
@ -135,7 +135,7 @@ exports[`should render demo and match snapshot 1`] = `
<div class="van-step__line"> <div class="van-step__line">
</div> </div>
</div> </div>
<div class="van-hairline van-step van-step--vertical"> <div class="van-hairline van-step van-step--vertical van-step--waiting">
<div class="van-step__title"> <div class="van-step__title">
<h3> <h3>
【City】Status2 【City】Status2
@ -151,7 +151,7 @@ exports[`should render demo and match snapshot 1`] = `
<div class="van-step__line"> <div class="van-step__line">
</div> </div>
</div> </div>
<div class="van-hairline van-step van-step--vertical"> <div class="van-hairline van-step van-step--vertical van-step--waiting">
<div class="van-step__title"> <div class="van-step__title">
<h3> <h3>
【City】Status3 【City】Status3

View File

@ -30,10 +30,8 @@ exports[`should change inactive color when using inactive-color prop 1`] = `
> >
</div> </div>
</div> </div>
<div class="van-hairline van-step van-step--horizontal"> <div class="van-hairline van-step van-step--horizontal van-step--waiting">
<div class="van-step__title" <div class="van-step__title">
style="color: red;"
>
B B
</div> </div>
<div class="van-step__circle-container"> <div class="van-step__circle-container">
@ -52,7 +50,7 @@ exports[`should change inactive color when using inactive-color prop 1`] = `
`; `;
exports[`should change inactive icon when using inactive-icon prop 1`] = ` exports[`should change inactive icon when using inactive-icon prop 1`] = `
<div class="van-hairline van-step van-step--horizontal"> <div class="van-hairline van-step van-step--horizontal van-step--waiting">
<div class="van-step__title"> <div class="van-step__title">
B B
</div> </div>
@ -78,7 +76,7 @@ exports[`should render icon slot correctly 1`] = `
<div class="van-step__line"> <div class="van-step__line">
</div> </div>
</div> </div>
<div class="van-hairline van-step van-step--horizontal"> <div class="van-hairline van-step van-step--horizontal van-step--waiting">
<div class="van-step__title"> <div class="van-step__title">
A A
</div> </div>