我創建了這個函式,它是一個帶有復選框的過濾器。我如何添加可以檢查所有/取消選中所有現有代碼的功能,以及向其中添加打字稿?
非常感謝任何幫助!
function FilterCheckbox(props) {
const wells = ["All","Legacy Wells", "Injection Wells", "Monitoring Wells"];
const [checkedWells, setCheckedWells] = useState([]);
const wellsProps = { wells, checkedWells, setCheckedWells };
return (
<div>
<CheckedWells {...wellsProps} />
</div>
);
}
function CheckedWells(props) {
const handleChecked = e => {
const well = props.wells[e.target.dataset.id];
let newCheckedWells = props.checkedWells.filter(item => item !== well);
if (e.target.checked) newCheckedWells.push(well);
props.setCheckedWells(newCheckedWells);
};
return (
<div>
{props.wells.map((well, id) => (
<label key={id}>
<input type="checkbox" data-id={id} onClick={handleChecked} /> {well}
</label>
))}
<p>{props.checkedValues}</p>
</div>
);
}
export default FilterCheckbox
uj5u.com熱心網友回復:
您應該在checked那里添加屬性并處理您的邏輯。例如checked={props.checkedWells.includes(<your logic here>)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/426585.html
標籤:javascript 反应 打字稿
上一篇:這兩個箭頭函式有什么區別?
