Skip to content

Commit 953a919

Browse files
committed
Removed actions when isEditable is true
1 parent 5af589d commit 953a919

3 files changed

Lines changed: 42 additions & 5 deletions

File tree

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,27 @@ export const UserMessageExample: FunctionComponent = () => {
1515
const messageInputRef = useRef<HTMLTextAreaElement>(null);
1616
const editButtonRef = useRef<HTMLButtonElement>(null);
1717
const [variant, setVariant] = useState<string | number | undefined>('Code');
18-
const [isEditable, setIsEditable] = useState<boolean>(false);
1918
const [isOpen, setIsOpen] = useState<boolean>(false);
2019
const [selected, setSelected] = useState<string>('Message content type');
2120
const [isExpandable, setIsExpanded] = useState(false);
2221

22+
const [isEditable, setIsEditable] = useState<boolean>(false);
23+
const prevIsEditable = useRef<boolean>(false);
24+
2325
useEffect(() => {
2426
if (isEditable && messageInputRef?.current) {
2527
messageInputRef.current.focus();
2628
const messageLength = messageInputRef.current.value.length;
2729
// Mimic the behavior of the textarea when the user clicks on a label to place the cursor at the end of the input value
2830
messageInputRef.current.setSelectionRange(messageLength, messageLength);
2931
}
32+
33+
// We only want to re-focus the edit action button if the user has previously clicked on it,
34+
// and prevent it from receiving focus on page load
35+
if (prevIsEditable.current && !isEditable && editButtonRef?.current) {
36+
editButtonRef.current.focus();
37+
prevIsEditable.current = false;
38+
}
3039
}, [isEditable]);
3140

3241
/* eslint-disable indent */
@@ -192,10 +201,8 @@ _Italic text, formatted with single underscores_
192201
};
193202

194203
const onUpdateOrCancelEdit = () => {
204+
prevIsEditable.current = isEditable;
195205
setIsEditable(false);
196-
if (editButtonRef?.current) {
197-
editButtonRef.current.focus();
198-
}
199206
};
200207

201208
const toggle = (toggleRef: Ref<MenuToggleElement>) => (

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,36 @@ describe('Message', () => {
472472
expect(screen.queryByRole('button', { name: label })).toBeFalsy();
473473
});
474474
});
475+
it('should not show actions if isEditable is true', async () => {
476+
render(
477+
<Message
478+
avatar="./img"
479+
role="bot"
480+
name="Bot"
481+
content="Hi"
482+
isEditable
483+
actions={{
484+
// eslint-disable-next-line no-console
485+
positive: { onClick: () => console.log('Good response') },
486+
// eslint-disable-next-line no-console
487+
negative: { onClick: () => console.log('Bad response') },
488+
// eslint-disable-next-line no-console
489+
copy: { onClick: () => console.log('Copy') },
490+
// eslint-disable-next-line no-console
491+
edit: { onClick: () => console.log('Edit') },
492+
// eslint-disable-next-line no-console
493+
share: { onClick: () => console.log('Share') },
494+
// eslint-disable-next-line no-console
495+
download: { onClick: () => console.log('Download') },
496+
// eslint-disable-next-line no-console
497+
listen: { onClick: () => console.log('Listen') }
498+
}}
499+
/>
500+
);
501+
ALL_ACTIONS.forEach(({ label }) => {
502+
expect(screen.queryByRole('button', { name: label })).toBeFalsy();
503+
});
504+
});
475505
it('should render unordered lists correctly', () => {
476506
render(<Message avatar="./img" role="user" name="User" content={UNORDERED_LIST} />);
477507
expect(screen.getByText('Here is an unordered list:')).toBeTruthy();

packages/module/src/Message/Message.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ export const MessageBase: FunctionComponent<MessageProps> = ({
373373
isCompact={isCompact}
374374
/>
375375
)}
376-
{!isLoading && actions && <ResponseActions actions={actions} />}
376+
{!isLoading && !isEditable && actions && <ResponseActions actions={actions} />}
377377
{userFeedbackForm && <UserFeedback {...userFeedbackForm} timestamp={dateString} isCompact={isCompact} />}
378378
{userFeedbackComplete && (
379379
<UserFeedbackComplete {...userFeedbackComplete} timestamp={dateString} isCompact={isCompact} />

0 commit comments

Comments
 (0)