How to add a money sign in front of my input field in Material UI

Viewed 5884
4 Answers

Instead of start adornment use the below code in the Input field.

InputProps={{
  startAdornment: <InputAdornment position="start">$</InputAdornment>,
}}

You just imported the wrong Input.

In demo.js on line 5: import Input from "@material-ui/core/TextField";

The correct import would be: import Input from '@material-ui/core/Input';

Please change the 'value' attribute like this to get the symbol before the input.

    <Input
      className={classes.input}
      value={"$" + value}
      onChange={handleInputChange}
      startAdornment={<InputAdornment position="start">A</InputAdornment>}
    />

Wrap them with Grid.

Change your code with this:

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>
  );
}
Related