Skip to content

Commit 33f9912

Browse files
Implemented Custom Input Component
1 parent ce6b945 commit 33f9912

File tree

8 files changed

+120
-33
lines changed

8 files changed

+120
-33
lines changed

package-lock.json

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"@testing-library/react": "^11.2.7",
88
"@testing-library/user-event": "^12.8.3",
99
"chart.js": "^3.3.2",
10+
"font-awesome": "^4.7.0",
1011
"react": "^17.0.2",
1112
"react-chartjs-2": "^3.0.3",
1213
"react-dom": "^17.0.2",

src/App.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function App() {
1717
const [color, setColor] = useState("red");
1818
const [completed, setCompleted] = useState([]);
1919
const [size, setSize] = useState(10);
20-
const [speed, setSpeed] = useState(1);
20+
const [speed, setSpeed] = useState(3);
2121
const [algType, setAlgType] = useState(0);
2222

2323
useEffect(() => {
@@ -44,7 +44,7 @@ function App() {
4444
setIsSorting(false);
4545
}
4646
},
47-
(500 * c) / speed,
47+
200 * c * speed,
4848
[...arr],
4949
i,
5050
j,

src/components/Button/Button.css

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@
33
height: 3rem;
44
border-radius: 10px;
55
border: none;
6-
background-color: rgba(199, 199, 204, 0.8);
76
cursor: pointer;
87
font-size: 1rem;
9-
}
10-
11-
.button:hover {
12-
background-color: rgba(199, 199, 204, 1);
8+
background-color: transparent;
139
}
1410

1511
.sort {
@@ -23,7 +19,5 @@
2319
}
2420

2521
.button:disabled {
26-
background-color: rgb(142, 142, 147);
27-
color: black;
2822
cursor: not-allowed;
2923
}

src/components/Navbar/Navbar.css

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,25 @@
1111
}
1212

1313
.navbar div {
14-
width: 60%;
14+
width: 70%;
1515
display: flex;
1616
align-items: center;
17-
justify-content: space-around;
17+
justify-content: space-evenly;
18+
}
19+
20+
.navbar div * {
21+
margin: 1%;
1822
}
1923

2024
@media (max-width: 800px) {
2125
.navbar {
2226
display: block;
2327
text-align: left;
28+
padding-top: 2%;
2429
}
2530

2631
.navbar div {
32+
margin: 3% auto;
2733
display: grid;
2834
width: 100%;
2935
grid-template-columns: 1fr 1fr;

src/components/Navbar/Navbar.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const Navbar = (props) => {
1616
value={props.algType}
1717
onChangeHandler={props.algChangeHandler}
1818
values={[0, 1, 2]}
19+
index="0"
1920
options={["Bubble Sort", "Selection Sort", "Insertion Sort"]}
2021
isDisabled={props.isSorting}
2122
/>
@@ -24,14 +25,18 @@ const Navbar = (props) => {
2425
onChangeHandler={props.sizeChangeHandler}
2526
values={[5, 10, 25, 50, 75, 100]}
2627
options={[5, 10, 25, 50, 75, 100]}
28+
index="1"
2729
isDisabled={props.isSorting}
30+
title="Size"
2831
/>
2932
<Select
3033
value={props.speed}
3134
onChangeHandler={props.speedChangeHandler}
32-
values={[0.25, 0.5, 1, 2, 4]}
35+
values={[5, 4, 3, 2, 1]}
36+
index="2"
3337
options={["0.25x", "0.5x", "1x", "2x", "4x"]}
3438
isDisabled={props.isSorting}
39+
title="Speed"
3540
/>
3641
</div>
3742
</div>

src/components/Select/Select.css

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,50 @@
1-
.select {
2-
/* background-color: rgb(218, 218, 218); */
3-
background-color: transparent;
4-
border-radius: 10px;
5-
padding-left: 1%;
1+
.select button:disabled {
2+
cursor: not-allowed;
3+
}
4+
5+
#select {
6+
width: 10rem;
67
height: 3rem;
8+
position: relative;
9+
text-align: center;
10+
}
11+
12+
#select button:disabled {
13+
cursor: not-allowed;
14+
}
15+
16+
#select button {
17+
width: 99%;
18+
height: 100%;
19+
background-color: transparent;
20+
border: none;
721
font-size: 1rem;
8-
width: 10rem;
9-
border: 1px solid rgb(218, 218, 218);
10-
margin: 1%;
11-
outline: none;
1222
cursor: pointer;
23+
padding: 0;
1324
}
1425

15-
.select:disabled {
16-
cursor: not-allowed;
26+
.options {
27+
position: absolute;
28+
background-color: rgb(255, 255, 255);
29+
z-index: 10;
30+
width: 100%;
31+
top: 100%;
32+
left: 10%;
33+
}
34+
35+
.options li {
36+
list-style: none;
37+
width: 100%;
38+
height: 3rem;
39+
cursor: pointer;
40+
border-top: 1px solid black;
41+
text-align: left;
42+
padding: 10%;
43+
}
44+
45+
.options li:hover {
46+
background-color: lightgreen;
47+
}
48+
49+
@media (max-width: 400px) {
1750
}

src/components/Select/Select.js

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,56 @@
11
import "./Select.css";
2+
import { useState, useRef, useEffect } from "react";
3+
import "font-awesome/css/font-awesome.min.css";
24

35
const Select = (props) => {
6+
const [showList, setShowList] = useState(false);
7+
const [index, setIndex] = useState(props.index);
8+
const ref = useRef();
9+
10+
useEffect(() => {
11+
document.addEventListener("click", outsideClickHandler);
12+
return () => {
13+
document.removeEventListener("click", outsideClickHandler);
14+
};
15+
});
16+
17+
const outsideClickHandler = (e) => {
18+
if (ref.current && !ref.current.contains(e.target)) {
19+
setShowList(false);
20+
}
21+
};
22+
23+
const showListHandler = () => {
24+
setShowList((prevState) => !prevState);
25+
};
26+
27+
const listClickHandler = (e, i) => {
28+
setIndex(i);
29+
setShowList(false);
30+
props.onChangeHandler(e);
31+
console.log(e.target.value);
32+
};
33+
434
let options = props.options.map((option, i) => {
535
return (
6-
<option value={props.values[i]} key={option}>
36+
<li
37+
value={props.values[i]}
38+
key={option}
39+
onClick={(e) => listClickHandler(e, i)}
40+
>
741
{option}
8-
</option>
42+
</li>
943
);
1044
});
45+
1146
return (
12-
<select
13-
value={props.value}
14-
onChange={props.onChangeHandler}
15-
disabled={props.isDisabled}
16-
className="select"
17-
>
18-
{options}
19-
</select>
47+
<div id="select" ref={ref}>
48+
<button onClick={showListHandler} disabled={props.isDisabled}>{`${
49+
props.title ? props.title + ":" : ""
50+
} ${props.options[index]}`}</button>
51+
<i className="fa fa-caret-down"></i>
52+
{showList && <ul className="options">{options}</ul>}
53+
</div>
2054
);
2155
};
2256

0 commit comments

Comments
 (0)