我正在使用material ui。我想在输入字段中显示search icon
,如图所示
我正在使用this API
Here是我的代码
我可以显示TextField
,但不能添加搜索图标。
您能否添加添加方式?
<TextField id="standard-bare" defaultValue="Bare" margin="normal" />
答案 0 :(得分:2)
您需要使用Input Adornments。
例如:
// imports
import IconButton from "@material-ui/core/IconButton";
import InputAdornment from "@material-ui/core/InputAdornment";
import SearchIcon from "@material-ui/icons/Search";
// render
<TextField
label="With normal TextField"
InputProps={{
endAdornment: (
<InputAdornment>
<IconButton>
<SearchIcon />
</IconButton>
</InputAdornment>
)
}}
/>
这是一个演示:
const {
TextField,
InputAdornment,
IconButton,
SearchIcon,
Icon
} = window['material-ui'];
class App extends React.Component {
render() {
return (
<TextField
label="With normal TextField"
InputProps={{
endAdornment: (
<InputAdornment>
<IconButton>
<Icon>search</Icon>
</IconButton>
</InputAdornment>
)
}}
/>
);
}
}
ReactDOM.render(<App />, document.getElementById('root'));
<script src="https://unpkg.com/react@latest/umd/react.development.js" crossorigin="anonymous"></script>
<script src="https://unpkg.com/react-dom@latest/umd/react-dom.development.js" crossorigin="anonymous"></script>
<script src="https://unpkg.com/@material-ui/core/umd/material-ui.development.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500" />
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<div id="root"></div>
答案 1 :(得分:2)