question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add inputProps prop that lets you pass props down to the inputComponent

See original GitHub issue

This would be very useful when you have props that you need to pass all the way down to the inputComponent.

Otherwise, you have to create a new hard-coded component for literally every unique combination of props that you may want to pass down to the input component. For example, if you wanted to use PhoneInput several places in your app but wanted to use a different label prop (or helperText or …) in each place, you would have to do something horrific like this:

const TextFieldWithLabel1 = ({ ...props }) => {
  return (
    <TextField {...props}
      label="Phone number"
    />
  )
}

const TextFieldWithLabel2 = ({ ...props }) => {
  return (
    <TextField {...props}
      label="Optional phone number"
    />
  )
}

        <PhoneInput inputComponent={TextFieldWithLabel1} />
        <PhoneInput inputComponent={TextFieldWithLabel2} />

What I’d like is a way to simply pass the props down like this:

        <PhoneInput
          inputComponent={TextField}
          inputProps={{
            label: "Optional phone number"
          }}
        />

This is how https://material-ui.com/api/text-field/#props lets you pass props to the <input> component that it wraps, for example.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
catamphetaminecommented, Oct 16, 2019

An example of how clashing (colliding) props like error could be passed:

src/PhoneInput.js

import PhoneInput from 'react-phone-number-input'
import MUIInput from 'material-ui'

function CustomInput({ errorProperty, ...rest }, ref) {
  return (
    <MUIInput 
      ref={ref} 
      error={errorProperty} 
      {...rest}/>
  )
}

CustomInput = React.forwardRef(CustomInput)

function CustomPhoneInput({ error, ...rest }, ref) {
  return (
    <PhoneInput
      ref={ref}
      {...rest}
      numberInputComponent={CustomInput}
      errorProperty={error}/>
  )
}

CustomPhoneInput = React.forwardRef(CustomPhoneInput)

export default CustomPhoneInput
0reactions
catamphetaminecommented, Oct 17, 2019

Ok, added numberInputProps and countrySelectProps today. Still the existing “passthrough” approach is preferred I guess (for the <input/> props).

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to pass props to the inputComponent material-ui?
Custom props can be passed to the TextMaskCustom using inputProps <FormControl> <InputLabel>react-text-mask</InputLabel> <Input ...
Read more >
How passing props to component works in React
Master how to pass props (properties) to components in React with this useful beginner's guide, including demo code.
Read more >
Input Components - React-admin - Marmelab
Additional props are passed down to the underlying component (usually a material-ui component). For instance, when setting the className prop on a TextInput ......
Read more >
How to create reusable form components with React Hook ...
Next, let's take a look at how we can pass the register property to our input. import React from 'react'; import ...
Read more >
<Field /> | Formik
component can either be a React component or the name of an HTML element to render. All additional props will be passed through....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found