Skip to content

Commit 41b3747

Browse files
day jonasschmedtmann#9 : Children Prop
1 parent 662fe55 commit 41b3747

2 files changed

Lines changed: 96 additions & 15 deletions

File tree

04-steps/self/src/App-v1.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { useState } from "react";
2+
3+
const messages = [
4+
"Learn React ⚛️",
5+
"Apply for jobs 💼",
6+
"Invest your new income 🤑",
7+
];
8+
9+
export default function App() {
10+
return (
11+
<div>
12+
<Steps />
13+
<Steps />
14+
</div>
15+
);
16+
}
17+
18+
function Steps() {
19+
const [step, setStep] = useState(1);
20+
const [isOpen, setIsOpen] = useState(true);
21+
22+
function handlePrevious() {
23+
if (step > 1) setStep((s) => s - 1);
24+
}
25+
26+
function handleNext() {
27+
if (step < 3) setStep((s) => s + 1);
28+
}
29+
30+
return (
31+
<>
32+
<button className="close" onClick={() => setIsOpen((io) => !io)}>
33+
&times;
34+
</button>
35+
{isOpen && (
36+
<div className="steps">
37+
<div className="numbers">
38+
<div className={step >= 1 ? "active" : ""}>1</div>
39+
<div className={step >= 2 ? "active" : ""}>2</div>
40+
<div className={step >= 3 ? "active" : ""}>3</div>
41+
</div>
42+
43+
<p className="message">
44+
Step {step}: {messages[step - 1]}
45+
</p>
46+
<div className="buttons">
47+
<button
48+
style={{ backgroundColor: "#7950f2", color: "#fff" }}
49+
onClick={handlePrevious}
50+
>
51+
Previous
52+
</button>
53+
<button
54+
style={{ backgroundColor: "#7950f2", color: "#fff" }}
55+
onClick={handleNext}
56+
>
57+
Next
58+
</button>
59+
</div>
60+
</div>
61+
)}
62+
</>
63+
);
64+
}

04-steps/self/src/App.js

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default function App() {
1010
return (
1111
<div>
1212
<Steps />
13-
<Steps />
13+
{/* <Steps /> */}
1414
</div>
1515
);
1616
}
@@ -40,25 +40,42 @@ function Steps() {
4040
<div className={step >= 3 ? "active" : ""}>3</div>
4141
</div>
4242

43-
<p className="message">
44-
Step {step}: {messages[step - 1]}
45-
</p>
43+
<StepMessage step={step}>{messages[step - 1]}</StepMessage>
44+
4645
<div className="buttons">
47-
<button
48-
style={{ backgroundColor: "#7950f2", color: "#fff" }}
49-
onClick={handlePrevious}
50-
>
51-
Previous
52-
</button>
53-
<button
54-
style={{ backgroundColor: "#7950f2", color: "#fff" }}
55-
onClick={handleNext}
46+
<Button
47+
bgColor="#7950f2"
48+
textColor="#fff"
49+
handleClick={handlePrevious}
5650
>
57-
Next
58-
</button>
51+
<span>👈</span>Previous
52+
</Button>
53+
<Button bgColor="#7950f2" textColor="#fff" handleClick={handleNext}>
54+
Next<span>👉</span>
55+
</Button>
5956
</div>
6057
</div>
6158
)}
6259
</>
6360
);
6461
}
62+
63+
function StepMessage({ step, children }) {
64+
return (
65+
<div className="message">
66+
<h3>Step {step}</h3>
67+
{children}
68+
</div>
69+
);
70+
}
71+
72+
function Button({ handleClick, bgColor, textColor, children }) {
73+
return (
74+
<button
75+
style={{ backgroundColor: bgColor, color: textColor }}
76+
onClick={handleClick}
77+
>
78+
{children}
79+
</button>
80+
);
81+
}

0 commit comments

Comments
 (0)