I am building a table. when a user clicks and drags through the cells, the cells between is highlight. But I don't know why when I onClick on a single cell it also highlights between the last cell I clicked and the new cell. I want to when I onClick it only highlight on one cell
Here is my code:
import React, {useState} from "react";
import './Style.css';
export default function Table() {
let div = [];
const [start, setStart] = useState(null);
const [end, setEnd] = useState(null);
const [selecting, setSelecting] = useState(false);
let beginSelection = i => {
setSelecting(true);
setStart(i);
updateSelection(i);
};
let endSelection = (i = end) => {
setSelecting(false);
updateSelection(i);
};
let updateSelection = i => {
if(selecting) {
setEnd(i);
}
};
for(let i = 0; i <= 31; i++ ) {
let a = (
<span
key={i}
className={
((end != null && start != null) && (end <= i && i<= start || (start <= i && i <= end)) ? "selected": "")
}
onMouseDown={()=>beginSelection(i)}
onMouseUp={()=>endSelection(i)}
onMouseMove={()=>updateSelection(i)}
>
{i}
</span>
);
div.push(a);
}
return div;
}
Here are my full code and demo: https://codesandbox.io/s/github/Kalipts/table_grid Please help me. Thanks