Passing props directly to the TextField
component using the sx
prop is a simple and effective method:
<TextField
type="number"
sx={{
"input[type=number]::-webkit-outer-spin-button": {
...spinnerStyles
},
"input[type=number]::-webkit-inner-spin-button": {
...spinnerStyles
}
}}
/>
If you prefer, you can also pass the props through the InputProps
prop of the TextField
:
<TextField
type="number"
InputProps={{
type: "number",
sx:{
"input[type=number]::-webkit-outer-spin-button": {
...spinnerStyles
},
"input[type=number]::-webkit-inner-spin-button": {
...spinnerStyles
}
}
}}
/>
Edit for requested styling
To customize the spinner buttons and their background, consider encoding your PNG button image in base64 format and use it as the background for the spinner:
const spinnerStyles = {
"-webkit-appearance": "none",
background:
"#000 url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACMAAAAcCAYAAADr9QYhAAAAAXNSR0IArs4c6QAAAERlWElmTU0AKgAAAAgAAYdpAAQAAAABAAAAGgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAAI6ADAAQAAAABAAAAHAAAAACbzWzwAAABB0lEQVRIDe2XMQ6DMAxFf6suwAjszLDCTeASHALEyFlg5hLsXIJDtPIQVFkkgrhDVCWLcQzJ84/liEeSJG84MIqiwMMVmCAI8HRAlAPBwxxSsIf/VKZpGozjiCiKWL7X3Z8oQyB1XSPLMnRdZw0khlEgKn8JkAiGg0iBrJse1UZZlmr/U7vvO7ZtO43xSWp61jB8ManvO7BJQVEBmxa2iXkYnWpOKfPSUV6Zb9sWaZpqX12WBeu6auM8IOozBNL3/SnQNE2Y55nvp/XFfYY67DAMIPs97oKob8U1w4FsQQhIdEwqI7J0ZFVVgerEZvi7yaSauGZMi9+NOQMThqEbP3FxHCPPc3wAmdpEetL9b2QAAAAASUVORK5CYII=) no-repeat center center",
width: "2em",
opacity: 1,
position: "absolute",
top: 0,
right: 0,
bottom: 0,
overflow: "hidden",
borderTopRightRadius: "0.25rem",
borderBottomRightRadius: "0.25rem"
};
You have the flexibility to style the remaining parts of the TextField as desired based on your existing styles or theme.
Output:
https://i.sstatic.net/8LIOl.png
View the working [Updated] CodeSandbox here: https://codesandbox.io/s/romantic-knuth-j0n5lw?file=/demo.tsx:961-1290