-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Fixing comment edits when cursor is in the middle #458
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
Changes from all commits
d577410
5ff7856
508e9f2
bbc4822
5063dab
84740e3
f9e85e6
1929b64
319f640
7d07a2b
aa8a233
ed8551f
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,13 +1,45 @@ | ||
| import React from 'react'; | ||
| import {TextInput} from 'react-native'; | ||
| import PropTypes from 'prop-types'; | ||
| import _ from 'underscore'; | ||
|
|
||
| const propTypes = { | ||
| // A ref to forward to the text input | ||
| forwardedRef: PropTypes.func.isRequired, | ||
| }; | ||
|
|
||
| /** | ||
| * On native layers we like to have the Text Input not focused so the user can read new chats without they keyboard in | ||
| * the way of the view | ||
| */ | ||
| // eslint-disable-next-line react/jsx-props-no-spreading | ||
| const TextInputFocusable = props => (<TextInput maxHeight={116} {...props} />); | ||
| class TextInputFocusable extends React.Component { | ||
| 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)) { | ||
| this.props.forwardedRef(this.textInput); | ||
| } | ||
| } | ||
|
|
||
| render() { | ||
| return ( | ||
| <TextInput | ||
| ref={el => this.textInput = el} | ||
| maxHeight={116} | ||
| /* eslint-disable-next-line react/jsx-props-no-spreading */ | ||
| {...this.props} | ||
| /> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| TextInputFocusable.displayName = 'TextInputFocusable'; | ||
| TextInputFocusable.propTypes = propTypes; | ||
|
|
||
| export default TextInputFocusable; | ||
| // export default TextInputFocusable; | ||
| export default React.forwardRef((props, ref) => ( | ||
| /* eslint-disable-next-line react/jsx-props-no-spreading */ | ||
| <TextInputFocusable {...props} forwardedRef={ref} /> | ||
| )); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import React from 'react'; | ||
| import PropTypes from 'prop-types'; | ||
| import {View, Image, TouchableOpacity} from 'react-native'; | ||
| import _ from 'underscore'; | ||
| import styles, {colors} from '../../../style/StyleSheet'; | ||
| import TextInputFocusable from '../../../components/TextInputFocusable'; | ||
| import sendIcon from '../../../../assets/images/icon-send.png'; | ||
|
|
@@ -32,9 +33,29 @@ class ReportActionCompose extends React.Component { | |
| super(props); | ||
|
|
||
| this.updateComment = this.updateComment.bind(this); | ||
| this.debouncedSaveReportComment = _.debounce(this.debouncedSaveReportComment.bind(this), 1000, false); | ||
| this.submitForm = this.submitForm.bind(this); | ||
| this.triggerSubmitShortcut = this.triggerSubmitShortcut.bind(this); | ||
| this.submitForm = this.submitForm.bind(this); | ||
| this.comment = ''; | ||
| } | ||
|
|
||
| componentDidUpdate(prevProps) { | ||
| // The first time the component loads the props is empty and the next time it may contain value. | ||
| // If it does let's update this.comment so that it matches the defaultValue that we show in textInput. | ||
| if (this.props.comment && prevProps.comment === '' && prevProps.comment !== this.props.comment) { | ||
| this.comment = this.props.comment; | ||
| } | ||
|
Contributor
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. NAB, it's bugging me a little that we can't do this in the constructor but not much we can do right now
Contributor
Author
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. Yeah it's a little annoying but its the best i could think off such that we could still use debounce and have this.comment always be up to date. |
||
| } | ||
|
|
||
| /** | ||
| * Save our report comment in Ion. We debounce this method in the constructor so that it's not called too often | ||
| * to update Ion and re-render this component. | ||
| * | ||
| * @param {string} comment | ||
| */ | ||
| debouncedSaveReportComment(comment) { | ||
| saveReportComment(this.props.reportID, comment || ''); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -43,7 +64,8 @@ class ReportActionCompose extends React.Component { | |
| * @param {string} newComment | ||
| */ | ||
| updateComment(newComment) { | ||
| saveReportComment(this.props.reportID, newComment || ''); | ||
| this.comment = newComment; | ||
| this.debouncedSaveReportComment(newComment); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -69,7 +91,7 @@ class ReportActionCompose extends React.Component { | |
| e.preventDefault(); | ||
| } | ||
|
|
||
| const trimmedComment = this.props.comment.trim(); | ||
| const trimmedComment = this.comment.trim(); | ||
|
|
||
| // Don't submit empty comments | ||
| // @TODO show an error in the UI | ||
|
|
@@ -78,6 +100,7 @@ class ReportActionCompose extends React.Component { | |
| } | ||
|
|
||
| this.props.onSubmit(trimmedComment); | ||
| this.textInput.clear(); | ||
| this.updateComment(''); | ||
| } | ||
|
|
||
|
|
@@ -101,11 +124,12 @@ class ReportActionCompose extends React.Component { | |
| multiline | ||
| textAlignVertical="top" | ||
| placeholder="Write something..." | ||
| ref={el => this.textInput = el} | ||
| placeholderTextColor={colors.textSupporting} | ||
| onChangeText={this.updateComment} | ||
| onKeyPress={this.triggerSubmitShortcut} | ||
| style={[styles.textInput, styles.textInputCompose, styles.flex4]} | ||
| value={this.props.comment || ''} | ||
| defaultValue={this.props.comment || ''} | ||
| maxLines={16} // This is the same that slack has | ||
| /> | ||
| <TouchableOpacity | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.