-
Notifications
You must be signed in to change notification settings - Fork 10.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(UiKit): Channels select (#31918)
- Loading branch information
1 parent
2d84fe2
commit ee5cdfc
Showing
15 changed files
with
494 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@rocket.chat/fuselage-ui-kit": minor | ||
"@rocket.chat/ui-kit": minor | ||
--- | ||
|
||
Introduced new elements for apps to select channels |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { TextEncoder, TextDecoder } from 'util'; | ||
|
||
global.TextEncoder = TextEncoder; | ||
// @ts-ignore | ||
Check warning on line 4 in packages/fuselage-ui-kit/jest.setup.ts GitHub Actions / 🔎 Code Check / Code Lint
|
||
global.TextDecoder = TextDecoder; | ||
|
||
global.ResizeObserver = jest.fn().mockImplementation(() => ({ | ||
observe: jest.fn(), | ||
unobserve: jest.fn(), | ||
disconnect: jest.fn(), | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
packages/fuselage-ui-kit/src/elements/ChannelsSelectElement/ChannelsSelectElement.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { RoomType } from '@rocket.chat/apps-engine/definition/rooms'; | ||
import { MockedServerContext } from '@rocket.chat/mock-providers'; | ||
import type { ChannelsSelectElement as ChannelsSelectElementType } from '@rocket.chat/ui-kit'; | ||
import { BlockContext } from '@rocket.chat/ui-kit'; | ||
import { render, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
import { contextualBarParser } from '../../surfaces'; | ||
import ChannelsSelectElement from './ChannelsSelectElement'; | ||
import { useChannelsData } from './hooks/useChannelsData'; | ||
|
||
const channelsBlock: ChannelsSelectElementType = { | ||
type: 'channels_select', | ||
appId: 'test', | ||
blockId: 'test', | ||
actionId: 'test', | ||
}; | ||
|
||
jest.mock('./hooks/useChannelsData'); | ||
|
||
const mockedOptions: ReturnType<typeof useChannelsData> = [ | ||
{ | ||
value: 'channel1_id', | ||
label: { | ||
name: 'Channel 1', | ||
avatarETag: 'test', | ||
type: RoomType.CHANNEL, | ||
}, | ||
}, | ||
{ | ||
value: 'channel2_id', | ||
label: { | ||
name: 'Channel 2', | ||
avatarETag: 'test', | ||
type: RoomType.CHANNEL, | ||
}, | ||
}, | ||
{ | ||
value: 'channel3_id', | ||
label: { | ||
name: 'Channel 3', | ||
avatarETag: 'test', | ||
type: RoomType.CHANNEL, | ||
}, | ||
}, | ||
]; | ||
|
||
const mockUseChannelsData = jest.mocked(useChannelsData); | ||
mockUseChannelsData.mockReturnValue(mockedOptions); | ||
|
||
describe('UiKit ChannelsSelect Element', () => { | ||
beforeAll(() => { | ||
jest.useFakeTimers(); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.useRealTimers(); | ||
}); | ||
|
||
beforeEach(() => { | ||
render( | ||
<MockedServerContext> | ||
<ChannelsSelectElement | ||
index={0} | ||
block={channelsBlock} | ||
context={BlockContext.FORM} | ||
surfaceRenderer={contextualBarParser} | ||
/> | ||
</MockedServerContext> | ||
); | ||
}); | ||
|
||
it('should render a UiKit channel selector', async () => { | ||
expect(await screen.findByRole('textbox')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should open the channel selector', async () => { | ||
const input = await screen.findByRole('textbox'); | ||
input.focus(); | ||
|
||
expect(await screen.findByRole('listbox')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should select a channel', async () => { | ||
const input = await screen.findByRole('textbox'); | ||
|
||
input.focus(); | ||
|
||
const option = (await screen.findAllByRole('option'))[0]; | ||
await userEvent.click(option, { delay: null }); | ||
|
||
const selected = await screen.findByRole('button'); | ||
expect(selected).toHaveValue('channel1_id'); | ||
}); | ||
}); |
78 changes: 78 additions & 0 deletions
78
packages/fuselage-ui-kit/src/elements/ChannelsSelectElement/ChannelsSelectElement.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { | ||
AutoComplete, | ||
Option, | ||
Box, | ||
Options, | ||
Chip, | ||
} from '@rocket.chat/fuselage'; | ||
import { useDebouncedValue } from '@rocket.chat/fuselage-hooks'; | ||
import { RoomAvatar } from '@rocket.chat/ui-avatar'; | ||
import type * as UiKit from '@rocket.chat/ui-kit'; | ||
import { memo, useCallback, useState } from 'react'; | ||
|
||
import { useUiKitState } from '../../hooks/useUiKitState'; | ||
import type { BlockProps } from '../../utils/BlockProps'; | ||
import { useChannelsData } from './hooks/useChannelsData'; | ||
|
||
type ChannelsSelectElementProps = BlockProps<UiKit.ChannelsSelectElement>; | ||
|
||
const ChannelsSelectElement = ({ | ||
block, | ||
context, | ||
}: ChannelsSelectElementProps) => { | ||
const [{ value, loading }, action] = useUiKitState(block, context); | ||
|
||
const [filter, setFilter] = useState(''); | ||
const filterDebounced = useDebouncedValue(filter, 300); | ||
|
||
const options = useChannelsData({ filter: filterDebounced }); | ||
|
||
const handleChange = useCallback( | ||
(value) => { | ||
action({ target: { value } }); | ||
}, | ||
[action] | ||
); | ||
|
||
return ( | ||
<AutoComplete | ||
value={value} | ||
onChange={handleChange} | ||
disabled={loading} | ||
filter={filter} | ||
setFilter={setFilter} | ||
renderSelected={({ selected: { value, label } }) => ( | ||
<Chip height='x20' value={value} mie={4}> | ||
<RoomAvatar | ||
size='x20' | ||
room={{ type: label?.type || 'c', _id: value, ...label }} | ||
/> | ||
<Box verticalAlign='middle' is='span' margin='none' mi={4}> | ||
{label.name} | ||
</Box> | ||
</Chip> | ||
)} | ||
renderItem={({ value, label, ...props }) => ( | ||
<Option | ||
key={value} | ||
{...props} | ||
label={label.name} | ||
avatar={ | ||
<RoomAvatar | ||
size={Options.AvatarSize} | ||
room={{ | ||
type: label.type, | ||
_id: value, | ||
avatarETag: label.avatarETag, | ||
}} | ||
{...props} | ||
/> | ||
} | ||
/> | ||
)} | ||
options={options} | ||
/> | ||
); | ||
}; | ||
|
||
export default memo(ChannelsSelectElement); |
117 changes: 117 additions & 0 deletions
117
...es/fuselage-ui-kit/src/elements/ChannelsSelectElement/MultiChannelsSelectElement.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { MockedServerContext } from '@rocket.chat/mock-providers'; | ||
import type { MultiChannelsSelectElement as MultiChannelsSelectElementType } from '@rocket.chat/ui-kit'; | ||
import { BlockContext } from '@rocket.chat/ui-kit'; | ||
import { render, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
import { contextualBarParser } from '../../surfaces'; | ||
import MultiChannelsSelectElement from './MultiChannelsSelectElement'; | ||
import { useChannelsData } from './hooks/useChannelsData'; | ||
|
||
const channelsBlock: MultiChannelsSelectElementType = { | ||
type: 'multi_channels_select', | ||
appId: 'test', | ||
blockId: 'test', | ||
actionId: 'test', | ||
}; | ||
|
||
jest.mock('./hooks/useChannelsData'); | ||
|
||
const mockedOptions: ReturnType<typeof useChannelsData> = [ | ||
{ | ||
value: 'channel1_id', | ||
label: { | ||
name: 'Channel 1', | ||
avatarETag: 'test', | ||
type: 'c', | ||
}, | ||
}, | ||
{ | ||
value: 'channel2_id', | ||
label: { | ||
name: 'Channel 2', | ||
avatarETag: 'test', | ||
type: 'c', | ||
}, | ||
}, | ||
{ | ||
value: 'channel3_id', | ||
label: { | ||
name: 'Channel 3', | ||
avatarETag: 'test', | ||
type: 'c', | ||
}, | ||
}, | ||
]; | ||
|
||
const mockUseChannelsData = jest.mocked(useChannelsData); | ||
mockUseChannelsData.mockReturnValue(mockedOptions); | ||
|
||
describe('UiKit MultiChannelsSelect Element', () => { | ||
beforeAll(() => { | ||
jest.useFakeTimers(); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.useRealTimers(); | ||
}); | ||
|
||
beforeEach(() => { | ||
render( | ||
<MockedServerContext> | ||
<MultiChannelsSelectElement | ||
index={0} | ||
block={channelsBlock} | ||
context={BlockContext.FORM} | ||
surfaceRenderer={contextualBarParser} | ||
/> | ||
</MockedServerContext> | ||
); | ||
}); | ||
|
||
it('should render a UiKit multiple channels selector', async () => { | ||
expect(await screen.findByRole('textbox')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should open the channels selector', async () => { | ||
const input = await screen.findByRole('textbox'); | ||
input.focus(); | ||
|
||
expect(await screen.findByRole('listbox')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should select channels', async () => { | ||
const input = await screen.findByRole('textbox'); | ||
|
||
input.focus(); | ||
|
||
const option1 = (await screen.findAllByRole('option'))[0]; | ||
await userEvent.click(option1, { delay: null }); | ||
|
||
const option2 = (await screen.findAllByRole('option'))[2]; | ||
await userEvent.click(option2, { delay: null }); | ||
|
||
const selected = await screen.findAllByRole('button'); | ||
expect(selected[0]).toHaveValue('channel1_id'); | ||
expect(selected[1]).toHaveValue('channel3_id'); | ||
}); | ||
|
||
it('should remove a selected channel', async () => { | ||
const input = await screen.findByRole('textbox'); | ||
|
||
input.focus(); | ||
|
||
const option1 = (await screen.findAllByRole('option'))[0]; | ||
await userEvent.click(option1, { delay: null }); | ||
|
||
const option2 = (await screen.findAllByRole('option'))[2]; | ||
await userEvent.click(option2, { delay: null }); | ||
|
||
const selected1 = (await screen.findAllByRole('button'))[0]; | ||
expect(selected1).toHaveValue('channel1_id'); | ||
await userEvent.click(selected1, { delay: null }); | ||
|
||
const remainingSelected = (await screen.findAllByRole('button'))[0]; | ||
expect(remainingSelected).toHaveValue('channel3_id'); | ||
}); | ||
}); |
Oops, something went wrong.