【发布时间】:2019-12-05 15:25:23
【问题描述】:
当用户将 excel 或 csv 文件拖放到我身上时,我会从该文件的列中动态创建 material-ui 芯片。我向他们展示了列并允许用户单击他们想要的列,然后我根据与该列关联的数据为他们构建列表。一切正常,除了当用户单击芯片时,我希望芯片变为绿色并添加一个复选标记以表明该芯片已被选中。他们可以选择许多筹码,因此如果他们选择另一个筹码,我希望该筹码变为绿色。他们还可以点击 x 并取消选择芯片,此时我希望芯片返回原始颜色并检查以消失。
return (
<div>
{
props.dropdownkeys.map((value, index) => (
<Chip label={value}
icon={<CheckIcon key={index} id={`check_${value}`} color='primary' visibility={clickedChip ? 'visible' : 'hidden'}/>}
key={index}
onClick={() => addChip(value, index)}
onDelete={() => deselectChip(value, index)}
size='small'
id={`chip_${value}`}
ref={setChipRef}
className={clickedChip ? classes.chipSelected : classes.chip }
/>
))
}
</div>
);
这是我动态创建的芯片,我试图基于 useState 挂钩来触发以更改 clickedchip
const [clickedChip, setClickedChip] = useState(false);
这是我的 addchip 功能
function addChip(value: number | string, index: number) {
debugger;
const chipClicked = getClickedChip(index);
if (chipClicked !== null) {
// chipClicked[0].style.backgroundColor = 'green';
// (chipClicked[0].firstChild as HTMLElement).style.color = 'red';
// (chipClicked[0].firstChild as HTMLElement).attributes[6].value = 'visible';
}
const getCols = props.excelDocumentObj.filter((column) => {
if (column[value] !== undefined) {return column[value];} else { return null;}
});
if (props.storeString !== '') {
props.setStoreString(',');
}
const rowLen = getCols.length;
for (const [i, storeNum] of getCols.entries()) {
if(typeof storeNum[value] === 'number') {
if(rowLen !== i + 1) {
props.setStoreString(`${storeNum[value] },`);
}
else {
props.setStoreString(`${storeNum[value]}`);
}
}
}
creatorStore.handleStoreListFromExcel(props.storeString);
}
我遇到的问题是我正在更改渲染器中的所有芯片,而不是我想要的选定芯片。我不想通过 dom 访问芯片,因为基本上我被告知不要在我的拉取请求中这样做。我是新手,所以我在执行这个简单的任务时遇到了问题,我们将不胜感激。
【问题讨论】:
标签: reactjs material-ui