I'm trying to do a application which create a component with a 7 segment led display, but the led display should show the inputted number. I've been trying to think in a logic to this, but it's not working.
Logic:
- For each inputted number, create a component.
- Inside this component, it has a
switch, and depending on the inputted number, it'll add theclassNamewhich contains the disabled style to eachdiv.
And when I try to input just one number, it works fine! But when I try it with two numbers, it just bug. As you can see in the images:

I've just two components: <App/> and <Numbers/>.
App:
function App() {
const [input, setInput] = useState('');
const [number, setNumber] = useState([]);
const changeNumbers = () => {
// Splitting inputted numbers to add each number to array.
const inputSplit = input.toString().split('');
const inputArray = inputSplit.map(Number);
setNumber(inputArray);
}
return (
<div>
<input onChange={(e) => {setInput(e.target.value)}}/>
<button onClick={() => {changeNumbers()}}>Confirm</button>
{
number.length ?
number.map((singleNumber, idx) => (
<Numbers singleNumber={singleNumber} key={idx} />
))
:
<Numbers singleNumber={0} />
}
</div>
);
}
Numbers:
const Numbers = ({ singleNumber }) => {
const switchDisabled = (element) => {
const segmentB = document.querySelector('.segment-b');
const segmentC = document.querySelector('.segment-c');
const segmentF = document.querySelector('.segment-f');
switch(element) {
case 2:
segmentC.classList.add('segment-disabled');
segmentF.classList.add('segment-disabled');
break;
case 6:
segmentB.classList.add('segment-disabled');
break;
default:
break;
}
}
useEffect(() => {
switchDisabled(singleNumber);
}, [singleNumber]);
return (
<div className="container">
<div className="segment segment-a"></div>
<div className="segment segment-b"></div>
<div className="segment segment-c"></div>
<div className="segment segment-d"></div>
<div className="segment segment-e"></div>
<div className="segment segment-f"></div>
<div className="segment segment-g"></div>
</div>
)
}
Styles:
.container {
width: 110px;
height: 160px;
position: relative; }
.segment {
position: absolute;
background-color: red; }
.segment-a {
top: 0;
left: 10px;
width: 90px;
height: 10px; }
.segment-b {
top: 13px;
right: 0;
height: 60px;
width: 10px; }
.segment-c {
bottom: 13px;
right: 0;
height: 60px;
width: 10px; }
.segment-d {
bottom: 0;
left: 10px;
width: 90px;
height: 10px; }
.segment-e {
bottom: 13px;
left: 0;
height: 60px;
width: 10px; }
.segment-f {
top: 13px;
left: 0;
height: 60px;
width: 10px; }
.segment-g {
top: 75px;
left: 16px;
width: 80px;
height: 10px; }
.segment-disabled {opacity: 0.1;}
Obs: I didn't posted the imports and exports to save space.