Conversation
In tests wrap ClassInputComponent with forwardRef Include failing test but skip it
| function Input() { | ||
| const [value, setValue] = useState(""); | ||
|
|
||
| function onChange(event) { |
| module.exports = require("./lib/react-input-mask.production.min.js"); | ||
| module.exports = require("./lib/react-input-mask.production.min"); | ||
| } else { | ||
| module.exports = require("./lib/react-input-mask.development.js"); |
| }); | ||
|
|
||
| it("should allow to modify value with beforeMaskedStateChange", async () => { | ||
| function beforeMaskedStateChange({ nextState }) { |
|
Should fix these issues too: |
| }); | ||
|
|
||
| const refCallback = node => { | ||
| inputRef.current = node; |
There was a problem hiding this comment.
First main change is here - to remove findDOMNode - however that alone breaks the children usage.
| return <ChildrenWrapper {...inputProps}>{children}</ChildrenWrapper>; | ||
| // {@link https://stackoverflow.com/q/63149840/327074} | ||
| const onlyChild = React.Children.only(children); | ||
| return React.cloneElement(onlyChild, { ...inputProps, ref: refCallback }); |
There was a problem hiding this comment.
These lines were what was required to get the children working. As far as I can tell the main difference is that React.Children.only(children) returns the actual child which is then cloned where as React.cloneElement(children) which is what was being done in <ChildrenWrapper> returns a parent that you can't properly attach a ref to.
| const input = getInputElement(); | ||
| if (!input) { | ||
| return; | ||
| } |
There was a problem hiding this comment.
minor fixed that helped with failing tests
| const input = getInputElement(); | ||
| if (!input) { | ||
| return; | ||
| } |
There was a problem hiding this comment.
minor fixed that helped with failing tests
| } | ||
|
|
||
| return <input {...inputProps} />; | ||
| return <input ref={refCallback} {...inputProps} />; |
There was a problem hiding this comment.
I separate out the ref from the inputProps for clarity
| </div> | ||
| ); | ||
| } | ||
| } |
There was a problem hiding this comment.
Doing a test with the new style of wrapping a class component in a React.forwardRef
| function handleRef(ref) { | ||
| input = ref; | ||
| function handleRef(node) { | ||
| input = node; |
There was a problem hiding this comment.
Rename to match react docs. It is the node which is the equivalent to ref.current.
| ref: ref => { | ||
| input = ref; | ||
| const refCallback = node => { | ||
| input = node; |
There was a problem hiding this comment.
Rename ref ---> node to match react docs. It is the node which is the equivalent to ref.current.
|
|
||
| **Caveat**: To support both class and function component children InputMask used to use `ReactDOM.findDOMNode`, which is now [deprecated](https://reactjs.org/docs/strict-mode.html#warning-about-deprecated-finddomnode-usage). To handle removing this, direct child class components are **no longer supported**. The `children` component is now either: | ||
|
|
||
| 1. a function component that implments `React.forwardRef` |
There was a problem hiding this comment.
| 1. a function component that implments `React.forwardRef` | |
| 1. a function component that implements `React.forwardRef` |
rokija
left a comment
There was a problem hiding this comment.
wondering if this will be merged and published to npm?
I did try the other version from the author of this PR (#239 (comment))
And the issue seems to be fixed there.
|
@rokija As far as I can tell this project is effectively archived. I created this PR so that anyone using the project can use this PR or my fork of it, but I am not expecting that this PR to be merged. |
|
I have put our fork of this as a stable version 3.0.0 now as we've been using it in production for 6 months https://github.com/mona-health/react-input-mask/ |
Thank you! But I can't use maskChar. On the @sanniassin I can. Warning: React does not recognize the |
|
Find out @ianchanning I can use |
|
@johanguse As far as I know this is because So indeed, |
|
Thank you so much for your version of the lib! It is exactly what I need. |
|
plz merge 👍 |
An attempt to fix #239, but tries to go a step further than #255 as that fails with the children tests.
This is still not a perfect fix as I gave up and skipped one of the tests which I think is the least relevant:
However it does handle all other tests.
I've also given up on handling Class Components for children - my main reference for doing this is the Material UI library. They continue to support class components - but warn that you will get the findDOMNode warning and suggest you use
forwardRefor wrap your class component insideforwardRef.https://mui.com/material-ui/guides/composition/#caveat-with-strictmode: