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

Fix lost focus for chat input #369

Merged
merged 1 commit into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/components/chat-view-container/chat-view-container.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('ChannelViewContainer', () => {
isLoading: false,
data: null,
},
activeMessengerId: '',
sendMessage: () => undefined,
uploadFileMessage: () => undefined,
fetchUsers: () => undefined,
Expand Down Expand Up @@ -409,6 +410,38 @@ describe('ChannelViewContainer', () => {
expect(textareaRef.current.focus).toHaveBeenCalled();
});

it('should not call focus on message input render if activeMessengerId not equal the id of textareaRef', () => {
const activeMessengerId = '1';
const textareaRef = {
current: {
focus: jest.fn(),
id: '3',
},
};

const wrapper = subject({ activeMessengerId });

(wrapper.instance() as any).onMessageInputRendered(textareaRef);

expect(textareaRef.current.focus).not.toHaveBeenCalled();
});

it('should call focus on message input render if activeMessengerId equal the id of textareaRef', () => {
const activeMessengerId = '1';
const textareaRef = {
current: {
focus: jest.fn(),
id: activeMessengerId,
},
};

const wrapper = subject({ activeMessengerId });

(wrapper.instance() as any).onMessageInputRendered(textareaRef);

expect(textareaRef.current.focus).toHaveBeenCalled();
});

describe('mapState', () => {
const getState = (state: any) =>
({
Expand All @@ -421,6 +454,9 @@ describe('ChannelViewContainer', () => {
data: null,
},
},
chat: {
activeMessengerId: '1',
},
} as RootState);

test('channel', () => {
Expand Down
10 changes: 9 additions & 1 deletion src/components/chat-view-container/chat-view-container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export interface Properties extends PublicProperties {
markAllMessagesAsReadInChannel: (payload: MarkAsReadPayload) => void;
startMessageSync: (payload: PayloadFetchMessages) => void;
stopSyncChannels: (payload: PayloadFetchMessages) => void;
activeMessengerId?: string;
context: {
isAuthenticated: boolean;
};
Expand All @@ -70,11 +71,13 @@ export class Container extends React.Component<Properties, State> {
const channel = denormalize(props.channelId, state) || null;
const {
authentication: { user },
chat: { activeMessengerId },
} = state;

return {
channel,
user,
activeMessengerId,
};
}

Expand Down Expand Up @@ -245,7 +248,12 @@ export class Container extends React.Component<Properties, State> {

onMessageInputRendered = (textareaRef: RefObject<HTMLTextAreaElement>) => {
if (textareaRef && textareaRef.current) {
textareaRef.current.focus();
if (
(this.props.activeMessengerId && this.props.activeMessengerId === textareaRef.current.id) ||
!this.props.activeMessengerId
) {
textareaRef.current.focus();
}
}
};

Expand Down
1 change: 1 addition & 0 deletions src/components/chat-view-container/chat-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ export class ChatView extends React.Component<Properties, State> {
{isMemberOfChannel && (
<MessageInput
onMessageInputRendered={this.props.onMessageInputRendered}
id={this.props.id}
onSubmit={this.props.sendMessage}
getUsersForMentions={this.searchMentionableUsers}
reply={this.props.reply}
Expand Down
2 changes: 2 additions & 0 deletions src/components/message-input/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require('./styles.scss');
export interface Properties {
className?: string;
placeholder?: string;
id?: string;
onSubmit: (message: string, mentionedUserIds: User['id'][], media: Media[]) => void;
initialValue?: string;
reply?: null | ParentMessage;
Expand Down Expand Up @@ -199,6 +200,7 @@ export class MessageInput extends React.Component<Properties, State> {
<MentionsInput
inputRef={this.textareaRef}
className='mentions-text-area__wrap'
id={this.props.id}
placeholder={this.props.placeholder}
onKeyDown={this.onSubmit}
onChange={this.contentChanged}
Expand Down