Skip to content

Commit 815d260

Browse files
feat(Message): Allow for no avatar (#752)
1 parent e15e279 commit 815d260

File tree

4 files changed

+15
-7
lines changed

4 files changed

+15
-7
lines changed

packages/module/patternfly-docs/content/extensions/chatbot/examples/Messages/BotMessage.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ _Italic text, formatted with single underscores_
318318
content="This bot has a square avatar. You can further customize the avatar by applying an additional class or passing [PatternFly avatar props](/components/avatar) to the `<Message>` component via `avatarProps`."
319319
hasRoundAvatar={false}
320320
/>
321+
<Message name="Bot" role="bot" content="This is a message from a bot with no avatar." />
321322
<Select
322323
id="single-select"
323324
isOpen={isOpen}

packages/module/patternfly-docs/content/extensions/chatbot/examples/Messages/UserMessage.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ _Italic text, formatted with single underscores_
333333
avatar={userAvatar}
334334
avatarProps={{ isBordered: true }}
335335
/>
336+
<Message name="User" role="user" content="This is a user message with no avatar" />
336337
<Message
337338
name="User"
338339
role="user"

packages/module/src/Message/Message.test.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@ describe('Message', () => {
228228
render(<Message avatar="./testImg" role="bot" name="Bot" content="Hi" />);
229229
expect(screen.getByRole('img')).toHaveAttribute('src', './testImg');
230230
});
231+
it('should not render avatar if no avatar prop is passed', () => {
232+
render(<Message role="bot" name="Bot" content="Hi" />);
233+
expect(screen.queryByRole('img')).not.toBeInTheDocument();
234+
});
231235
it('should render botWord correctly', () => {
232236
render(<Message avatar="./img" role="bot" name="Bot" content="Hi" botWord="人工知能" />);
233237
expect(screen.getByText('Bot')).toBeTruthy();

packages/module/src/Message/Message.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export interface MessageProps extends Omit<HTMLProps<HTMLDivElement>, 'role'> {
9797
/** Name of the user */
9898
name?: string;
9999
/** Avatar src for the user */
100-
avatar: string;
100+
avatar?: string;
101101
/** Timestamp for the message */
102102
timestamp?: string;
103103
/** Set this to true if message is being loaded */
@@ -459,12 +459,14 @@ export const MessageBase: FunctionComponent<MessageProps> = ({
459459
{...props}
460460
>
461461
{/* We are using an empty alt tag intentionally in order to reduce noise on screen readers */}
462-
<Avatar
463-
className={`pf-chatbot__message-avatar ${hasRoundAvatar ? 'pf-chatbot__message-avatar--round' : ''} ${avatarClassName ? avatarClassName : ''}`}
464-
src={avatar}
465-
alt=""
466-
{...avatarProps}
467-
/>
462+
{avatar && (
463+
<Avatar
464+
className={`pf-chatbot__message-avatar ${hasRoundAvatar ? 'pf-chatbot__message-avatar--round' : ''} ${avatarClassName ? avatarClassName : ''}`}
465+
src={avatar}
466+
alt=""
467+
{...avatarProps}
468+
/>
469+
)}
468470
<div className="pf-chatbot__message-contents">
469471
<div className="pf-chatbot__message-meta">
470472
{name && (

0 commit comments

Comments
 (0)