Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

customInputProps with Typescript #295

Closed
cescoferraro opened this issue Mar 19, 2019 · 6 comments · Fixed by #330
Closed

customInputProps with Typescript #295

cescoferraro opened this issue Mar 19, 2019 · 6 comments · Fixed by #330

Comments

@cescoferraro
Copy link

              <NumberFormat
                customInput={TextField}
                disableUnderline={true}
                value={batch.price}
                onChange={(ev) => {
                  setState({
                    ...state,
                    batches: state.batches.map((bt, idx) => idx === index ? { ...bt, price: ev.target.value } : bt)
                  })
                }}
                decimalScale={2}
                thousandSeparator="."
                decimalSeparator=","
                prefix="R$"
              />

I am using material-ui TextField input as a customInput. Problem is that I need to pass it some other props like disableUnderline, but this prop does not exist on NumberFormat and it makes the typescript compiles scream, although it works!

TS2322: Type '{ customInput: ComponentType<FilledInputProps>; disableUnderline: boolean; value: any; onChange: (ev: ChangeEvent<HTMLInputElement>) => void; decimalScale: number; thousandSeparator: string; decimalSeparator: string; prefix: string; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<NumberFormat> & Readonly<NumberFormatProps> & Readonly<{ children?: ReactNode; }>'.
  Property 'disableUnderline' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<NumberFormat> & Readonly<NumberFormatProps> & Readonly<{ children?: ReactNode; }>'.
@cescoferraro
Copy link
Author

I forked it and the following works for me
master...cescoferraro:master
but I am afraid there is no typescript way of solving this without creating an extra prop.
Material-ui itself does that by proving an InputProps props so you can pass props to the native element
https://material-ui.com/api/text-field/

@Robfz
Copy link

Robfz commented Mar 25, 2019

We have the same use case as @cescoferraro, a customInputProps prop would be nice.

Our current workaround is:

const CustomInput = ({ customInputProps, ...inputProps }) => { ... }

...

// @ts-ignore
<NumberFormat
  value={value}
  customInput={CustomInput}
  customInputProps={someProps}
/>

@cescoferraro
Copy link
Author

#328 solves this @Robfz
can you take a look at this @s-yadav ?

@Yankovsky
Copy link
Contributor

@s-yadav @cescoferraro @Robfz I've fixed typings in #330

@Yankovsky
Copy link
Contributor

In the meantime you can install yarn add -D @yankovsky/react-number-format, it is just a fork of 4.0.8 with a fix from pr.

@s-yadav
Copy link
Owner

s-yadav commented Aug 5, 2019

I don't think we need customInputProps to solve the typing issue, as we forward all the props to custom input. I liked the @Yankovsky solution to accept any other props. It does allow accepting any random prop, but I think for DX that tradeoff should be ok.
@cescoferraro What do you think?

I have merged and released @Yankovsky PR on v4.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants