I have the following situation, I've made a list that adds (after pressing a button) a React Node. The model final layout is something like this
The current code is the following:
import * as React from 'react';
import { Icon } from '../icon/icon';
import { Button } from '../..';
export interface PriorizationListProps {
children: React.ReactNode;
limit?: number;
}
export const PriorizationList: React.FunctionComponent<PriorizationListProps> = ({ limit, children }) => {
const [items, setItems] = React.useState<React.ReactNode[]>([]);
const move = (currentIndex: number, futureIndex: number) => {
if (futureIndex !== -1 && futureIndex < items.length) {
const item = items[currentIndex];
const movingItem = items[futureIndex];
items[currentIndex] = movingItem;
items[futureIndex] = item;
setItems(items);
}
};
const deleteItem = (index: number) => {
setItems(prevItems => prevItems.filter((_,i) => i !== index));
};
const addItem = () => {
const newItems = items.concat([children]);
setItems(newItems);
};
const disabledByLimit = () => limit === items.length;
return (
<>
{items.map((item, index) => {
return (
<div className="PriorizationList">
<span className="PriorizationList-order">{`${index + 1}.`}</span>
<span className="PriorizationList-content">{item}</span>
<span className="PriorizationList-icon">
<Icon type="pushUp" onClick={() => move(index, index - 1)} />
<Icon type="pushDown" onClick={() => move(index, index + 1)} />
<Icon type="delete" onClick={() => deleteItem(index)} />
</span>
</div>
);
})}
<Button type="text" className="PriorizationList-button" onClick={addItem} disabled={disabledByLimit()}>
<span>Add</span>
<Icon type="add" />
</Button>
</>
);
};
PriorizationList.defaultProps = {
limit: 10
};
export default PriorizationList;
The two issues I'm having are, when I delete an item from the list, it always deletes the last one. And the moving arrows are not working. I tried multiple changes but none of them worked.
