// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 import React, { FC, HTMLAttributes, useEffect, useRef, useState } from 'react'; import { BaseProps } from '../../Base'; import PrimaryButton from '../../Button/PrimaryButton'; import SecondaryButton from '../../Button/SecondaryButton'; import Input from '../../Input'; import { MessageVariant } from './ChatBubbleContainer'; import { StyledChatBubble } from './Styled'; export interface EditableChatBubbleProps extends Omit, 'css'>, BaseProps { /** Determines styling for outgoing and incoming messages. */ variant: MessageVariant; /** The name of the user that sent the message. */ senderName: string; /** The text content of the message. */ content: string; /** Determines if the name should be shown or not. */ showName?: boolean; /** Adds the bubble tail style to a message. */ showTail?: boolean; /** A callback function to edit the message's content. */ save: (e: any, newContent: string) => void; /** The button label for saving an edit. */ saveLabel?: string; /** A callback function that changes the UI to allow the user to edit the content of the message */ cancel: (e: any) => void; /** The button label for canceling an edit. */ cancelLabel?: string; } export const EditableChatBubble: FC< React.PropsWithChildren > = (props) => { const { showName = true, variant, senderName, content, showTail, cancel, cancelLabel = 'Cancel', save, saveLabel = 'Save', ...rest } = props; const [text, setText] = useState(content); const inputEl = useRef(null); useEffect(() => { if (inputEl && inputEl.current) { inputEl.current.focus(); } }, []); const handleChange = (e: any) => { e.preventDefault(); setText(e.target.value); }; return ( {showName &&
{senderName}
}
save(e, text)}>
save(e, text)} />
{showTail && ( )}
); }; export default EditableChatBubble;