Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
MM-19519 - PostContext to handle menus opened/closed within Posts (#4124
Browse files Browse the repository at this point in the history
)

* Created react context for post and included opened menu handler for child components

* Feedback review
  • Loading branch information
marianunez authored and sudheerDev committed Nov 12, 2019
1 parent 936c87b commit 5a89e8b
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 49 deletions.
9 changes: 9 additions & 0 deletions components/autocomplete_selector.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default class AutocompleteSelector extends React.PureComponent {
helpText: PropTypes.node,
placeholder: PropTypes.string,
footer: PropTypes.node,
toggleFocus: PropTypes.func,
};

static defaultProps = {
Expand Down Expand Up @@ -63,10 +64,18 @@ export default class AutocompleteSelector extends React.PureComponent {

onFocus = () => {
this.setState({focused: true});

if (this.props.toggleFocus) {
this.props.toggleFocus(true);
}
}

onBlur = () => {
this.setState({focused: false});

if (this.props.toggleFocus) {
this.props.toggleFocus(false);
}
}

render() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import MenuActionProvider from 'components/suggestion/menu_action_provider';
import GenericUserProvider from 'components/suggestion/generic_user_provider.jsx';
import GenericChannelProvider from 'components/suggestion/generic_channel_provider.jsx';
import AutocompleteSelector from 'components/autocomplete_selector';
import PostContext from 'components/post_view/post_context';

export default class ActionMenu extends React.PureComponent {
static propTypes = {
Expand Down Expand Up @@ -89,13 +90,18 @@ export default class ActionMenu extends React.PureComponent {
const {action} = this.props;

return (
<AutocompleteSelector
providers={this.providers}
onSelected={this.handleSelected}
placeholder={action.name}
inputClassName='post-attachment-dropdown'
value={this.state.value}
/>
<PostContext.Consumer>
{({handlePopupOpened}) => (
<AutocompleteSelector
providers={this.providers}
onSelected={this.handleSelected}
placeholder={action.name}
inputClassName='post-attachment-dropdown'
value={this.state.value}
toggleFocus={handlePopupOpened}
/>
)}
</PostContext.Consumer>
);
}
}
88 changes: 46 additions & 42 deletions components/post_view/post/post.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {A11yCustomEventTypes} from 'utils/constants';
import PostProfilePicture from 'components/post_profile_picture';
import PostBody from 'components/post_view/post_body';
import PostHeader from 'components/post_view/post_header';
import PostContext from 'components/post_view/post_context';

class Post extends React.PureComponent {
static propTypes = {
Expand Down Expand Up @@ -292,52 +293,55 @@ class Post extends React.PureComponent {
}

return (
<div
ref={this.postRef}
id={'post_' + post.id}
data-testid='postView'
role='listitem'
className={`a11y__section ${this.getClassName(post, isSystemMessage, isMeMessage, fromWebhook, fromAutoResponder, fromBot)}`}
tabIndex='0'
onFocus={this.handlePostFocus}
onBlur={this.removeFocus}
onMouseOver={this.setHover}
onMouseLeave={this.unsetHover}
onTouchStart={this.setHover}
aria-label={this.state.currentAriaLabel}
aria-atomic={true}
>
<PostContext.Provider value={{handlePopupOpened: this.handleDropdownOpened}}>
<div
role='application'
data-testid='postContent'
className={'post__content ' + centerClass}
aria-hidden={this.state.ariaHidden}
ref={this.postRef}
id={'post_' + post.id}
data-testid='postView'
role='listitem'
className={`a11y__section ${this.getClassName(post, isSystemMessage, isMeMessage, fromWebhook, fromAutoResponder, fromBot)}`}
tabIndex='0'
onFocus={this.handlePostFocus}
onBlur={this.removeFocus}
onMouseOver={this.setHover}
onMouseLeave={this.unsetHover}
onTouchStart={this.setHover}
aria-label={this.state.currentAriaLabel}
aria-atomic={true}
>
<div className='post__img'>
{profilePic}
</div>
<div>
<PostHeader
post={post}
handleCommentClick={this.handleCommentClick}
handleCardClick={this.handleCardClick}
handleDropdownOpened={this.handleDropdownOpened}
compactDisplay={this.props.compactDisplay}
isFirstReply={this.props.isFirstReply}
replyCount={this.props.replyCount}
showTimeWithoutHover={!hideProfilePicture}
hover={this.state.hover || this.state.a11yActive}
/>
<PostBody
post={post}
handleCommentClick={this.handleCommentClick}
compactDisplay={this.props.compactDisplay}
isCommentMention={this.props.isCommentMention}
isFirstReply={this.props.isFirstReply}
/>
<div
role='application'
id='postContent'
data-testid='postContent'
className={'post__content ' + centerClass}
aria-hidden={this.state.ariaHidden}
>
<div className='post__img'>
{profilePic}
</div>
<div>
<PostHeader
post={post}
handleCommentClick={this.handleCommentClick}
handleCardClick={this.handleCardClick}
handleDropdownOpened={this.handleDropdownOpened}
compactDisplay={this.props.compactDisplay}
isFirstReply={this.props.isFirstReply}
replyCount={this.props.replyCount}
showTimeWithoutHover={!hideProfilePicture}
hover={this.state.hover || this.state.a11yActive}
/>
<PostBody
post={post}
handleCommentClick={this.handleCommentClick}
compactDisplay={this.props.compactDisplay}
isCommentMention={this.props.isCommentMention}
isFirstReply={this.props.isFirstReply}
/>
</div>
</div>
</div>
</div>
</PostContext.Provider>
);
}
}
Expand Down
14 changes: 14 additions & 0 deletions components/post_view/post_context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import React from 'react';

const PostContext = React.createContext({

// Post component event handler that should be
// called when any child component opens/closes a
// popup type component.
handlePopupOpened: null,
});

export default PostContext;

0 comments on commit 5a89e8b

Please sign in to comment.