3

I am building a webpage with React and Material-UI. I have a TextField element and I want to remove the arrow buttons - if I were using CSS I would use the following code:

input[type=number]::-webkit-outer-spin-button,
input[type=number]::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

But I want to style the page using CSS in JS where I use export default withStyles(styles)(FormPersonalDetails). How do I do this? `

const styles = theme => ({
    number: {
        // styling code goes here//
    }
});

render() function:

const { classes } = this.props;
return (
    <TextField className={classes.number} />
)
Ryan Cogswell
  • 75,046
  • 9
  • 218
  • 198
user2260199
  • 877
  • 4
  • 13
  • 20

1 Answers1

8

There are a few syntax options. I have included two options below. The first one uses className on TextField and then targets the descendant input element. The second one applies the className directly to the input element via the TextField inputProps property.

The & is used in both to refer to the parent selector (i.e. the element that the class is being applied to).

import React from "react";
import ReactDOM from "react-dom";

import TextField from "@material-ui/core/TextField";
import { withStyles } from "@material-ui/core/styles";

const styles = theme => ({
  number: {
    "& input::-webkit-outer-spin-button, & input::-webkit-inner-spin-button": {
      "-webkit-appearance": "none",
      margin: 0
    }
  },
  input: {
    "&::-webkit-outer-spin-button, &::-webkit-inner-spin-button": {
      "-webkit-appearance": "none",
      margin: 0
    }
  }
});

function App({ classes }) {
  return (
    <div className="App">
      <TextField type="number" className={classes.number} />
      <br />
      <TextField type="number" inputProps={{ className: classes.input }} />
    </div>
  );
}
const StyledApp = withStyles(styles)(App);
const rootElement = document.getElementById("root");
ReactDOM.render(<StyledApp />, rootElement);

Edit TextField remove spin buttons

Ryan Cogswell
  • 75,046
  • 9
  • 218
  • 198