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

composer set correct numberOfLines on intitial render #15296

Merged
1 change: 1 addition & 0 deletions src/ONYXKEYS.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export default {
REPORT: 'report_',
REPORT_ACTIONS: 'reportActions_',
REPORT_DRAFT_COMMENT: 'reportDraftComment_',
REPORT_DRAFT_COMMENT_NUMBER_OF_LINES: 'reportDraftCommentNumberOfLines_',
REPORT_ACTIONS_DRAFTS: 'reportActionsDrafts_',
REPORT_USER_IS_TYPING: 'reportUserIsTyping_',
POLICY: 'policy_',
Expand Down
19 changes: 15 additions & 4 deletions src/components/Composer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ const propTypes = {
/** The value of the comment box */
value: PropTypes.string,

/** Number of lines for the comment */
numberOfLines: PropTypes.number,

/** Callback method to update number of lines for the comment */
onNumberOfLinesChange: PropTypes.func,

/** Callback method to handle pasting a file */
onPasteFile: PropTypes.func,

Expand Down Expand Up @@ -75,6 +81,8 @@ const propTypes = {
const defaultProps = {
defaultValue: undefined,
value: undefined,
numberOfLines: 1,
onNumberOfLinesChange: () => {},
maxLines: -1,
onPasteFile: () => {},
shouldClear: false,
Expand Down Expand Up @@ -116,7 +124,7 @@ class Composer extends React.Component {
: `${props.value || ''}`;

this.state = {
numberOfLines: 1,
numberOfLines: props.numberOfLines || 1,
Copy link
Member

Choose a reason for hiding this comment

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

I believe only using props.numberOfLines will do the job? We are already using 1 for numberOfLines in default props.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, thanks.

selection: {
start: initialValue.length,
end: initialValue.length,
Expand Down Expand Up @@ -162,7 +170,8 @@ class Composer extends React.Component {
if (prevProps.value !== this.props.value
|| prevProps.defaultValue !== this.props.defaultValue
|| prevProps.isComposerFullSize !== this.props.isComposerFullSize
|| prevProps.windowWidth !== this.props.windowWidth) {
|| prevProps.windowWidth !== this.props.windowWidth
|| prevProps.numberOfLines !== this.props.numberOfLines) {
this.updateNumberOfLines();
}

Expand Down Expand Up @@ -334,11 +343,13 @@ class Composer extends React.Component {
const lineHeight = parseInt(computedStyle.lineHeight, 10) || 20;
const paddingTopAndBottom = parseInt(computedStyle.paddingBottom, 10)
+ parseInt(computedStyle.paddingTop, 10);
const numberOfLines = getNumberOfLines(this.props.maxLines, lineHeight, paddingTopAndBottom, this.textInput.scrollHeight);
const computedNumberOfLines = getNumberOfLines(this.props.maxLines, lineHeight, paddingTopAndBottom, this.textInput.scrollHeight);
const numberOfLines = computedNumberOfLines === 0 ? Math.max(this.props.numberOfLines, 1) : computedNumberOfLines;
Copy link
Member

Choose a reason for hiding this comment

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

I think we don't need Math.max

updateIsFullComposerAvailable(this.props, numberOfLines);
this.setState({
numberOfLines,
});
this.props.onNumberOfLinesChange(numberOfLines);
});
}

Expand All @@ -357,7 +368,6 @@ class Composer extends React.Component {
selection={this.state.selection}
onChange={this.shouldCallUpdateNumberOfLines}
onSelectionChange={this.onSelectionChange}
numberOfLines={this.state.numberOfLines}
style={[
propStyles,

Expand All @@ -367,6 +377,7 @@ class Composer extends React.Component {
]}
/* eslint-disable-next-line react/jsx-props-no-spreading */
{...propsWithoutStyles}
numberOfLines={this.state.numberOfLines}
disabled={this.props.isDisabled}
/>
);
Expand Down
21 changes: 21 additions & 0 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,15 @@ function saveReportComment(reportID, comment) {
Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${reportID}`, comment);
}

/**
* Saves the number of lines for the comment
* @param {String} reportID
* @param {Number} numberOfLines
*/
function saveReportCommentNumberOfLines(reportID, numberOfLines) {
Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT_NUMBER_OF_LINES}${reportID}`, numberOfLines);
}

/**
* Immediate indication whether the report has a draft comment.
*
Expand Down Expand Up @@ -935,6 +944,16 @@ function saveReportActionDraft(reportID, reportActionID, draftMessage) {
Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS}${reportID}_${reportActionID}`, draftMessage);
}

/**
* Saves the number of lines for the report action draft
* @param {String} reportID
* @param {Number} reportActionID
* @param {Number} numberOfLines
*/
function saveReportActionDraftNumberOfLines(reportID, reportActionID, numberOfLines) {
Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT_NUMBER_OF_LINES}${reportID}_${reportActionID}`, numberOfLines);
}

/**
* @param {String} reportID
* @param {String} previousValue
Expand Down Expand Up @@ -1375,11 +1394,13 @@ export {
subscribeToReportCommentPushNotifications,
unsubscribeFromReportChannel,
saveReportComment,
saveReportCommentNumberOfLines,
broadcastUserIsTyping,
togglePinnedState,
editReportComment,
handleUserDeletedLinks,
saveReportActionDraft,
saveReportActionDraftNumberOfLines,
deleteReportComment,
navigateToConciergeChat,
setReportWithDraft,
Expand Down
17 changes: 17 additions & 0 deletions src/pages/home/report/ReportActionCompose.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ const propTypes = {
/** The comment left by the user */
comment: PropTypes.string,

/** Number of lines for the comment */
numberOfLines: PropTypes.number,

/** The ID of the report actions will be created for */
reportID: PropTypes.string.isRequired,

Expand Down Expand Up @@ -107,6 +110,7 @@ const propTypes = {
const defaultProps = {
betas: [],
comment: '',
numberOfLines: 1,
modal: {},
report: {},
reportActions: [],
Expand Down Expand Up @@ -550,6 +554,14 @@ class ReportActionCompose extends React.Component {
}
}

/**
* Update the number of lines for a comment in Onyx
* @param {Number} numberOfLines
*/
updateNumberOfLines(numberOfLines) {
Report.saveReportCommentNumberOfLines(this.props.reportID, numberOfLines);
}

/**
* Listens for keyboard shortcuts and applies the action
*
Expand Down Expand Up @@ -823,6 +835,8 @@ class ReportActionCompose extends React.Component {
setIsFullComposerAvailable={this.setIsFullComposerAvailable}
isComposerFullSize={this.props.isComposerFullSize}
value={this.state.value}
numberOfLines={this.props.numberOfLines}
onNumberOfLinesChange={numberOfLines => this.updateNumberOfLines(numberOfLines)}
Copy link
Member

Choose a reason for hiding this comment

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

Let's bind updateNumberOfLines

onLayout={(e) => {
const composerHeight = e.nativeEvent.layout.height;
if (this.state.composerHeight === composerHeight) {
Expand Down Expand Up @@ -925,6 +939,9 @@ export default compose(
comment: {
key: ({reportID}) => `${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${reportID}`,
},
numberOfLines: {
key: ({reportID}) => `${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT_NUMBER_OF_LINES}${reportID}`,
},
modal: {
key: ONYXKEYS.MODAL,
},
Expand Down
22 changes: 22 additions & 0 deletions src/pages/home/report/ReportActionItemMessageEdit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {InteractionManager, Keyboard, View} from 'react-native';
import PropTypes from 'prop-types';
import _ from 'underscore';
import ExpensiMark from 'expensify-common/lib/ExpensiMark';
import {withOnyx} from 'react-native-onyx';
import reportActionPropTypes from './reportActionPropTypes';
import styles from '../../../styles/styles';
import Composer from '../../../components/Composer';
Expand All @@ -24,6 +25,7 @@ import CONST from '../../../CONST';
import withWindowDimensions, {windowDimensionsPropTypes} from '../../../components/withWindowDimensions';
import withLocalize, {withLocalizePropTypes} from '../../../components/withLocalize';
import withKeyboardState, {keyboardStatePropTypes} from '../../../components/withKeyboardState';
import ONYXKEYS from '../../../ONYXKEYS';

const propTypes = {
/** All the data of the action */
Expand All @@ -32,6 +34,9 @@ const propTypes = {
/** Draft message */
draftMessage: PropTypes.string.isRequired,

/** Number of lines for the draft message */
numberOfLines: PropTypes.number,

/** ReportID that holds the comment we're editing */
reportID: PropTypes.string.isRequired,

Expand All @@ -57,6 +62,7 @@ const defaultProps = {
forwardedRef: () => {},
report: {},
shouldDisableEmojiPicker: false,
numberOfLines: 1,
};

class ReportActionItemMessageEdit extends React.Component {
Expand Down Expand Up @@ -136,6 +142,14 @@ class ReportActionItemMessageEdit extends React.Component {
}
}

/**
* Update the number of lines for a draft in Onyx
* @param {Number} numberOfLines
*/
updateNumberOfLines(numberOfLines) {
Report.saveReportActionDraftNumberOfLines(this.props.reportID, this.props.action.reportActionID, numberOfLines);
}

/**
* Delete the draft of the comment being edited. This will take the comment out of "edit mode" with the old content.
*/
Expand Down Expand Up @@ -273,6 +287,8 @@ class ReportActionItemMessageEdit extends React.Component {
}}
selection={this.state.selection}
onSelectionChange={this.onSelectionChange}
numberOfLines={this.props.numberOfLines}
onNumberOfLinesChange={numberOfLines => this.updateNumberOfLines(numberOfLines)}
Copy link
Member

Choose a reason for hiding this comment

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

Same here

/>
<View style={styles.editChatItemEmojiWrapper}>
<EmojiPickerButton
Expand Down Expand Up @@ -314,6 +330,12 @@ export default compose(
withLocalize,
withWindowDimensions,
withKeyboardState,
withOnyx({
numberOfLines: {
key: ({reportID, action}) => `${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT_NUMBER_OF_LINES}${reportID}_${action.reportActionID}`,
initWithStoredValues: false,
},
}),
)(React.forwardRef((props, ref) => (
/* eslint-disable-next-line react/jsx-props-no-spreading */
<ReportActionItemMessageEdit {...props} forwardedRef={ref} />
Expand Down