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

Migrate Composer index.android.js to function component #22542

Merged
merged 5 commits into from
Jul 13, 2023
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
96 changes: 53 additions & 43 deletions src/components/Composer/index.android.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, {useEffect, useCallback, useRef, useMemo} from 'react';
import {StyleSheet} from 'react-native';
import PropTypes from 'prop-types';
import _ from 'underscore';
Expand Down Expand Up @@ -63,57 +63,67 @@ const defaultProps = {
style: null,
};

class Composer extends React.Component {
constructor(props) {
super(props);
function Composer({shouldClear, onClear, isDisabled, maxLines, forwardedRef, isComposerFullSize, setIsFullComposerAvailable, ...props}) {
const textInput = useRef(null);

this.state = {
propStyles: StyleSheet.flatten(this.props.style),
};
}
/**
* Set the TextInput Ref
* @param {Element} el
*/
const setTextInputRef = useCallback((el) => {
textInput.current = el;
if (!_.isFunction(forwardedRef) || textInput.current === null) {
return;
}

componentDidMount() {
// This callback prop is used by the parent component using the constructor to
// get a ref to the inner textInput element e.g. if we do
// <constructor ref={el => this.textInput = el} /> this will not
// return a ref to the component, but rather the HTML element by default
if (!this.props.forwardedRef || !_.isFunction(this.props.forwardedRef)) {
return;
}

this.props.forwardedRef(this.textInput);
}
forwardedRef(textInput.current);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

componentDidUpdate(prevProps) {
if (prevProps.shouldClear || !this.props.shouldClear) {
useEffect(() => {
if (!shouldClear) {
return;
}

this.textInput.clear();
this.props.onClear();
}

render() {
return (
<RNTextInput
autoComplete="off"
placeholderTextColor={themeColors.placeholderText}
ref={(el) => (this.textInput = el)}
onContentSizeChange={(e) => ComposerUtils.updateNumberOfLines(this.props, e)}
rejectResponderTermination={false}
textAlignVertical="center"
// Setting a really high number here fixes an issue with the `maxNumberOfLines` prop on TextInput, where on Android the text input would collapse to only one line,
// when it should actually expand to the container (https://github.com/Expensify/App/issues/11694#issuecomment-1560520670)
// @Szymon20000 is working on fixing this (android-only) issue in the in the upstream PR (https://github.com/facebook/react-native/pulls?q=is%3Apr+is%3Aopen+maxNumberOfLines)
// TODO: remove this commend once upstream PR is merged
maximumNumberOfLines={this.props.isComposerFullSize ? 1000000 : this.props.maxLines}
style={this.state.propStyles}
/* eslint-disable-next-line react/jsx-props-no-spreading */
{...this.props}
editable={!this.props.isDisabled}
/>
);
}
textInput.current.clear();
onClear();
}, [shouldClear, onClear]);

/**
* Set maximum number of lines
* @return {Number}
*/
const maximumNumberOfLines = useMemo(() => {
if (isComposerFullSize) return 1000000;
return maxLines;
Comment on lines +99 to +101
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hard coding is off, lets leave it as undefined

Copy link
Collaborator

Choose a reason for hiding this comment

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

NVM

}, [isComposerFullSize, maxLines]);

const styles = useMemo(() => {
StyleSheet.flatten(props.style);
}, [props.style]);

return (
<RNTextInput
autoComplete="off"
placeholderTextColor={themeColors.placeholderText}
ref={setTextInputRef}
onContentSizeChange={(e) => ComposerUtils.updateNumberOfLines({maxLines, isComposerFullSize, isDisabled, setIsFullComposerAvailable}, e)}
rejectResponderTermination={false}
textAlignVertical="center"
// Setting a really high number here fixes an issue with the `maxNumberOfLines` prop on TextInput, where on Android the text input would collapse to only one line,
// when it should actually expand to the container (https://github.com/Expensify/App/issues/11694#issuecomment-1560520670)
// @Szymon20000 is working on fixing this (android-only) issue in the in the upstream PR (https://github.com/facebook/react-native/pulls?q=is%3Apr+is%3Aopen+maxNumberOfLines)
// TODO: remove this commend once upstream PR is merged
maximumNumberOfLines={maximumNumberOfLines}
style={styles}
/* eslint-disable-next-line react/jsx-props-no-spreading */
{...props}
editable={!isDisabled}
/>
);
}

Composer.propTypes = propTypes;
Expand Down