【发布时间】:2019-10-23 14:57:38
【问题描述】:
我有以下情况,我制作了一个列表,添加(按下按钮后)一个 React 节点。模型最终布局是这样的
当前代码如下:
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;
我遇到的两个问题是,当我从列表中删除一个项目时,它总是会删除最后一个。并且移动箭头不起作用。 我尝试了多项更改,但都没有成功。
【问题讨论】:
标签: reactjs typescript react-hooks