forked from srdjan/react-multistep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreact-multistep.js
161 lines (143 loc) · 4.06 KB
/
react-multistep.js
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
import React, { useState } from 'react'
import { css, styled, setup } from 'goober'
setup(React.createElement)
const Ol = styled('ol')`
margin: 0;
padding-bottom: 2.2rem;
list-style-type: none;
`
const LiClass = props => css`
display: inline-block;
text-align: center;
line-height: 4.5rem;
padding: 0 0.7rem;
cursor: pointer;
color: ${props.state === 'todo' ? 'silver': 'black'};
border-bottom: 4px solid ${props.state === 'todo' ? 'silver' : '#33C3F0'};
&:before {
position: relative;
bottom: -3.99rem;
float: left;
left: 50%;
${props.state === 'todo' ? 'content: "\u039F";' :
props.state === 'doing' ? 'content: "\u2022";' :
'content: "\u2713";'}
color: ${props.state === 'todo' ? 'silver' : 'white'};
background-color: ${props.state === 'todo' ? 'white' : '#33C3F0'};
width: 1.2em;
line-height: ${props.state === 'todo' ? '1.2em' : '1.4em'};
border-radius: ${props.state === 'todo' ? '0' : '1.2em'};
}
&:hover,
&::before {
color: #0FA0CE;
}
&:after {
content: "\\00a0\\00a0";
}
span {
padding: 0 1.5rem;
}
`
const getTopNavStyles = (indx, length) => {
let styles = []
for (let i = 0; i < length; i++) {
if (i < indx) {
styles.push('done')
} else if (i === indx) {
styles.push('doing')
} else {
styles.push('todo')
}
}
return styles
}
const getButtonsState = (indx, length) => {
if (indx > 0 && indx < length - 1) {
return {
showPreviousBtn: true,
showNextBtn: true
}
} else if (indx === 0) {
return {
showPreviousBtn: false,
showNextBtn: true
}
} else {
return {
showPreviousBtn: true,
showNextBtn: false
}
}
}
export default function MultiStep(props) {
let showNav = true
const { activeComponentClassName, inactiveComponentClassName } = props;
if (props.showNavigation) showNav = props.showNavigation
let prevStyle = {}
if (props.prevStyle) prevStyle = props.prevStyle
let nextStyle = {}
if (props.nextStyle) nextStyle = props.nextStyle
const [stylesState, setStyles] = useState(getTopNavStyles(0, props.steps.length))
const [compState, setComp] = useState(0)
const [buttonsState, setButtons] = useState(getButtonsState(0, props.steps.length))
const setStepState = (indx) => {
setStyles(getTopNavStyles(indx, props.steps.length))
setComp(indx < props.steps.length ? indx : compState)
setButtons(getButtonsState(indx, props.steps.length))
}
const next = () => setStepState(compState + 1)
const previous = () => setStepState(compState > 0 ? compState - 1 : compState)
const handleKeyDown = evt => evt.which === 13 ? next(props.steps.length) : {}
const handleOnClick = evt => {
if (
evt.currentTarget.value === props.steps.length - 1 &&
compState === props.steps.length - 1
) {
setStepState(props.steps.length)
} else {
setStepState(evt.currentTarget.value)
}
}
const renderSteps = () =>
props.steps.map((s, i) => (
<li
className={LiClass({state: stylesState[i]})}
onClick={handleOnClick}
key={i}
value={i}
>
<span>{i+1}</span>
</li>
))
const renderNav = (show) =>
show && (
<div>
<button
style={buttonsState.showPreviousBtn ? props.prevStyle : { display: 'none' }}
onClick={previous}
>
Prev
</button>
<button
style={buttonsState.showNextBtn ? props.nextStyle : { display: 'none' }}
onClick={next}
>
Next
</button>
</div>
)
return (
<div onKeyDown={handleKeyDown}>
<Ol>{renderSteps()}</Ol>
{inactiveComponentClassName ?
props.steps.map((step, index) => {
const className = index === compState ? activeComponentClassName : inactiveComponentClassName
return (<div className={className}>{step.component}</div>)
}) :
<div>{props.steps[compState].component}</div>
}
<div>{renderNav(showNav)}</div>
</div>
)
}