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

Added string[] and readonly string[] for NumberFormatProps for defaultValue #464

Closed
wants to merge 2 commits into from
Closed

Added string[] and readonly string[] for NumberFormatProps for defaultValue #464

wants to merge 2 commits into from

Conversation

Magofoco
Copy link

This PR try to fix a type error of NumberFormatProps when using the MaterialUi Textfield inputComponent in InputProps

Failing example:
https://codesandbox.io/s/wonderful-chaplygin-f3otf
If you go to the component MyInput you can see that the component NumberFormatCustom takes NumberFormatProps as props.
The Material UI Textfied, in its InputProps, can take a inputComponent which can be the NumberFormatCustom component.
This leads to a type error since the props Textfied's defaultValue types are: any (https://material-ui.com/api/text-field/)
while the NumberFormatProps's defaultValue is defined as: string | number | undefined.

I tried to extend the types of defaultValue by including string[] and readonly string[] which seem to solve the above mentioned error

@changelogg
Copy link

changelogg bot commented Nov 23, 2020

Hey! Changelogs info seems to be missing or might be in incorrect format.
Please use the below template in PR description to ensure Changelogg can detect your changes:
- (tag) changelog_text
or
- tag: changelog_text
OR
You can add tag in PR header or while doing a commit too
(tag) PR header
or
tag: PR header
Valid tags: added / feat, changed, deprecated, fixed / fix, removed, security, build, ci, chore, docs, perf, refactor, revert, style, test
Thanks!
For more info, check out changelogg docs

@Magofoco Magofoco changed the title Added readonly string[] for NumberFormatProps Added string[] and readonly string[] for NumberFormatProps for defaultValue Nov 23, 2020
@@ -39,7 +39,7 @@ declare module "react-number-format" {
removeFormatting?: (formattedValue: string) => string;
mask?: string | string[];
value?: number | string;
defaultValue?: number | string;
defaultValue?: number | string | string[] | readonly string[];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But isn't this wrong.? defaultValue will never be array of string.

Copy link
Author

@Magofoco Magofoco Nov 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@s-yadav
I was thinking the same, but apparently it can literally take any.
See several examples here: https://codesandbox.io/s/awesome-benz-7vj06 with different defaultValue types

For example, an array of string such as:
const myArray = ["First Element", "Second Element", "Third"]
if assigned to defaultValue, will produce:
First Element, Second Element, Third Element

Screenshot 2020-11-27 at 22 06 15

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can because of typecasting by JS. But that will still be wrong. Also, the type of value and default value should match.
Wrong typing can lead to confusion on dev end.

Is there any other way to force type in material ui.?

I guess the Material-UI has mentioned it as any because the Material UI supports the text field to be rendered as select.

@Magofoco Magofoco requested a review from s-yadav December 3, 2020 13:23
@s-yadav
Copy link
Owner

s-yadav commented Dec 6, 2020

Related issue.
#446

From the issue looks like @type/react allows the ReadonlyArray;

@Magofoco Can you also check the suggested solution in that issue.

@Magofoco Magofoco closed this by deleting the head repository Oct 21, 2022
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 this pull request may close these issues.

2 participants