-
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
Migrated Textinput/index.js to functional component #24076
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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'; | ||
|
@@ -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) { | ||
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`, | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why was this error not happening before, but it is now? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
||
|
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.
As per the style guide, it is suggested to destructure the props.
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.
But we do pass props as it is to BaseTextInput. So, I guess it is okay to use props as parameter.
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.
Sure, thanks