Skip to content

Commit

Permalink
MM-13384 Fix clicking username in profile popover (mattermost#2161)
Browse files Browse the repository at this point in the history
* Fix clicking username in profile popover

* Update snapshots
  • Loading branch information
jwilander committed Dec 12, 2018
1 parent 33b56f9 commit 45ffce3
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 14 deletions.
3 changes: 3 additions & 0 deletions components/__snapshots__/textbox.test.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ exports[`components/TextBox should match snapshot with required props 1`] = `
isRHS={false}
listComponent={[Function]}
listStyle="top"
listenForMentionKeyClick={false}
onBlur={[Function]}
onChange={[Function]}
onHeightChange={[Function]}
Expand Down Expand Up @@ -168,6 +169,7 @@ exports[`components/TextBox should throw error when new property is too long 1`]
isRHS={false}
listComponent={[Function]}
listStyle="top"
listenForMentionKeyClick={false}
onBlur={[Function]}
onChange={[Function]}
onHeightChange={[Function]}
Expand Down Expand Up @@ -323,6 +325,7 @@ exports[`components/TextBox should throw error when value is too long 1`] = `
isRHS={false}
listComponent={[Function]}
listStyle="top"
listenForMentionKeyClick={false}
onBlur={[Function]}
onChange={[Function]}
onHeightChange={[Function]}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ exports[`components/AddUserToChannelModal should match snapshot 1`] = `
isRHS={false}
listComponent={[Function]}
listStyle="bottom"
listenForMentionKeyClick={false}
maxLength="64"
onChange={[Function]}
onItemSelected={[Function]}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ exports[`components/CreateComment should match snapshot read only channel 1`] =
id="reply_textbox"
initialText=""
isRHS={true}
listenForMentionKeyClick={true}
onBlur={[Function]}
onChange={[Function]}
onKeyDown={[Function]}
Expand Down Expand Up @@ -116,6 +117,7 @@ exports[`components/CreateComment should match snapshot, comment with message 1`
id="reply_textbox"
initialText=""
isRHS={true}
listenForMentionKeyClick={true}
onBlur={[Function]}
onChange={[Function]}
onKeyDown={[Function]}
Expand Down Expand Up @@ -243,6 +245,7 @@ exports[`components/CreateComment should match snapshot, emoji picker disabled 1
id="reply_textbox"
initialText=""
isRHS={true}
listenForMentionKeyClick={true}
onBlur={[Function]}
onChange={[Function]}
onKeyDown={[Function]}
Expand Down Expand Up @@ -358,6 +361,7 @@ exports[`components/CreateComment should match snapshot, empty comment 1`] = `
id="reply_textbox"
initialText=""
isRHS={true}
listenForMentionKeyClick={true}
onBlur={[Function]}
onChange={[Function]}
onKeyDown={[Function]}
Expand Down Expand Up @@ -485,6 +489,7 @@ exports[`components/CreateComment should match snapshot, non-empty message and u
id="reply_textbox"
initialText=""
isRHS={true}
listenForMentionKeyClick={true}
onBlur={[Function]}
onChange={[Function]}
onKeyDown={[Function]}
Expand Down
1 change: 1 addition & 0 deletions components/create_comment/create_comment.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,7 @@ export default class CreateComment extends React.PureComponent {
disabled={readOnlyChannel}
characterLimit={this.props.maxPostSize}
badConnection={this.props.badConnection}
listenForMentionKeyClick={true}
/>
<span
ref='createCommentControls'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ exports[`components/create_post Show tutorial 1`] = `
emojiEnabled={true}
handlePostError={[Function]}
id="post_textbox"
listenForMentionKeyClick={true}
onBlur={[Function]}
onChange={[Function]}
onKeyDown={[Function]}
Expand Down Expand Up @@ -188,6 +189,7 @@ exports[`components/create_post should match snapshot for center textbox 1`] = `
emojiEnabled={true}
handlePostError={[Function]}
id="post_textbox"
listenForMentionKeyClick={true}
onBlur={[Function]}
onChange={[Function]}
onKeyDown={[Function]}
Expand Down Expand Up @@ -320,6 +322,7 @@ exports[`components/create_post should match snapshot for read only channel 1`]
emojiEnabled={true}
handlePostError={[Function]}
id="post_textbox"
listenForMentionKeyClick={true}
onBlur={[Function]}
onChange={[Function]}
onKeyDown={[Function]}
Expand Down Expand Up @@ -418,6 +421,7 @@ exports[`components/create_post should match snapshot when file upload disabled
emojiEnabled={true}
handlePostError={[Function]}
id="post_textbox"
listenForMentionKeyClick={true}
onBlur={[Function]}
onChange={[Function]}
onKeyDown={[Function]}
Expand Down Expand Up @@ -550,6 +554,7 @@ exports[`components/create_post should match snapshot, init 1`] = `
emojiEnabled={true}
handlePostError={[Function]}
id="post_textbox"
listenForMentionKeyClick={true}
onBlur={[Function]}
onChange={[Function]}
onKeyDown={[Function]}
Expand Down
1 change: 1 addition & 0 deletions components/create_post/create_post.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,7 @@ export default class CreatePost extends React.Component {
disabled={readOnlyChannel}
characterLimit={this.props.maxPostSize}
badConnection={this.props.badConnection}
listenForMentionKeyClick={true}
/>
<span
ref='createPostControls'
Expand Down
2 changes: 1 addition & 1 deletion components/profile_popover/profile_popover.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class ProfilePopover extends React.PureComponent {
if (this.props.hide) {
this.props.hide();
}
EventEmitter.emit('mention_key_click', this.props.user.username);
EventEmitter.emit('mention_key_click', this.props.user.username, this.props.isRHS);
}

handleEditAccountSettings(e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ exports[`components/QuickSwitchModal should match snapshot 1`] = `
isRHS={false}
listComponent={[Function]}
listStyle="bottom"
listenForMentionKeyClick={false}
maxLength="64"
onChange={[Function]}
onItemSelected={[Function]}
Expand Down
34 changes: 34 additions & 0 deletions components/suggestion/suggestion_box.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import PropTypes from 'prop-types';
import React from 'react';

import EventEmitter from 'mattermost-redux/utils/event_emitter';

import QuickInput from 'components/quick_input.jsx';
import Constants from 'utils/constants.jsx';
import * as UserAgent from 'utils/user_agent.jsx';
Expand Down Expand Up @@ -119,6 +121,11 @@ export default class SuggestionBox extends React.Component {
* box is rendered. This allows the reused component to otherwise respond to changes.
*/
contextId: PropTypes.string,

/**
* If true, listen for clicks on a mention and populate the input with said mention, defaults to false
*/
listenForMentionKeyClick: PropTypes.bool,
}

static defaultProps = {
Expand All @@ -132,6 +139,7 @@ export default class SuggestionBox extends React.Component {
openOnFocus: false,
openWhenEmpty: false,
replaceAllInputOnSelect: false,
listenForMentionKeyClick: false,
}

constructor(props) {
Expand Down Expand Up @@ -164,6 +172,16 @@ export default class SuggestionBox extends React.Component {
};
}

componentDidMount() {
if (this.props.listenForMentionKeyClick) {
EventEmitter.addListener('mention_key_click', this.handleMentionKeyClick);
}
}

componentWillUnmount() {
EventEmitter.removeListener('mention_key_click', this.handleMentionKeyClick);
}

componentDidUpdate(prevProps) {
if (prevProps.contextId !== this.props.contextId) {
const textbox = this.getTextbox();
Expand All @@ -173,6 +191,21 @@ export default class SuggestionBox extends React.Component {
}
}

handleMentionKeyClick = (mentionKey, isRHS) => {
if (this.props.isRHS !== isRHS) {
return;
}

let insertText = '@' + mentionKey;

// if the current text does not end with a whitespace, then insert a space
if (this.props.value && (/[^\s]$/).test(this.props.value)) {
insertText = ' ' + insertText;
}

this.addTextAtCaret(insertText, '');
}

getTextbox = () => {
if (!this.refs.input) {
return null;
Expand Down Expand Up @@ -581,6 +614,7 @@ export default class SuggestionBox extends React.Component {
Reflect.deleteProperty(props, 'replaceAllInputOnSelect');
Reflect.deleteProperty(props, 'renderDividers');
Reflect.deleteProperty(props, 'contextId');
Reflect.deleteProperty(props, 'listenForMentionKeyClick');

// This needs to be upper case so React doesn't think it's an html tag
const SuggestionListComponent = listComponent;
Expand Down
16 changes: 3 additions & 13 deletions components/textbox/textbox.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export default class Textbox extends React.Component {
characterLimit: PropTypes.number.isRequired,
disabled: PropTypes.bool,
badConnection: PropTypes.bool,
listenForMentionKeyClick: PropTypes.bool,
currentUserId: PropTypes.string.isRequired,
profilesInChannel: PropTypes.arrayOf(PropTypes.object).isRequired,
profilesNotInChannel: PropTypes.arrayOf(PropTypes.object).isRequired,
Expand All @@ -51,6 +52,7 @@ export default class Textbox extends React.Component {
static defaultProps = {
supportsCommands: true,
isRHS: false,
listenForMentionKeyClick: false,
};

constructor(props) {
Expand Down Expand Up @@ -80,19 +82,6 @@ export default class Textbox extends React.Component {
this.props.onChange(e);
}

handlePopoverMentionKeyClick = (mentionKey) => {
const textbox = this.refs.message.getTextbox();
let insertText = '@' + mentionKey;
const oldValue = textbox.value;

// if the current text does not end with a whitespace, then insert a space
if (oldValue && (/[^\s]$/).test(oldValue)) {
insertText = ' ' + insertText;
}

textbox.value = oldValue + insertText;
}

checkMessageLength = (message) => {
if (this.props.handlePostError) {
if (message.length > this.props.characterLimit) {
Expand Down Expand Up @@ -337,6 +326,7 @@ export default class Textbox extends React.Component {
isRHS={this.props.isRHS}
disabled={this.props.disabled}
contextId={this.props.channelId}
listenForMentionKeyClick={this.props.listenForMentionKeyClick}
/>
{preview}
<div className={'help__text ' + helpTextClass}>
Expand Down
3 changes: 3 additions & 0 deletions components/widgets/settings/autocomplete_selector.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ describe('components/widgets/settings/AutocompleteSelector', () => {
isRHS={false}
listComponent={[Function]}
listStyle="top"
listenForMentionKeyClick={false}
onBlur={[Function]}
onChange={[Function]}
onFocus={[Function]}
Expand Down Expand Up @@ -82,6 +83,7 @@ describe('components/widgets/settings/AutocompleteSelector', () => {
isRHS={false}
listComponent={[Function]}
listStyle="top"
listenForMentionKeyClick={false}
onBlur={[Function]}
onChange={[Function]}
onFocus={[Function]}
Expand Down Expand Up @@ -121,6 +123,7 @@ describe('components/widgets/settings/AutocompleteSelector', () => {
isRHS={false}
listComponent={[Function]}
listStyle="top"
listenForMentionKeyClick={false}
onBlur={[Function]}
onChange={[Function]}
onFocus={[Function]}
Expand Down

0 comments on commit 45ffce3

Please sign in to comment.