-
Notifications
You must be signed in to change notification settings - Fork 256
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
Fix/remove finddomnode #308
base: master
Are you sure you want to change the base?
Fix/remove finddomnode #308
Conversation
In tests wrap ClassInputComponent with forwardRef Include failing test but skip it
@@ -6,9 +6,9 @@ import InputMask from "../src"; | |||
function Input() { | |||
const [value, setValue] = useState(""); | |||
|
|||
function onChange(event) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix eslint error
} else { | ||
module.exports = require("./lib/react-input-mask.development.js"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix eslint errors
@@ -1088,7 +1093,7 @@ describe("react-input-mask", () => { | |||
}); | |||
|
|||
it("should allow to modify value with beforeMaskedStateChange", async () => { | |||
function beforeMaskedStateChange({ nextState }) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix eslint error
Should fix these issues too: |
@@ -261,33 +265,35 @@ const InputMask = forwardRef(function InputMask(props, forwardedRef) { | |||
setInputState(newInputState); | |||
}); | |||
|
|||
const refCallback = node => { | |||
inputRef.current = node; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
@@ -220,6 +221,9 @@ const InputMask = forwardRef(function InputMask(props, forwardedRef) { | |||
} | |||
|
|||
const input = getInputElement(); | |||
if (!input) { | |||
return; | |||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor fixed that helped with failing tests
@@ -144,6 +142,9 @@ const InputMask = forwardRef(function InputMask(props, forwardedRef) { | |||
// https://github.com/sanniassin/react-input-mask/issues/108 | |||
function onMouseDown(event) { | |||
const input = getInputElement(); | |||
if (!input) { | |||
return; | |||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor fixed that helped with failing tests
} | ||
|
||
return <input {...inputProps} />; | ||
return <input ref={refCallback} {...inputProps} />; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I separate out the ref
from the inputProps
for clarity
return ( | ||
<div> | ||
<input {...this.props} /> | ||
<input ref={innerRef} {...restProps} /> | ||
</div> | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename ref
---> node
to match react docs. It is the node
which is the equivalent to ref.current
.
@@ -146,10 +146,42 @@ function InvalidInput(props) { | |||
} | |||
``` | |||
|
|||
**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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1. a function component that implments `React.forwardRef` | |
1. a function component that implements `React.forwardRef` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
forwardRef
or wrap your class component insideforwardRef
.https://mui.com/material-ui/guides/composition/#caveat-with-strictmode: