Skip to content

Commit ee4f209

Browse files
committed
First viable version v1.0.0
1 parent f963ecc commit ee4f209

7 files changed

Lines changed: 80 additions & 101 deletions

File tree

README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Using [npm](https://www.npmjs.com/):
2121
## Usage
2222

2323
```js
24-
<Steps totalSteps={3}>
24+
<Steps total={3}>
2525
<Step order={1} component={Step1} />
2626
<Step order={2} component={Step2} />
2727
<Step order={3} component={Step3} />
@@ -33,11 +33,11 @@ Using [npm](https://www.npmjs.com/):
3333

3434
### `<Steps />`
3535

36-
This is the wrapper component. You must define every step component under `<Steps />` component. It takes `totalSteps` as a prop which must match the number of `Step` components inside.
36+
This is the wrapper component. You must define every step component under `<Steps />` component. It takes `total` as a prop which must match the number of `Step` components inside.
3737

38-
| Properties | |
39-
| -------------------------- | ------------------------------------------- |
40-
| **`totalSteps`** _integer_ | Define how many steps you will be creating. |
38+
| Properties | |
39+
| --------------------- | ------------------------------------------- |
40+
| **`total`** _integer_ | Define how many steps you will be creating. |
4141

4242
<br />
4343
<br />
@@ -133,8 +133,8 @@ Example usages:
133133
| Properties | --- |
134134
| -------------------- | ----------------------------------------------------------------------------------------------------- |
135135
| **`text`** _String_ | the text that will button render |
136-
| **`prev`** _Boolean_ | gives the button 'Go to Previous Step' functionality. It doesn't show in the first step component. |
137-
| **`next`** _Boolean_ | gives the button 'Go to Next Step' functionality. It doesn't show in the last step component. |
136+
| **`prev`** _Boolean_ | gives the button 'Go to Previous Step' functionality. Disabled in the first step. |
137+
| **`next`** _Boolean_ | gives the button 'Go to Next Step' functionality. Disabled in the last step. |
138138
| _any props_ | all the other props passed to the **Button** component will be passed any props to the button element |
139139

140140
<br />
@@ -152,8 +152,8 @@ Example usages:
152152
<Navigation before="HEY " after=" HI" /> // if you have 3 steps, it renders 3 buttons, each step number has 'HEY' before and 'HI' after (HEY 1 HI, HEY 2 HI, HEY 3 HI)
153153
```
154154

155-
| Properties | --- |
156-
| --------------------- | ------------------------------------------------------- |
157-
| **`text`** _String_ | the text that each button will show |
158-
| **`before`** _String_ | the text that goes before the step number in the button |
159-
| **`after`** _String_ | the text that goes after the step number in the button |
155+
| Properties | --- |
156+
| --------------------- | ------------------------------------------------------------------------- |
157+
| **`text`** _String_ | the text that each button will show, disables `before` and `after` props. |
158+
| **`before`** _String_ | the text that goes before the step number in the button. |
159+
| **`after`** _String_ | the text that goes after the step number in the button. |

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "react-step-builder",
33
"description": "Unopinionated multi step interface builder.",
44
"author": "Samet Mutevelli <mutevellisamet@gmail.com> (https://sametmutevelli.com)",
5-
"version": "1.0.33",
5+
"version": "1.1.0",
66
"private": false,
77
"dependencies": {
88
"react": "^16.12.0",

src/index.js

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import React, { useEffect } from "react";
1+
import React from "react";
22
import ReactDOM from "react-dom";
33
import { Steps, Step, Button, Navigation } from "./lib";
44

55
import "./style.css";
66

77
const Step1 = props => {
88
return (
9-
<div>
9+
<div className="step">
1010
First Name:{" "}
1111
<input
1212
name="firstname"
@@ -25,7 +25,7 @@ const Step1 = props => {
2525

2626
const Step2 = props => {
2727
return (
28-
<div>
28+
<div className="step">
2929
Email:{" "}
3030
<input
3131
name="email"
@@ -44,7 +44,7 @@ const Step2 = props => {
4444

4545
const Step3 = props => {
4646
return (
47-
<div>
47+
<div className="step">
4848
Address:{" "}
4949
<input
5050
name="address"
@@ -63,33 +63,48 @@ const Step3 = props => {
6363

6464
const Step4 = props => {
6565
return (
66-
<div>
66+
<div className="step">
67+
<p>
68+
<strong>Name:</strong> {props.mainState.firstname}{" "}
69+
{props.mainState.lastname}
70+
</p>
71+
<p>
72+
<strong>Email:</strong> {props.mainState.email}
73+
</p>
74+
<p>
75+
<strong>Password:</strong> {props.mainState.password}
76+
</p>
77+
<p>
78+
<strong>Address:</strong> {props.mainState.address}
79+
</p>
6780
<p>
68-
Name: {props.mainState.firstname} {props.mainState.lastname}
81+
<strong>Phone:</strong> {props.mainState.phone}
6982
</p>
70-
<p>Email: {props.mainState.email}</p>
71-
<p>Password: {props.mainState.password}</p>
72-
<p>Address: {props.mainState.address}</p>
73-
<p>Phone: {props.mainState.phone}</p>
7483
</div>
7584
);
7685
};
7786

7887
const Persistent = () => {
7988
return (
8089
<div>
81-
<Button prev text="Prev" />
82-
<Button next text="Next" />
83-
<Navigation before="This is " after=". step" />
90+
<div className="navigation">
91+
<Button prev text="Prev" />
92+
<Navigation
93+
text="&#x25C9;"
94+
active="active-button"
95+
visited="visited-button"
96+
/>
97+
<Button next text="Next" />
98+
</div>
8499
</div>
85100
);
86101
};
87102

88103
const App = () => {
89104
return (
90105
<div className="steps_wrapper">
91-
<Steps totalSteps={4} lockMode>
92-
<h1>React Step Builder</h1>
106+
<Steps total={4}>
107+
<h1>React Step Builder v1.1.0</h1>
93108
<Step order={1} component={Step1} />
94109
<Step order={2} component={Step2} />
95110
<Step order={3} component={Step3} />
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,33 @@ import { StepContext } from "./Steps";
44
export const Button = ({ next, prev, text, ...theRest }) => {
55
const context = useContext(StepContext);
66
const { prevStep, nextStep, steps } = context;
7-
8-
return (prev && steps.current === 1) ||
9-
(next && steps.current === steps.total) ? null : (
7+
const { current, total } = steps;
8+
return (
109
<button
1110
onClick={next ? nextStep : prev ? prevStep : null}
12-
disabled={next && steps.current + 1 === steps.lock}
11+
disabled={(prev && current === 1) || (next && current === total)}
1312
{...theRest}
1413
>
1514
{text}
1615
</button>
1716
);
1817
};
1918

20-
export const Navigation = ({ text, before, after }) => {
19+
export const Navigation = ({ text, before, after, active, visited }) => {
2120
const context = useContext(StepContext);
2221
const { jumpToStep, steps } = context;
2322
const stepsArray = [...Array(steps.total).keys()].map(x => x + 1);
2423

25-
return stepsArray.map(step => {
26-
return (
27-
<button
28-
key={step}
29-
onClick={() => jumpToStep(step)}
30-
disabled={step >= steps.lock} //steps.current === step ||
31-
>
32-
{text || `${before || ""}${step}${after || ""}`}
33-
</button>
34-
);
35-
});
24+
return stepsArray.map(step => (
25+
<button
26+
key={step}
27+
onClick={() => jumpToStep(step)}
28+
disabled={steps.current === step}
29+
className={
30+
step === steps.current ? active : step < steps.current ? visited : null
31+
}
32+
>
33+
{text || `${before || ""}${step}${after || ""}`}
34+
</button>
35+
));
3636
};

src/lib/Step.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/lib/Steps.js

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,61 +4,39 @@ export const StepContext = React.createContext();
44

55
const useMainState = INITIAL_VALUE => {
66
const [state, setState] = React.useState(INITIAL_VALUE);
7-
87
const setMainState = newState => setState({ ...state, ...newState });
9-
10-
const handleChange = event =>
8+
const handleChange = event => {
119
setState({ ...state, [event.target.name]: event.target.value });
12-
10+
};
1311
return [state, setMainState, handleChange];
1412
};
1513

16-
export const useStepNavigation = (totalSteps, lockMode) => {
17-
const [currentStep, setCurrentStep] = React.useState(1);
18-
19-
const prevStep = () => setCurrentStep(currentStep > 2 ? currentStep - 1 : 1);
20-
const nextStep = () =>
21-
setCurrentStep(currentStep < totalSteps ? currentStep + 1 : totalSteps);
22-
const jumpToStep = step => setCurrentStep(Number(step));
23-
24-
return [
25-
{ current: currentStep, total: totalSteps, lock: currentStep + 1 },
26-
prevStep,
27-
nextStep,
28-
jumpToStep
29-
];
14+
export const useStepNavigation = total => {
15+
const [current, setCurrent] = React.useState(1);
16+
const prevStep = () => setCurrent(current > 2 ? current - 1 : 1);
17+
const nextStep = () => setCurrent(current < total ? current + 1 : total);
18+
const jumpToStep = step => setCurrent(Number(step));
19+
return [{ current, total }, prevStep, nextStep, jumpToStep];
3020
};
3121

32-
const Steps = ({ totalSteps, lockMode, children }) => {
22+
export const Step = ({ component: Component, order, persist }) => {
23+
const context = React.useContext(StepContext);
24+
const { current } = context.steps;
25+
return order === current || persist ? <Component {...context} /> : false;
26+
};
27+
const Steps = ({ total, children }) => {
3328
const [mainState, setMainState, handleChange] = useMainState({});
34-
const [steps, prevStep, nextStep, jumpToStep] = useStepNavigation(
35-
totalSteps,
36-
lockMode
37-
);
38-
console.log({
29+
const [steps, prevStep, nextStep, jumpToStep] = useStepNavigation(total);
30+
const value = {
3931
mainState,
4032
setMainState,
4133
handleChange,
42-
steps: { total: steps.total, current: steps.current, lock: steps.lock },
34+
steps,
4335
prevStep,
4436
nextStep,
4537
jumpToStep
46-
});
47-
return (
48-
<StepContext.Provider
49-
value={{
50-
mainState,
51-
setMainState,
52-
handleChange,
53-
steps: { total: steps.total, current: steps.current, lock: steps.lock },
54-
prevStep,
55-
nextStep,
56-
jumpToStep
57-
}}
58-
>
59-
{children}
60-
</StepContext.Provider>
61-
);
38+
};
39+
return <StepContext.Provider value={value}>{children}</StepContext.Provider>;
6240
};
6341

6442
export default Steps;

src/lib/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import Steps from "./Steps";
2-
import Step from "./Step";
3-
import { Button, Navigation } from "./Navigators";
1+
import Steps, { Step } from "./Steps";
2+
import { Button, Navigation } from "./Components";
43
export { Steps, Step, Button, Navigation };

0 commit comments

Comments
 (0)