gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
<Select multiple displayEmpty sx={{fontSize: ofScreener ? 11 : 13}} value={personName}…
- User
<Select multiple displayEmpty sx={{fontSize: ofScreener ? 11 : 13}} value={personName} onChange={handleChange} renderValue={(selected) => { // console.log(selected);
codeif (selected.length === 0) { return <Typography fontSize={ofScreener ? 11 : 13} color="#a2a2a2">пробой уровня</Typography>; } return <Grid display="flex" > {selected.map((value, index) => ( <span key={index}> <Typography sx={{ display: "flex", alignItems: "center", mr: 1, fontSize: ofScreener ? "11px" : "13px", color: "#000", backgroundColor: randomColor(), border: "none", borderRradius: "2px", boxSizing: "content-box", padding: ofScreener ? "0 7px 0 7px" : "0 10px 0 10px", "& svg": { display: "none", }, // "&:hover": { // padding: ofScreener ? "0 2px 0 7px" : "0 2px 0 10px", // "& svg": { // display: "block", // fontSize: ofScreener ? "15px" : "18px", // cursor: "pointer", // }, // }, }} > {truncate(value, 15)} <CloseIcon onClick={() => handleTagDelete(index)} /> </Typography> </span> ))} </Grid>; }} > - Assistant
Для того чтобы узнать ширину селекта в данном коде, можно использовать следующий подход:
- Добавьте реф для компонента
<Select>:
jsxconst selectRef = useRef();- Присвойте реф компоненту
<Select>:
jsx<Select ref={selectRef} // остальные пропсы > {/* содержимое селекта */} </Select>- После рендера компонента, можно получить ширину селекта с помощью
clientWidth:
jsxuseEffect(() => { if (selectRef.current) { const selectWidth = selectRef.current.clientWidth; console.log(selectWidth); } }, []);Теперь в консоли будет выведена ширина селекта в пикселях.
- Добавьте реф для компонента
- User
добавь тайпскрипт
- Assistant
Для добавления TypeScript в код, необходимо внести следующие изменения:
- Добавьте типы пропов для компонента
<Select>:
tsximport { SelectChangeEvent } from "@mui/material/Select"; // ... const handleChange = (event: SelectChangeEvent<typeof personName>) => { setPersonName(event.target.value); }; const handleTagDelete = (index: number) => { // ... }; // ... <Select multiple displayEmpty sx={{ fontSize: ofScreener ? 11 : 13 }} value={personName} onChange={handleChange} renderValue={(selected) => { // ... return ( <Grid display="flex"> {selected.map((value, index) => ( <span key={index}> {/* ... */} </span> ))} </Grid> ); }} > {/* ... */} </Select> - Добавьте типы пропов для компонента