Skip to content

Commit

Permalink
[FIX] User auto complete breaks on enter key press (#27213)
Browse files Browse the repository at this point in the history
  • Loading branch information
yash-rajpal authored Nov 16, 2022
1 parent b3220b6 commit ed63a76
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { MultiSelectFiltered, Icon, Box, Chip } from '@rocket.chat/fuselage';
import type { Options } from '@rocket.chat/fuselage';
import { useDebouncedValue } from '@rocket.chat/fuselage-hooks';
import { useEndpoint } from '@rocket.chat/ui-contexts';
import { useQuery } from '@tanstack/react-query';
import React, { memo, ReactElement, useState, ComponentProps } from 'react';
import React, { memo, ReactElement, useState, useCallback, useMemo } from 'react';

import UserAvatar from '../avatar/UserAvatar';
import AutocompleteOptions, { OptionsContext } from './UserAutoCompleteMultipleOptions';
Expand Down Expand Up @@ -55,33 +54,58 @@ const UserAutoCompleteMultipleFederated = ({
{ keepPreviousData: true },
);

const options = data || [];
const options = useMemo(() => data || [], [data]);

const onAddSelected: ComponentProps<typeof Options>['onSelect'] = ([value]) => {
setFilter('');
const cachedOption = options.find(([curVal]) => curVal === value)?.[1];
if (!cachedOption) {
throw new Error('UserAutoCompleteMultiple - onAddSelected - failed to cache option');
}
setSelectedCache({ ...selectedCache, [value]: cachedOption });
};
const onAddUser = useCallback(
(username: string): void => {
const user = options.find(([val]) => val === username)?.[1];
if (!user) {
throw new Error('UserAutoCompleteMultiple - onAddSelected - failed to cache option');
}
setSelectedCache((selectedCache) => ({ ...selectedCache, [username]: user }));
},
[setSelectedCache, options],
);

const onRemoveUser = useCallback(
(username: string): void =>
setSelectedCache((selectedCache) => {
const users = { ...selectedCache };
delete users[username];
return users;
}),
[setSelectedCache],
);

const handleOnChange = useCallback(
(usernames: string[]) => {
onChange(usernames);
const newAddedUsername = usernames.filter((username) => !value.includes(username))[0];
const removedUsername = value.filter((username) => !usernames.includes(username))[0];
setFilter('');
newAddedUsername && onAddUser(newAddedUsername);
removedUsername && onRemoveUser(removedUsername);
},
[onChange, setFilter, onAddUser, onRemoveUser, value],
);

return (
<OptionsContext.Provider value={{ options, onSelect: onAddSelected }}>
<OptionsContext.Provider value={{ options }}>
<MultiSelectFiltered
data-qa-type='user-auto-complete-input'
placeholder={placeholder}
value={value}
onChange={onChange}
onChange={handleOnChange}
filter={filter}
setFilter={setFilter}
renderSelected={({ value, onMouseDown }: { value: string; onMouseDown: () => void }): ReactElement => {
const currentCachedOption = selectedCache[value];
const currentCachedOption = selectedCache[value] || {};

return (
<Chip key={value} {...props} height='x20' onMouseDown={onMouseDown} mie='x4' mb='x2'>
{currentCachedOption._federated ? <Icon size='x20' name='globe' /> : <UserAvatar size='x20' username={value} />}
<Box is='span' margin='none' mis='x4'>
{currentCachedOption.name || currentCachedOption.username}
{currentCachedOption.name || currentCachedOption.username || value}
</Box>
</Chip>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,22 @@ type Options = Array<[UserAutoCompleteOptionType['username'], UserAutoCompleteOp

type OptionsContextValue = {
options: ComponentProps<typeof Options>['options'];
onSelect: ComponentProps<typeof Options>['onSelect'];
};

export const OptionsContext = createContext<OptionsContextValue>({
options: [],
onSelect: () => undefined,
});
const UserAutoCompleteMultipleOptions = forwardRef(function UserAutoCompleteMultipleOptions(
{ onSelect: _onSelect, ...props }: ComponentProps<typeof Options>,
{ onSelect, ...props }: ComponentProps<typeof Options>,
ref: Ref<HTMLElement>,
): ReactElement {
const { options, onSelect } = useContext(OptionsContext);
const { options } = useContext(OptionsContext);
return (
<Options
{...props}
key='AutocompleteOptions'
options={options}
onSelect={(val): void => {
onSelect(val);
_onSelect(val);
}}
onSelect={onSelect}
ref={ref}
renderItem={UserAutoCompleteMultipleOption}
/>
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/client/sidebar/header/CreateDirectMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const CreateDirectMessage: FC<CreateDirectMessageProps> = ({ onClose }) => {
});

return (
<Modal>
<Modal data-qa='create-direct-modal'>
<Modal.Header>
<Modal.Title>{t('Direct_Messages')}</Modal.Title>
<Modal.Close onClick={onClose} />
Expand Down
4 changes: 2 additions & 2 deletions apps/meteor/tests/e2e/create-channel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ test.describe.serial('channel-management', () => {
await poHomeChannel.sidenav.openNewByLabel('Channel');
await poHomeChannel.sidenav.checkboxPrivateChannel.click();
await poHomeChannel.sidenav.inputChannelName.type(channelName);
await poHomeChannel.sidenav.btnCreateChannel.click();
await poHomeChannel.sidenav.btnCreate.click();

await expect(page).toHaveURL(`/channel/${channelName}`);
});
Expand All @@ -30,7 +30,7 @@ test.describe.serial('channel-management', () => {

await poHomeChannel.sidenav.openNewByLabel('Channel');
await poHomeChannel.sidenav.inputChannelName.type(channelName);
await poHomeChannel.sidenav.btnCreateChannel.click();
await poHomeChannel.sidenav.btnCreate.click();

await expect(page).toHaveURL(`/group/${channelName}`);
});
Expand Down
26 changes: 26 additions & 0 deletions apps/meteor/tests/e2e/create-direct.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { test, expect } from './utils/test';
import { HomeChannel } from './page-objects';

test.use({ storageState: 'admin-session.json' });

test.describe.serial('channel-direct-message', () => {
let poHomeChannel: HomeChannel;

test.beforeEach(async ({ page }) => {
poHomeChannel = new HomeChannel(page);

await page.goto('/home');
});

test('expect create a direct room', async ({ page }) => {
await poHomeChannel.sidenav.openNewByLabel('Direct Messages');

await poHomeChannel.sidenav.inputDirectUsername.click();
await page.keyboard.type('rocket.cat');
await page.waitForTimeout(200);
await page.keyboard.press('Enter');
await poHomeChannel.sidenav.btnCreate.click();

await expect(page).toHaveURL('direct/rocket.catrocketchat.internal.admin.test');
});
});
8 changes: 6 additions & 2 deletions apps/meteor/tests/e2e/page-objects/fragments/home-sidenav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ export class HomeSidenav {
return this.page.locator('#modal-root [data-qa="create-channel-modal"] [data-qa-type="channel-name-input"]');
}

get btnCreateChannel(): Locator {
get inputDirectUsername(): Locator {
return this.page.locator('#modal-root [data-qa="create-direct-modal"] [data-qa-type="user-auto-complete-input"]');
}

get btnCreate(): Locator {
return this.page.locator('//*[@id="modal-root"]//button[contains(text(), "Create")]');
}

Expand Down Expand Up @@ -56,6 +60,6 @@ export class HomeSidenav {
await this.openNewByLabel('Channel');
await this.checkboxPrivateChannel.click();
await this.inputChannelName.type(name);
await this.btnCreateChannel.click();
await this.btnCreate.click();
}
}

0 comments on commit ed63a76

Please sign in to comment.