[英]How to add a money sign in front of my input field in Material UI
我有一个输入字段,我想在左边有一个美元符号。 我检查了文档并尝试使用InputAdornment
进行操作,但每次我这样做时,美元符号都不会出现。 这是我的代码https://codesandbox.io/s/material-demo-wnei9?file=/demo.js
在Input
字段中使用下面的代码,而不是开始装饰。
InputProps={{
startAdornment: <InputAdornment position="start">$</InputAdornment>,
}}
请像这样更改“值”属性以在输入之前获取符号。
<Input
className={classes.input}
value={"$" + value}
onChange={handleInputChange}
startAdornment={<InputAdornment position="start">A</InputAdornment>}
/>
您刚刚导入了错误的Input
。
在第 5 行的demo.js
中: import Input from "@material-ui/core/TextField";
正确的导入是: import Input from '@material-ui/core/Input';
用网格包裹它们。
用这个改变你的代码:
import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import Typography from "@material-ui/core/Typography";
import Slider from "@material-ui/core/Slider";
import Input from "@material-ui/core/TextField";
import InputAdornment from "@material-ui/core/InputAdornment";
import Grid from "@material-ui/core/Grid";
import FormControl from "@material-ui/core/FormControl";
const useStyles = makeStyles({
root: {
width: 250
},
input: {
width: 100
}
});
export default function InputSlider() {
const classes = useStyles();
const [value, setValue] = React.useState(500);
const handleSliderChange = (event, newValue) => {
setValue(newValue);
};
const handleInputChange = event => {
setValue(event.target.value === "" ? "" : Number(event.target.value));
};
return (
<div className={classes.root}>
<Slider
min={500}
max={10000}
value={typeof value === "number" ? value : 0}
onChange={handleSliderChange}
aria-labelledby="input-slider"
/>
<FormControl fullWidth className={classes.margin}>
<Grid container spacing={2} alignItems="center">
<Grid item>$</Grid>
<Grid item>
<Input
className={classes.input}
value={value}
onChange={handleInputChange}
startAdornment={
<InputAdornment position="start">A</InputAdornment>
}
style={{ display: "inline-block" }}
/>
</Grid>
</Grid>
</FormControl>
</div>
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.