-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add CustomMessageActionsList to ComponentContext (#2226)
### 🎯 Goal Adds CustomMessageActionsList to ComponentContext for easier message actions adjustment.
- Loading branch information
1 parent
61213d7
commit 2c6e56c
Showing
8 changed files
with
179 additions
and
51 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
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
52 changes: 52 additions & 0 deletions
52
src/components/MessageActions/CustomMessageActionsList.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,52 @@ | ||
import React from 'react'; | ||
|
||
import { CustomMessageActions } from '../../context/MessageContext'; | ||
|
||
import type { StreamMessage } from '../../context/ChannelStateContext'; | ||
import type { DefaultStreamChatGenerics } from '../../types/types'; | ||
|
||
export type CustomMessageActionsListProps< | ||
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics | ||
> = { | ||
message: StreamMessage<StreamChatGenerics>; | ||
customMessageActions?: CustomMessageActions<StreamChatGenerics>; | ||
}; | ||
|
||
/** | ||
* @deprecated alias for `CustomMessageActionsListProps`, will be removed in the next major release | ||
*/ | ||
export type CustomMessageActionsType< | ||
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics | ||
> = CustomMessageActionsListProps<StreamChatGenerics>; | ||
|
||
export const CustomMessageActionsList = < | ||
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics | ||
>( | ||
props: CustomMessageActionsListProps<StreamChatGenerics>, | ||
) => { | ||
const { customMessageActions, message } = props; | ||
|
||
if (!customMessageActions) return null; | ||
|
||
const customActionsArray = Object.keys(customMessageActions); | ||
|
||
return ( | ||
<> | ||
{customActionsArray.map((customAction) => { | ||
const customHandler = customMessageActions[customAction]; | ||
|
||
return ( | ||
<button | ||
aria-selected='false' | ||
className='str-chat__message-actions-list-item str-chat__message-actions-list-item-button' | ||
key={customAction} | ||
onClick={(event) => customHandler(message, event)} | ||
role='option' | ||
> | ||
{customAction} | ||
</button> | ||
); | ||
})} | ||
</> | ||
); | ||
}; |
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
63 changes: 63 additions & 0 deletions
63
src/components/MessageActions/__tests__/CustomMessageActionsList.test.js
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,63 @@ | ||
import React from 'react'; | ||
import { fireEvent, render } from '@testing-library/react'; | ||
import renderer from 'react-test-renderer'; | ||
import { CustomMessageActionsList } from '../CustomMessageActionsList'; | ||
import { act } from 'react-dom/test-utils'; | ||
|
||
describe('CustomMessageActionsList', () => { | ||
it('should render custom list of actions', () => { | ||
const message = { id: 'mId' }; | ||
|
||
const actions = { | ||
key0: () => {}, | ||
key1: () => {}, | ||
}; | ||
|
||
const tree = renderer.create( | ||
<CustomMessageActionsList customMessageActions={actions} message={message} />, | ||
); | ||
|
||
expect(tree.toJSON()).toMatchInlineSnapshot(` | ||
Array [ | ||
<button | ||
aria-selected="false" | ||
className="str-chat__message-actions-list-item str-chat__message-actions-list-item-button" | ||
onClick={[Function]} | ||
role="option" | ||
> | ||
key0 | ||
</button>, | ||
<button | ||
aria-selected="false" | ||
className="str-chat__message-actions-list-item str-chat__message-actions-list-item-button" | ||
onClick={[Function]} | ||
role="option" | ||
> | ||
key1 | ||
</button>, | ||
] | ||
`); | ||
}); | ||
|
||
it('should allow clicking custom action', () => { | ||
const message = { id: 'mId' }; | ||
|
||
const actions = { | ||
key0: jest.fn(), | ||
}; | ||
|
||
const { getByText } = render( | ||
<CustomMessageActionsList customMessageActions={actions} message={message} />, | ||
); | ||
|
||
const button = getByText('key0'); | ||
|
||
const event = new Event('click', { bubbles: true }); | ||
|
||
act(() => { | ||
fireEvent(button, event); | ||
}); | ||
|
||
expect(actions.key0).toHaveBeenCalledWith(message, expect.any(Object)); // replacing SyntheticEvent with any(Object) | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './MessageActions'; | ||
export * from './MessageActionsBox'; | ||
export * from './CustomMessageActionsList'; |
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