MUI/React新手技术求助:如何让InputBase占满搜索容器空间而非Icon Button容器
Hey there! I see you're just getting started with MUI/React—let's fix that layout issue where your search icon is hogging all the space instead of the InputBase.
问题根源
The main issue lies in how you've set up the flex layout and width properties:
- Your
Searchcomponent usesdisplay: flex, but you didn't define proper flex behavior for its children (formandSearchIconWrapper). - The
SearchIconWrapperhaswidth: "100%", which makes it take up all available width and squeeze the InputBase into a tiny space. - The
formelement doesn't have any flex properties to tell it to fill the remaining space.
修改方案
Here's how to adjust your code to get the desired layout:
Tweak the
Searchstyled component
Keep it as a flex container, and add vertical alignment to ensure the input and icon are properly aligned.Fix the
SearchIconWrapper
Removewidth: "100%"and setflex: 0 0 autoinstead—this tells the wrapper to only take the space it needs, no more, no less.Make the
formfill remaining space
Addflex: 1to the form so it takes up all the space not used by the icon wrapper, and setdisplay: flexto ensure the InputBase inside fills the form's width.
修改后的完整代码
import * as React from "react"; import { styled } from "@mui/material/styles"; import Box from "@mui/material/Box"; import InputBase from "@mui/material/InputBase"; import SearchIcon from "@mui/icons-material/Search"; import IconButton from "@mui/material/IconButton"; const Search = styled("div")(({ theme }) => ({ display: "flex", position: "relative", borderRadius: 30, backgroundColor: "#ffffff", border: "1px", borderStyle: "solid", borderColor: "#55597d", width: "auto", // 让子元素垂直居中对齐 alignItems: "center", })); const SearchIconWrapper = styled("div")(({ theme }) => ({ padding: theme.spacing(0, 2), height: "100%", display: "flex", alignItems: "center", justifyContent: "flex-end", // 仅占用自身需要的空间,不拉伸不收缩 flex: "0 0 auto", })); const StyledInputBase = styled(InputBase)(({ theme }) => ({ color: "inherit", width: "100%", "& .MuiInputBase-input": { padding: theme.spacing(1, 1, 1, 0), // 给输入框左侧增加留白,提升视觉体验 paddingLeft: `calc(1em + ${theme.spacing(2)})`, paddingRight: `calc(1em + ${theme.spacing(1)})`, transition: theme.transitions.create("width"), }, })); export default function SearchAppBar({ searchQuery, setSearchQuery, clearGenre, onDropDownChange, }) { return ( <Box sx={{ flexGrow: 1 }}> <Search sx={{ width: { xs: "90vw", md: "50vw", lg: "30vw" }, margin: "auto", marginBottom: "20px", }} > <form action="/" method="get" style={{ flex: 1, display: "flex" }}> <StyledInputBase defaultValue={searchQuery} placeholder="Search All Games…" inputProps={{ "aria-label": "search" }} type="search" name="s" id="site-search" /> </form> <SearchIconWrapper> <IconButton> <SearchIcon style={{ color: "#55597d" }} /> </IconButton> </SearchIconWrapper> </Search> </Box> ); }
额外说明
- Added
alignItems: "center"to theSearchcomponent to ensure vertical alignment between the input and icon. - Adjusted padding on
StyledInputBaseto give the input text better breathing room. - Added
display: flexto the form to make sure the InputBase inside fills the entire form width.
Now your InputBase will take up all available space in the search container, while the search icon only occupies the space it needs!
内容的提问来源于stack exchange,提问作者Forged

