Material-ui: [TextField] type=number missing min, max, step Typescript types

Created on 27 Nov 2017  ·  8Comments  ·  Source: mui-org/material-ui

  • [x] I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior

<TextField type="number" min="0" max="10" step="1" /> should compile with Typescript

Current Behavior

Receive following Typescript error for min, similar for max and step: [ts] Property 'min' does not exist on type 'IntrinsicAttributes & TextFieldProps & { children?: ReactNode; }'.

Your Environment

| Tech | Version |
|--------------|---------|
| Material-UI | 1.0.0-beta.21 |
| React | 16.0.0 |
| Typescript | 2.6.1 |

bug 🐛 TextField typescript

Most helpful comment

Does this work?

<TextField type="number" inputProps={{ min: "0", max: "10", step: "1" }} />

All 8 comments

Does this work?

<TextField type="number" inputProps={{ min: "0", max: "10", step: "1" }} />

Yes that works. inputProps accepts an object with anything in it, though, so I'm effectively opting out of the type system by doing it this way.

Yes that works. inputProps accepts an object with anything in it, though, so I'm effectively opting out of the type system by doing it this way.

Good point, I've opened #9321 to fix the typing of inputProps.

inputProps is intended to allow for _anything goes_ on an InputComponent. This allows for open integration with external components, for which unfortunately we may not know or have types.

Example:

import * as MaskedInput from 'react-text-mask'

      <TextField
        InputProps={{
          inputComponent: MaskedInput,
        }}
        inputProps={{
          guide: false,
          mask,
          placeholderChar: '\u2000',
        }}
        type="tel"
        value={value}
      />

So the alternative to avoiding strongly typed inputProps in the same scenario (thanks @pelotom) is

import { InputProps as MuiInputProps } from 'material-ui/Input'

      <TextField
        InputProps={{
          inputComponent: (inputProps: MuiInputProps) => (
            <MaskedInput {...inputProps} guide={false} mask={mask} placeholderChar="\u2000" />
          ),
        }}
        type="tel"
        value={value}
      />

@pelotom is filing a new proposal/issue to revisit this API.

@rosskevin
MaskedInput is not defined,
mask is not defined
:(

This seems like an overly complicated workaround

It would be better to define these at the TextField level, so that validation can be performed without a bunch of repeated logic everywhere. I'd hate to have to extend TextField every time I want to use it for a number field with min/max/step requirements.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kybarg picture kybarg  ·  164Comments

chadobado picture chadobado  ·  119Comments

aranw picture aranw  ·  95Comments

tleunen picture tleunen  ·  59Comments

celiao picture celiao  ·  54Comments