-
Notifications
You must be signed in to change notification settings - Fork 3k
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
focus
and blur
aren't callable when refs are passed into TextInputFocusable
#2786
Comments
focus
and blur
are undefined when refs are passed into TextInputFocusablefocus
and blur
aren't callable when refs are passed into TextInputFocusable
I got it working locally. The problem with the ref is related to the As you can see, it's logging a A similar thing happens with the Example code from the React Docsfunction logProps(Component) {
class LogProps extends React.Component {
componentDidUpdate(prevProps) {
console.log('old props:', prevProps);
console.log('new props:', this.props);
}
render() {
const {forwardedRef, ...rest} = this.props;
// Assign the custom prop "forwardedRef" as a ref
return <Component ref={forwardedRef} {...rest} />;
}
}
// Note the second param "ref" provided by React.forwardRef.
// We can pass it along to LogProps as a regular prop, e.g. "forwardedRef"
// And it can then be attached to the Component.
return React.forwardRef((props, ref) => {
return <LogProps {...props} forwardedRef={ref} />;
});
} And here's our withLocalize with the fix appliedfunction withLocalizeHOC(WrappedComponent) {
const WithLocalize = (props) => {
const translations = {
translate: (phrase, variables) => translate(props.preferredLocale, phrase, variables),
numberFormat: (number, options) => numberFormat(props.preferredLocale, number, options),
timestampToRelative: timestamp => DateUtils.timestampToRelative(props.preferredLocale, timestamp),
timestampToDateTime: (timestamp, includeTimezone) => DateUtils.timestampToDateTime(
props.preferredLocale,
timestamp,
includeTimezone,
),
toLocalPhone: number => toLocalPhone(props.preferredLocale, number),
fromLocalPhone: number => fromLocalPhone(props.preferredLocale, number),
};
return (
<WrappedComponent
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
+ ref={props.forwardedRef}
translate={translations.translate}
numberFormat={translations.numberFormat}
timestampToRelative={translations.timestampToRelative}
timestampToDateTime={translations.timestampToDateTime}
toLocalPhone={translations.toLocalPhone}
fromLocalPhone={translations.fromLocalPhone}
/>
);
};
WithLocalize.displayName = `WithLocalize(${getComponentDisplayName(WrappedComponent)})`;
WithLocalize.propTypes = {
preferredLocale: PropTypes.string,
+ forwardedRef: PropTypes.func,
};
WithLocalize.defaultProps = {
preferredLocale: 'en',
+ forwardedRef: undefined,
};
return React.forwardRef((props, ref) => (
// eslint-disable-next-line react/jsx-props-no-spreading
<WithLocalize {...props} forwardedRef={ref} />
));
} Solutionsa) One of them would be to fix both HOCs to pass the refs properly, but this may lead to unexpected things breaking. b) The other one, which would be faster and safer, is to remove the HOCs from this component and pass it either the translated strings it needs or the translate function, decoupling it from the app's global state and localization system to keeping it as a presentational component. I'm more inclined to apply the solution (b) now and explore (a) in the future to avoid this from happening again. Let me know what you think. |
If you haven’t already, check out our contributing guidelines for onboarding and email [email protected] to request to join our Slack channel!
Expected Result:
When we pass a ref to a
TextInputFocusable
,focus
andblur
should be callable as they are basic functionality of a text input.Actual Result:
When we call
focus
orblur
here and here respectively, they aren't callable, even though we're passing in a ref to theTextInputFocusable
here.Action Performed:
Workaround:
For places where we use programmatically focus or blur
TextInputFocusable
, it's broken.Platform:
Web
iOS
Android
Desktop App
Mobile Web
Version Number:
Logs: https://stackoverflow.com/c/expensify/questions/4856
Notes/Photos/Videos: Any additional supporting documentation
Expensify/Expensify Issue URL:
View all open jobs on Upwork
The text was updated successfully, but these errors were encountered: