Skip to content

Commit 1161615

Browse files
committed
feat(Message): Allow for no avatar
1 parent bb159de commit 1161615

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
@@ -90,6 +90,7 @@ export default MessageLoading;
9090
content="Text-based message from a bot with 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`."
9191
hasRoundAvatar={false}
9292
/>
93+
<Message name="Bot" role="bot" content="Text-based message from a bot with no avatar." />
9394
</>
9495
);
9596
};

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
@@ -39,6 +39,7 @@ Here is an unordered list:
3939
avatar={userAvatar}
4040
avatarProps={{ isBordered: true }}
4141
/>
42+
<Message name="User" role="user" content="Example user message with no avatar" />
4243
</>
4344
);
4445
};

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ describe('Message', () => {
9898
render(<Message avatar="./testImg" role="bot" name="Bot" content="Hi" />);
9999
expect(screen.getByRole('img')).toHaveAttribute('src', './testImg');
100100
});
101+
it('should not render avatar if no avatar prop is passed', () => {
102+
render(<Message role="bot" name="Bot" content="Hi" />);
103+
expect(screen.queryByRole('img')).not.toBeInTheDocument();
104+
});
101105
it('should render botWord correctly', () => {
102106
render(<Message avatar="./img" role="bot" name="Bot" content="Hi" botWord="人工知能" />);
103107
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
@@ -60,7 +60,7 @@ export interface MessageProps extends Omit<React.HTMLProps<HTMLDivElement>, 'rol
6060
/** Name of the user */
6161
name?: string;
6262
/** Avatar src for the user */
63-
avatar: string;
63+
avatar?: string;
6464
/** Timestamp for the message */
6565
timestamp?: string;
6666
/** Set this to true if message is being loaded */
@@ -126,12 +126,14 @@ export const Message: React.FunctionComponent<MessageProps> = ({
126126
{...props}
127127
>
128128
{/* We are using an empty alt tag intentionally in order to reduce noise on screen readers */}
129-
<Avatar
130-
className={`pf-chatbot__message-avatar ${hasRoundAvatar ? 'pf-chatbot__message-avatar--round' : ''} ${avatarClassName ? avatarClassName : ''}`}
131-
src={avatar}
132-
alt=""
133-
{...avatarProps}
134-
/>
129+
{avatar && (
130+
<Avatar
131+
className={`pf-chatbot__message-avatar ${hasRoundAvatar ? 'pf-chatbot__message-avatar--round' : ''} ${avatarClassName ? avatarClassName : ''}`}
132+
src={avatar}
133+
alt=""
134+
{...avatarProps}
135+
/>
136+
)}
135137
<div className="pf-chatbot__message-contents">
136138
<div className="pf-chatbot__message-meta">
137139
{name && (

0 commit comments

Comments
 (0)