I am practising react and I have this task to build an interface where a button can add elements, another button can remove element and two oter butttons can move each element up or down stepwise like this:
I have been able to make buttons add and remove element but changing positions of elements has been giving me some headache. Below is my code:
const [inputList, setInputList] = useState([{ inputBox: '' }]);
const handleInputChange = (e, index) => {
const { name, value } = e.target;
const list = [...inputList];
list[index][name] = value;
setInputList(list);
};
const handleRemoveClick = (index) => {
const list = [...inputList];
list.splice(index, 1);
setInputList(list);
};
const handleAddClick = () => {
setInputList([...inputList, { inputBox: '' }]);
};
const upPosition = () => {
console.log('up');
};
const downPosition = () => {
console.log('down');
};
return (
<div>
<h3>Add Elements</h3>
<button onClick={handleAddClick}>+</button>
{inputList.map((x, i) => {
return (
<div className='box inputBox' key={i}>
<input
name='inputBox'
value={x.inputBox}
onChange={(e) => handleInputChange(e, i)}
/>
<button onClick={() => upPosition()}>
<i className='fas fa-long-arrow-alt-up'></i>
</button>
<button onClick={() => downPosition()}>
<i className='fas fa-long-arrow-alt-down'></i>
</button>
<button className='mr10' onClick={() => handleRemoveClick(i)}>
<i className='fas fa-times'></i>
</button>
</div>
);
})}
</div>
);
You can make use of splice here
Beside the shifting, you have to handle the 2 cases also, where you can't go up if the index is 0
and you can't go down if the index is inputList.length - 1
NOTE: To handle the both cases, I've disabled the buttons because that would be more meaningful to let the end user know that this button is disable so you can't go up or down.
Live Demo
const upPosition = ( index ) => {
const copy = { ...inputList[index] };
setInputList( ( oldState ) => {
const newState = oldState.filter( ( o, i ) => i !== index );
newState.splice( index - 1, 0, copy );
return newState;
} )
};
const downPosition = (index) => {
const copy = { ...inputList[index] };
setInputList( ( oldState ) => {
const newState = oldState.filter( ( o, i ) => i !== index );
newState.splice( index + 1, 0, copy );
return newState;
} )
};