Skip to content

Commit

Permalink
fix: autofocus on open
Browse files Browse the repository at this point in the history
  • Loading branch information
spaenleh committed Apr 1, 2022
1 parent 646b82d commit beac425
Show file tree
Hide file tree
Showing 5 changed files with 334 additions and 294 deletions.
7 changes: 7 additions & 0 deletions cypress/plugins/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = (on, config) => {
if (config.testingType === 'component') {
require('@cypress/react/plugins/react-scripts')(on, config);
}

return config;
};
3 changes: 2 additions & 1 deletion example/src/components/ChatboxWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Chatbox, {
import { MUTATION_KEYS } from '@graasp/query-client';
import { useMutation, hooks } from '../config/queryClient';
import { PartialNewChatMessage } from '../../../src';
import { AvatarHookType } from '../../../src/types';

type Props = {};

Expand Down Expand Up @@ -58,7 +59,7 @@ const ChatboxWrapper: FC<Props> = () => {
sendMessageFunction={sendMessage}
deleteMessageFunction={deleteMessage}
editMessageFunction={editMessage}
useAvatarHook={hooks.useAvatar}
useAvatarHook={hooks.useAvatar as AvatarHookType}
/>
);
};
Expand Down
7 changes: 6 additions & 1 deletion src/components/Input.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { ChangeEvent, FC, RefObject } from 'react';
import React, { ChangeEvent, FC, RefObject, useEffect } from 'react';
import IconButton from '@material-ui/core/IconButton';
import TextField from '@material-ui/core/TextField';
import Box from '@material-ui/core/Box';
Expand Down Expand Up @@ -44,6 +44,11 @@ const Input: FC<Props> = ({

const { t } = useTranslation();

// autofocus on first render
useEffect(() => {
inputRef.current?.focus();
}, []);

const onClick = (): void => {
if (textInput) {
sendMessageFunction?.({ chatId, body: textInput });
Expand Down
16 changes: 16 additions & 0 deletions src/render-ui.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ImmutableMember, Member } from './types';
import { MEMBERS } from '../cypress/fixtures/members';
import { List } from 'immutable';
import { mockUseAvatar } from '../cypress/fixtures/mockHooks';
import { inputTextFieldTextAreaCypress } from './config/selectors';

describe('Render Avatar', () => {
beforeEach(() => {
Expand Down Expand Up @@ -32,3 +33,18 @@ describe('Render Avatar', () => {
cy.get(`@${fakeHookName}`).should('have.been.called');
});
});

describe('Autofocus on first render', () => {
it('should autofocus on open', () => {
mount(
<Chatbox
chatId={CHAT_ID}
currentMember={new ImmutableMember(MEMBERS.ANNA)}
members={List(Object.values(MEMBERS) as Member[])}
messages={List(CHAT_MESSAGES)}
/>,
).then(() =>
cy.get(`#${inputTextFieldTextAreaCypress}`).should('be.focused'),
);
});
});
Loading

0 comments on commit beac425

Please sign in to comment.