mui-org/material-ui

Feature request: ability to override the Input component of a TextField #12472

jedwards1211 posted onGitHub

<!--- Provide a general summary of the feature in the Title above -->

<!-- Thank you very much for contributing to Material-UI by creating an issue! ❤️ To avoid duplicate issues we ask you to check off the following list. -->

<!-- Checked checkbox should look like this: [x] -->

  • This is a v1.x issue. <!-- (v0.x is no longer maintained) -->
  • I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior

<!--- Describe how it should work. -->

redux-form-material-ui v5 has a quick-and-easy TextField adapter component that injects the necessary helperText, value, onChange, etc.

But to be able to use it for an address field, I need to override the Input component with something that does autocomplete (e.g. using react-places-autocomplete)

Current Behavior

<!--- Explain the difference from current behavior. -->

Right now I'd have to basically duplicate the code in TextField, but with my own Input component instead.


Right now I'd have to basically duplicate the code in TextField, but with my own Input component instead.

@jedwards1211 What's wrong with that? The TextField is a simple wrapper component to solve 80% of the use cases. It doesn't aim at being more. Recomposing the text field is definitely a great pattern, it's allowing people to be closer to the actual DOM, something you might need here.

posted by oliviertassinari over 6 years ago

As far as I understand the problem, I'm very tempted to label the issue "won't fix".

posted by oliviertassinari over 6 years ago

Well, what do you see as the downside of allowing people to override the individual components? It would be a small, very simple amount of code to support this so to me it seems like there's virtually no downside to it, and plenty of potential benefit.

posted by jedwards1211 over 6 years ago

I can easily release a fork of TextField as it's own package but I thought it wasn't very justified to create additional fragmentation in the ecosystem for such a small, non-breaking change. Especially since it wouldn't automatically stay consistent with API changes here. Though I hadn't thought that I may be able to just use Select for this, we'll see.

posted by jedwards1211 over 6 years ago

I'm happy to make the PR to do this btw, but I'm certainly not going to waste my time making PRs to this project anymore without any idea if they're likely to get merged

posted by jedwards1211 over 6 years ago

Well, what do you see as the downside of allowing people to override the individual components?

@jedwards1211 It's more about the direction we want to encourage people to go into. What's wrong about using FromControl, InputLabel, Input and FormHelperText over the TextField?

posted by oliviertassinari over 6 years ago

Regarding the integration with redux-form, you might not need a library for it: https://github.com/mui-org/material-ui/issues/8377#issuecomment-331893521

posted by oliviertassinari over 6 years ago

But we migrated to react-final-form:

import React from 'react'
import PropTypes from 'prop-types'
import TextField from 'web/modules/components/TextField'

function RFTextField(props) {
  const {
    autoComplete,
    helperText,
    input: { name, ...input },
    InputProps,
    meta: { dirty, error, submitError, submitFailed },
    ...other
  } = props

  return (
    <TextField
      error={Boolean((dirty || submitFailed) && (error || submitError))}
      {...input}
      {...other}
      id={name}
      name={name}
      InputProps={{
        inputProps: {
          autoComplete,
        },
        ...InputProps,
      }}
      helperText={dirty || submitFailed ? error || submitError : helperText}
    />
  )
}

RFTextField.propTypes = {
  autoComplete: PropTypes.string,
  helperText: PropTypes.node,
  input: PropTypes.shape({
    name: PropTypes.string.isRequired,
  }).isRequired,
  InputProps: PropTypes.object,
  meta: PropTypes.shape({
    dirty: PropTypes.bool.isRequired,
    error: PropTypes.string,
    submitError: PropTypes.string,
    submitFailed: PropTypes.bool.isRequired,
  }).isRequired,
}

export default RFTextField
posted by oliviertassinari over 6 years ago

Alright, encouraging one pattern doesn't mean we should prevent another when the overhead is low. What about adding some InputComponent, SelectComponent, FormHelperTextComponent, FormControlComponent, and InputLabelComponent properties?

posted by oliviertassinari over 6 years ago

The overhead I can think of is about making:

  • the TextField source code harder to read
  • add some more bytes down the wire

This might not be significant, so we should be good.

posted by oliviertassinari over 6 years ago

Yeah it is true that it would add more bytes, that's a concrete downside. Now that I read what TextField is doing, it's not as complicated as I thought so duplicating its behavior with a custom input for redux-form wouldn't be so bad. I was a bit worried at first that I would overlook something and burn time on it. Which is true to some degree, the error and helperText logic are a bit subtle.

I shouldn't have complained about whether a PR would get merged, because I always wind up with code I can use for myself even if it doesn't get merged. (I did release material-ui-popup-state btw)

posted by jedwards1211 over 6 years ago

Now that I read what TextField is doing, it's not as complicated as I thought so duplicating its behavior with a custom input for redux-form wouldn't be so bad.

@jedwards1211 Let me know what direction you want to take, I'm happy with both sides :).

I did release material-ui-popup-state btw

Awesome! I'm sorry I haven't answered yet on this topic yet. I have a backlog of items to handle that is growing. Do you want to link it in the documentation?

posted by oliviertassinari over 6 years ago

Yeah I'll make a PR for that soon!

posted by jedwards1211 over 6 years ago

@0maxxam0 funded this issue with $20. See it on IssueHunt

posted by IssueHuntBot over 6 years ago

Fund this Issue

$20.00
Funded
Only logged in users can fund an issue

Pull requests

Recent activities

0maxxam0 funded 20.00 for mui-org/material-ui# 12472
over 6 years ago