-
Notifications
You must be signed in to change notification settings - Fork 197
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
Export higher order components that can easily adapt any inputComponent to work with PhoneInput #283
Comments
@TylerRick Hmmm, nice work, actually. The HOC approach you've illustrated is an elegant way of writing a custom input component, though I though: does anyone actually need to supply a custom higher-order input component now that the "lower-order"
Yeah, I also noticed these two.
I've been opposing this whole hooks thing until recently. |
FYI: Released |
Is there an easy way to port this to React-Native? Tried using custom RN components for the numberInputComponent, and countrySelectComponent, but not sure where else to go from here... |
@pjay79 |
@catamphetamine this is what i am doing...
|
@pjay79 Your error is another one: some React component is |
@catamphetamine Don't think it is undefined. See image. Changed the import to:
|
@pjay79 Well, that's what I was talking about: the default |
It seems like this library is too biased towards its built-in input components (
InputBasic
andInputSmart
) and makes it too hard to use other input components like those from Material UI.It's possible to use other input components, using custom adapter components, but it just seems a lot harder than it ought to be. It took me many hours to figure out, and only after completely giving up, and then after discovering clues and progress by others (#214, #226) who have already struggled with attempting to integrate with (in my case) material-ui before me.
Ideally, everything should be "swappable": it should be trivial to swap out the default input component for any other one of your choice.
Ideally, the docs and examples should include at least one example of integration with each popular input library, such as material-ui.
Ideally, inputs from other other libraries (as well as the native
<input />
) should just work "out of the box".But since I don't know exactly how to make that possible (we might want to look at how other libraries make things easily swappable for ideas), I started looking at how feasible it would be to make some general-purpose "adapter" functions that adapt other inputs so they can be used with PhoneInput.
Here's what I have working so far: https://codesandbox.io/s/integrate-react-phone-number-input-with-material-uicore-ohfwp
I've split it into the following concerns/HOCs, which you can compose as needed:
withValueFromTarget
: Adapts theonChange(event: Event)
API of most input components to theonChange(value: string)
typing required by this library [#214]withFocusMethod
: Add a.focus()
method to the component using a ref (#281 would be a better solution though)withFormattedPhoneNumber
: "Consumes" (doesn't pass on) the following props and uses them to format the input value like the defaultInputBasic
does (#226 (comment)):country : string?
— The currently selected country.undefined
means "International" (no country selected).metadata : object
—libphonenumber-js
metadata.So basically I'm splitting up the monolithic
InputBasic
component into smaller functional components, because I think hooks and functional components are the future of React and are simpler, more concise, and easier to reason about.adaptMuiInputForPhoneInput
composes all of the above concerns together and lets you wrap a Material UIInput
orTextField
component to make it compatible.A similar HOC should be easy to write for
input
or any other custom input component.I could probably submit a pull request if there were enough interest.
Not sure where a good place for passing these default props would be:
but for now I've lumped it into
withFormattedPhoneNumber
.Not done yet:
withSmartCaret
HOC that adds "smart" caret positioning usinginput-format
libraryonFocus()
— Is used to toggle the--focus
CSS class.onBlur()
— Is used to toggle the--focus
CSS class.The text was updated successfully, but these errors were encountered: