Skip to content

Commit

Permalink
Merge pull request #15296 from Pujan92/fix/compose-box-onlayout-fix-1…
Browse files Browse the repository at this point in the history
…5024

composer set correct numberOfLines on intitial render
  • Loading branch information
marcochavezf authored Mar 27, 2023
2 parents 2f922c4 + 6caefe4 commit 773e0dd
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 4 deletions.
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,
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 ? this.props.numberOfLines : computedNumberOfLines;
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 @@ -631,6 +631,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 @@ -936,6 +945,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 @@ -1398,11 +1417,13 @@ export {
subscribeToReportCommentPushNotifications,
unsubscribeFromReportChannel,
saveReportComment,
saveReportCommentNumberOfLines,
broadcastUserIsTyping,
togglePinnedState,
editReportComment,
handleUserDeletedLinks,
saveReportActionDraft,
saveReportActionDraftNumberOfLines,
deleteReportComment,
navigateToConciergeChat,
setReportWithDraft,
Expand Down
18 changes: 18 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 @@ -116,6 +119,7 @@ const propTypes = {
const defaultProps = {
betas: [],
comment: '',
numberOfLines: 1,
modal: {},
report: {},
reportActions: [],
Expand Down Expand Up @@ -163,6 +167,7 @@ class ReportActionCompose extends React.Component {
this.addAttachment = this.addAttachment.bind(this);
this.insertSelectedEmoji = this.insertSelectedEmoji.bind(this);
this.setExceededMaxCommentLength = this.setExceededMaxCommentLength.bind(this);
this.updateNumberOfLines = this.updateNumberOfLines.bind(this);
this.comment = props.comment;

// React Native will retain focus on an input for native devices but web/mWeb behave differently so we have some focus management
Expand Down Expand Up @@ -561,6 +566,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 @@ -829,6 +842,8 @@ class ReportActionCompose extends React.Component {
setIsFullComposerAvailable={this.setIsFullComposerAvailable}
isComposerFullSize={this.props.isComposerFullSize}
value={this.state.value}
numberOfLines={this.props.numberOfLines}
onNumberOfLinesChange={this.updateNumberOfLines}
onLayout={(e) => {
const composerHeight = e.nativeEvent.layout.height;
if (this.state.composerHeight === composerHeight) {
Expand Down Expand Up @@ -931,6 +946,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
23 changes: 23 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 All @@ -70,6 +76,7 @@ class ReportActionItemMessageEdit extends React.Component {
this.onSelectionChange = this.onSelectionChange.bind(this);
this.addEmojiToTextBox = this.addEmojiToTextBox.bind(this);
this.setExceededMaxCommentLength = this.setExceededMaxCommentLength.bind(this);
this.updateNumberOfLines = this.updateNumberOfLines.bind(this);
this.saveButtonID = 'saveButton';
this.cancelButtonID = 'cancelButton';
this.emojiButtonID = 'emojiButton';
Expand Down Expand Up @@ -136,6 +143,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 +288,8 @@ class ReportActionItemMessageEdit extends React.Component {
}}
selection={this.state.selection}
onSelectionChange={this.onSelectionChange}
numberOfLines={this.props.numberOfLines}
onNumberOfLinesChange={this.updateNumberOfLines}
/>
<View style={styles.editChatItemEmojiWrapper}>
<EmojiPickerButton
Expand Down Expand Up @@ -314,6 +331,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

0 comments on commit 773e0dd

Please sign in to comment.