Skip to content
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

Migrated Textinput/index.js to functional component #24076

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 47 additions & 46 deletions src/components/TextInput/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, {useEffect, useRef} from 'react';
import _ from 'underscore';
import styles from '../../styles/styles';
import * as styleConst from './styleConst';
Expand All @@ -7,65 +7,66 @@ import * as baseTextInputPropTypes from './baseTextInputPropTypes';
import DomUtils from '../../libs/DomUtils';
import Visibility from '../../libs/Visibility';

class TextInput extends React.Component {
componentDidMount() {
if (this.props.disableKeyboard) {
this.textInput.setAttribute('inputmode', 'none');
function TextInput(props) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per the style guide, it is suggested to destructure the props.

Always use destructuring to get prop values. Destructuring is necessary to assign default values to props.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we do pass props as it is to BaseTextInput. So, I guess it is okay to use props as parameter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, thanks

const textInputRef = useRef(null);
const removeVisibilityListenerRef = useRef(null);

useEffect(() => {
if (props.disableKeyboard) {
textInputRef.current.setAttribute('inputmode', 'none');
}

if (this.props.name) {
this.textInput.setAttribute('name', this.props.name);
if (props.name) {
textInputRef.current.setAttribute('name', props.name);
}

// Forcefully activate the soft keyboard when the user switches between tabs while input was focused.
this.removeVisibilityListener = Visibility.onVisibilityChange(() => {
if (!Visibility.isVisible() || !this.textInput || DomUtils.getActiveElement() !== this.textInput) {
removeVisibilityListenerRef.current = Visibility.onVisibilityChange(() => {
if (!Visibility.isVisible() || !textInputRef.current || DomUtils.getActiveElement() !== textInputRef.current) {
return;
}
this.textInput.blur();
this.textInput.focus();
textInputRef.current.blur();
textInputRef.current.focus();
});
}

componentWillUnmount() {
if (!this.removeVisibilityListener) {
return;
}
this.removeVisibilityListener();
}

render() {
const isLabeledMultiline = Boolean(this.props.label.length) && this.props.multiline;
const labelAnimationStyle = {
'--active-label-translate-y': `${styleConst.ACTIVE_LABEL_TRANSLATE_Y}px`,
'--active-label-scale': `${styleConst.ACTIVE_LABEL_SCALE}`,
'--label-transition-duration': `${styleConst.LABEL_ANIMATION_DURATION}ms`,
return () => {
if (!removeVisibilityListenerRef.current) return;
removeVisibilityListenerRef.current();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const isLabeledMultiline = Boolean(props.label.length) && props.multiline;
const labelAnimationStyle = {
'--active-label-translate-y': `${styleConst.ACTIVE_LABEL_TRANSLATE_Y}px`,
'--active-label-scale': `${styleConst.ACTIVE_LABEL_SCALE}`,
'--label-transition-duration': `${styleConst.LABEL_ANIMATION_DURATION}ms`,
};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we move these styles to the styles file?


return (
<BaseTextInput
// eslint-disable-next-line react/jsx-props-no-spreading
{...this.props}
innerRef={(el) => {
this.textInput = el;
if (!this.props.innerRef) {
return;
}
return (
<BaseTextInput
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
innerRef={(el) => {
textInputRef.current = el;
if (!props.innerRef) {
return;
}

if (_.isFunction(this.props.innerRef)) {
this.props.innerRef(el);
return;
}
if (_.isFunction(props.innerRef)) {
props.innerRef(el);
return;
}

this.props.innerRef.current = el;
}}
inputStyle={[styles.baseTextInput, styles.textInputDesktop, isLabeledMultiline ? styles.textInputMultiline : {}, ...this.props.inputStyle]}
textInputContainerStyles={[labelAnimationStyle, ...this.props.textInputContainerStyles]}
/>
);
}
// eslint-disable-next-line no-param-reassign
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this error not happening before, but it is now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a flaky eslint thing. It should have errored before migration since we were disabling this rule in other class components like this.

Maybe the // eslint-disable-next-line react/jsx-props-no-spreading rule above caused some overlap (not sure sure). I hope its not a blocker?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a blocker.

props.innerRef.current = el;
}}
inputStyle={[styles.baseTextInput, styles.textInputDesktop, isLabeledMultiline ? styles.textInputMultiline : {}, ...props.inputStyle]}
textInputContainerStyles={[labelAnimationStyle, ...props.textInputContainerStyles]}
/>
);
}

TextInput.displayName = 'TextInput';
TextInput.propTypes = baseTextInputPropTypes.propTypes;
TextInput.defaultProps = baseTextInputPropTypes.defaultProps;

Expand Down