/
Stepper.tsx
163 lines (137 loc) · 4.66 KB
/
Stepper.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import React, { forwardRef, Children, cloneElement } from 'react';
import {
MantineColor,
DefaultProps,
MantineNumberSize,
MantineSize,
Selectors,
useComponentDefaultProps,
} from '@mantine/styles';
import { ForwardRefWithStaticComponents } from '@mantine/utils';
import { Box } from '../Box';
import { Step, StepStylesNames } from './Step/Step';
import { StepCompleted } from './StepCompleted/StepCompleted';
import useStyles from './Stepper.styles';
export type StepperStylesNames = Selectors<typeof useStyles> | StepStylesNames;
export interface StepperProps
extends DefaultProps<StepperStylesNames>,
React.ComponentPropsWithRef<'div'> {
/** <Stepper.Step /> components only */
children: React.ReactNode;
/** Called when step is clicked */
onStepClick?(stepIndex: number): void;
/** Active step index */
active: number;
/** Step icon displayed when step is completed */
completedIcon?: React.ReactNode;
/** Step icon displayed when step is in progress */
progressIcon?: React.ReactNode;
/** Active and progress Step colors from theme.colors */
color?: MantineColor;
/** Step icon size in px */
iconSize?: number;
/** Content padding-top from theme.spacing or number to set value in px */
contentPadding?: MantineNumberSize;
/** Component orientation */
orientation?: 'vertical' | 'horizontal';
/** Icon position relative to step body */
iconPosition?: 'right' | 'left';
/** Component size */
size?: MantineSize;
/** Radius from theme.radius, or number to set border-radius in px */
radius?: MantineNumberSize;
/** Breakpoint at which orientation will change from horizontal to vertical */
breakpoint?: MantineNumberSize;
}
type StepperComponent = ForwardRefWithStaticComponents<
StepperProps,
{
Step: typeof Step;
Completed: typeof StepCompleted;
}
>;
const defaultProps: Partial<StepperProps> = {
contentPadding: 'md',
size: 'md',
radius: 'xl',
orientation: 'horizontal',
iconPosition: 'left',
};
export const Stepper: StepperComponent = forwardRef<HTMLDivElement, StepperProps>((props, ref) => {
const {
className,
children,
onStepClick,
active,
completedIcon,
progressIcon,
color,
iconSize,
contentPadding,
size,
radius,
orientation,
breakpoint,
iconPosition,
classNames,
styles,
unstyled,
...others
} = useComponentDefaultProps('Stepper', defaultProps, props);
const { classes, cx } = useStyles(
{ contentPadding, color, orientation, iconPosition, size, iconSize, breakpoint },
{ classNames, styles, unstyled, name: 'Stepper' }
);
const convertedChildren = Children.toArray(children) as React.ReactElement[];
const _children = convertedChildren.filter((child) => child.type !== StepCompleted);
const completedStep = convertedChildren.find((item) => item.type === StepCompleted);
const items = _children.reduce<React.ReactElement[]>((acc, item, index) => {
const shouldAllowSelect =
typeof item.props.allowStepSelect === 'boolean'
? item.props.allowStepSelect
: typeof onStepClick === 'function';
acc.push(
cloneElement(item, {
__staticSelector: 'Stepper',
icon: item.props.icon || index + 1,
key: index,
state:
active === index ? 'stepProgress' : active > index ? 'stepCompleted' : 'stepInactive',
onClick: () => shouldAllowSelect && typeof onStepClick === 'function' && onStepClick(index),
allowStepClick: shouldAllowSelect && typeof onStepClick === 'function',
completedIcon: item.props.completedIcon || completedIcon,
progressIcon: item.props.progressIcon || progressIcon,
color: item.props.color || color,
iconSize,
size,
radius,
classNames,
styles,
iconPosition: item.props.iconPosition || iconPosition,
orientation,
unstyled,
})
);
if (orientation === 'horizontal' && index !== _children.length - 1) {
acc.push(
<div
className={cx(classes.separator, { [classes.separatorActive]: index < active })}
key={`separator-${index}`}
/>
);
}
return acc;
}, []);
const stepContent = _children[active]?.props?.children;
const completedContent = completedStep?.props?.children;
const content = active > _children.length - 1 ? completedContent : stepContent;
return (
<Box className={cx(classes.root, className)} ref={ref} {...others}>
<div className={classes.steps}>{items}</div>
{content && <div className={classes.content}>{content}</div>}
</Box>
);
}) as any;
Stepper.Step = Step;
Stepper.Completed = StepCompleted;
Stepper.displayName = '@mantine/core/Stepper';