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

[material-ui][TextField] InputProps vs inputProps confusion #11377

Closed
1 task done
eyn opened this issue May 14, 2018 · 27 comments · Fixed by #42062
Closed
1 task done

[material-ui][TextField] InputProps vs inputProps confusion #11377

eyn opened this issue May 14, 2018 · 27 comments · Fixed by #42062
Assignees
Labels
breaking change component: text field This is the name of the generic UI component, not the React module! new feature New feature or request package: material-ui Specific to @mui/material v6.x

Comments

@eyn
Copy link
Contributor

eyn commented May 14, 2018

The TextField (nor Input) does not pass the step property through to the input as a step attribute.

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

Expected Behavior

<Textfield type="number" step="0.01" /> renders a <input type="number" step="0.01" />

Current Behavior

<Textfield type="number" step="0.01" /> renders a <input type="number" />

Steps to Reproduce (for bugs)

  1. Go to https://codesandbox.io/s/lyxw254vz9
  2. Click on the up/down arrows on right of textbox and see differences in behaviour

Context

Your Environment

Tech Version
Material-UI 1.0.0.rc0
React 16
browser Safari - Not possible to insert 0.xx number and pass validation. Chrome accepts 0.xx number and validates it but step is still by unit
@eyn eyn changed the title [Textfield] Does not support step property for type=number [TextField] Does not support step property for type=number May 14, 2018
@eyn
Copy link
Contributor Author

eyn commented May 14, 2018

Happy to do a pr for this if you're happy having step added to TextField (and Input)?

@sakulstra
Copy link
Contributor

sakulstra commented May 14, 2018

didn't test, but I guess you should use <TextField inputProps={{step: 0.01}} /> instead? It#s a prop on Input instead of TextField.

update: sry, just checked your sandbox :D you already came up with that. What#s the problem with the inputProps solution? I guess there are more props we don't pass down automagically: https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement so it might be consistent to not do it at all.

@oliviertassinari
Copy link
Member

@eyn We try to find a tradeoff between the popular properties applied on the input and the bundle size of the component. step hasn't made it yet.

@oliviertassinari oliviertassinari added waiting for 👍 Waiting for upvotes component: text field This is the name of the generic UI component, not the React module! labels May 14, 2018
@oliviertassinari
Copy link
Member

I have added the waiting for users upvotes tag. I'm closing the issue as I'm not sure people are looking for such abstraction. So please upvote this issue if you are. We will prioritize our effort based on the number of upvotes.

@eyn
Copy link
Contributor Author

eyn commented May 15, 2018

Agree it's a balance and step probably falls outside that line at the moment. Just wanted at least a discussion. Any thoughts about a helpful message in development mode for properties that are valid on <input /> but not passed on by TextField or Input.

@oliviertassinari
Copy link
Member

Any thoughts about a helpful message in development mode for properties that are valid on but not passed on by TextField or Input.

@eyn I think that it's a good idea 👍

@leMaik
Copy link
Member

leMaik commented Jan 24, 2019

So… Is there actually no way to specify the steps attribute? 😞

@oliviertassinari
Copy link
Member

@leMaik You can with the inputProps property: https://codesandbox.io/s/lyxw254vz9

<TextField inputProps={{ step: "0.01" }} type="number" />

@oliviertassinari
Copy link
Member

oliviertassinari commented Jan 24, 2019

I think that @eyn's idea is brilliant. We could add warnings about this issue at runtime so people don't have to find this issue on GitHub.

@leMaik
Copy link
Member

leMaik commented Jan 26, 2019

@oliviertassinari Oh, with a small i. 😓 A warning would have helped me a lot, indeed. I'll look into adding them later. 👍

@sakulstra
Copy link
Contributor

we've been running into this problem over and over again(with min/max/step) :D and eventually ended up writing a custom NumberField which deflattens these props. Would it make sense to expose such a component within material-ui?

@oliviertassinari
Copy link
Member

@sakulstra Do you have an example of how it works? It could be interesting.

@sakulstra
Copy link
Contributor

Pretty sure it isn't 😅

We're using this within formik so i tried to extract the not super exciting gist of what we do ;)

const NumberField = ({step, min, max, inputProps, ...rest}) => {
  let inputProps = Object.assing({}, inputProps);
  if (step) inputProps.step = step;
  if (min) inputProps.min = min;
  if (max) inputProps.max = max;
  return (
    <TextField inputProps={inputProps} {...rest} />
  )
}

There's probably a more elegant solution to this, bu we basically whitelisted some props which currently aren't forwarded to inputProps automatically and manually forward them.

@oliviertassinari
Copy link
Member

oliviertassinari commented May 3, 2019

@sakulstra Thanks for sharing the implementation. It's a problem we are exploring in #14580.

There is one pragmatic option we have considered: we could deprecate the inputProps. Instead of forwarding the props to the root element, we could forward them to the inner input element. You almost never need to target the wrapping div (we solve the problem with a ContainerComponent and ContainerProps props for the ListItem).

However, there is one downside to it. It breaks the convention almost all the other components are following. What's the purpose of a convention? Create predictability and intuitive APIs. I'm not sure we actually have an intuitive API here. I see a lot of people struggling with the inputProps existence. We would solve:

cc @eps1lon

@sakulstra
Copy link
Contributor

@oliviertassinari thanks for the references, looking trough this, it seems like your approach makes a lot more sense! Looking forward to this change ;)

@jhoffmcd
Copy link

jhoffmcd commented May 8, 2019

As a last resort, if the choice is pragmatic and intuitive, I would choose to break convention I suppose. Form in general have always been a pain point with special complexities so they might unique in a sense. Seems like it solves a whole host of problems that would be nice to eliminate.

@oliviertassinari oliviertassinari removed the waiting for 👍 Waiting for upvotes label Aug 31, 2019
@oliviertassinari oliviertassinari changed the title [TextField] Does not support step property for type=number [TextField] InputProps vs inputProps confusion Aug 31, 2019
@oliviertassinari oliviertassinari added the priority: important This change can make a difference label Aug 31, 2019
@oliviertassinari oliviertassinari removed the priority: important This change can make a difference label Dec 1, 2019
@davext
Copy link

davext commented Apr 1, 2021

I'm still having trouble understanding the difference between inputProps vs InputProps

image TextField.d.ts

  1. I noticed that autoComplete="new-password" mentioned here works when I have "inputProps" but it does not when I have InputProps
  2. endAdornment works when I have InputProps and not when I have inputProps
  3. if I have both (which wouldn't make sense based on the screenshot attached) it slows down the browser significantly.

@oliviertassinari
Copy link
Member

oliviertassinari commented Apr 1, 2021

@davext The component/DOM structure of TextField is:

  • FormControl
    • InputLabel
    • Input
      • input
    • FormHelperText

it should clarify it

@davext

This comment has been minimized.

@mnajdova
Copy link
Member

I'm still having trouble understanding the difference between inputProps vs InputProps

index c33fab4291..7a582b6b84 100644
--- a/packages/material-ui/src/TextField/TextField.js
+++ b/packages/material-ui/src/TextField/TextField.js
@@ -302,7 +302,7 @@ TextField.propTypes /* remove-proptypes */ = {
    */
   inputProps: PropTypes.object,
   /**
-   * Props applied to the Input element.
+   * Props applied to the Input component.
    * It will be a [`FilledInput`](/api/filled-input/),
    * [`OutlinedInput`](/api/outlined-input/) or [`Input`](/api/input/)
    * component depending on the `variant` prop value.

Could this diff help? It would at least add a differentiation between the component vs element.

@oliviertassinari
Copy link
Member

oliviertassinari commented Jun 25, 2021

@mnajdova I'm not sure what the right terminology is. From my perspective, props are first applied to the element, then made available in the component's render method.

At this point, I would either propose:

  1. A better documentation [material-ui][TextField] InputProps vs inputProps confusion #11377 (comment)

Capture d’écran 2021-06-25 à 15 59 59

  1. Renaming inputhtmlInput to match the htmlFor (React), htmlColor (Icon component) props convention.

We can likely do these two changes.

@vicary
Copy link

vicary commented Jul 27, 2021

The current convention triggers the lint rule react/jsx-no-duplicate-props and we have no way around it.

@mahady-manana
Copy link

Prevent duplicate properties in JSX (react/jsx-no-duplicate-props)

   .....
    InputProps={InputProps}
    inputProps={inputProps}
   ......
    />

This problem was signaled here since 2018: ESlint give error duplicate error.

Is there a solution to fix this issue ?
We are using v5.

@DiegoAndai DiegoAndai self-assigned this Feb 12, 2024
@danilo-leal danilo-leal changed the title [TextField] InputProps vs inputProps confusion [material-ui][TextField] InputProps vs inputProps confusion Mar 5, 2024
@danilo-leal danilo-leal added the package: material-ui Specific to @mui/material label Mar 5, 2024
@DiegoAndai
Copy link
Member

Hey everyone, I'm working on this alongside the #41281 work. My current view on how to solve the confusion relying on the slots pattern is:

InputProps

To be deprecated in favor of slotProps.input

inputProps

For this one, I see two options to deprecate in favor of:

  1. slotProps.htmlInput: Differentiates the name from slotProps.input and follows React's htmlFor pattern. Pro: keeps the input element available without accessing nested slots.
  2. slotProps.input.slotProps.input: More confusing than option 1, but might be logical for users familiar with the slots pattern.

I prefer option 1., but wonder about your opinion or if you see other options.

@DiegoAndai
Copy link
Member

DiegoAndai commented Apr 29, 2024

I opened my proposal as a PR ☝🏼

@mnajdova
Copy link
Member

mnajdova commented May 1, 2024

  1. slotProps.htmlInput: Differentiates the name from slotProps.input and follows React's htmlFor pattern. Pro: keeps the input element available without accessing nested slots.

I like this better, hoisting the API that makes sense on top makes sense, however this will fail if the Input slot is changed and it has a different API for the HTML input. I don't think this is very likely to happen, but at least the second option does not suffer from this issue.

@DiegoAndai
Copy link
Member

this will fail if the Input slot is changed and it has a different API for the HTML input.

I think option 2 suffers from this as well. If the custom input does not support slotProps.input, then TextField's slotProps.input.slotProps.input wouldn't work either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change component: text field This is the name of the generic UI component, not the React module! new feature New feature or request package: material-ui Specific to @mui/material v6.x
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.