Skip to content

Commit

Permalink
[Chat] Add autoFocus API to RichTextSendBox (#4435)
Browse files Browse the repository at this point in the history
* Add autoFocus API to RichTextSendBox

* Update api view

* Change files
  • Loading branch information
Leah-Xia-Microsoft authored Apr 9, 2024
1 parent e9f6662 commit ded48cf
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"type": "none",
"area": "improvement",
"workstream": "RTE",
"comment": "Add autoFocus API to RichTextSendBox",
"packageName": "@azure/communication-react",
"email": "[email protected]",
"dependentChangeType": "none"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3883,6 +3883,7 @@ export const RichTextSendBox: (props: RichTextSendBoxProps) => JSX.Element;
// @beta
export interface RichTextSendBoxProps {
activeAttachmentUploads?: AttachmentMetadata[];
autoFocus?: 'sendBoxTextField';
disabled?: boolean;
onCancelAttachmentUpload?: (attachmentId: string) => void;
onSendMessage: (content: string) => Promise<void>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export interface RichTextEditorProps {
strings: Partial<RichTextSendBoxStrings>;
showRichTextEditorFormatting: boolean;
styles: RichTextEditorStyleProps;
autoFocus?: 'sendBoxTextField';
}

/**
Expand All @@ -71,7 +72,8 @@ export interface RichTextEditorComponentRef {
* @beta
*/
export const RichTextEditor = React.forwardRef<RichTextEditorComponentRef, RichTextEditorProps>((props, ref) => {
const { initialContent, onChange, placeholderText, strings, showRichTextEditorFormatting, content } = props;
const { initialContent, onChange, placeholderText, strings, showRichTextEditorFormatting, content, autoFocus } =
props;
const editor = useRef<IEditor | null>(null);
const contentValue = useRef<string | undefined>(content);
const theme = useTheme();
Expand Down Expand Up @@ -202,6 +204,7 @@ export const RichTextEditor = React.forwardRef<RichTextEditorComponentRef, RichT
data-testid={'rooster-rich-text-editor'}
// if we don't use 'allowKeyboardEventPropagation' only the enter key is caught
onKeyDown={props.onKeyDown}
focusOnInit={autoFocus === 'sendBoxTextField'}
/>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export interface RichTextInputBoxComponentProps {
// otherwise the editor will grow to fit the content
richTextEditorStyleProps: (isExpanded: boolean) => RichTextEditorStyleProps;
isHorizontalLayoutDisabled?: boolean;
autoFocus?: 'sendBoxTextField';
}

/**
Expand All @@ -71,7 +72,8 @@ export const RichTextInputBoxComponent = (props: RichTextInputBoxComponentProps)
hasAttachments,
richTextEditorStyleProps,
isHorizontalLayoutDisabled = false,
content
content,
autoFocus
} = props;
const theme = useTheme();
const [showRichTextEditorFormatting, setShowRichTextEditorFormatting] = useState(false);
Expand Down Expand Up @@ -175,6 +177,7 @@ export const RichTextInputBoxComponent = (props: RichTextInputBoxComponentProps)
strings={strings}
showRichTextEditorFormatting={showRichTextEditorFormatting}
styles={richTextEditorStyle}
autoFocus={autoFocus}
/>
</Stack.Item>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ export interface RichTextSendBoxProps {
* @beta
*/
activeAttachmentUploads?: AttachmentMetadata[];
/**
* enumerable to determine if the input box has focus on render or not.
* When undefined nothing has focus on render
*/
autoFocus?: 'sendBoxTextField';
/* @conditional-compile-remove(attachment-upload) */
/**
* Optional callback to remove the attachment upload before sending by clicking on
Expand All @@ -158,6 +163,7 @@ export const RichTextSendBox = (props: RichTextSendBoxProps): JSX.Element => {
const {
disabled = false,
systemMessage,
autoFocus,
onSendMessage,
/* @conditional-compile-remove(attachment-upload) */
activeAttachmentUploads,
Expand Down Expand Up @@ -348,6 +354,7 @@ export const RichTextSendBox = (props: RichTextSendBoxProps): JSX.Element => {
// setting the content will ensure that the latest content is used when editor is re-rendered
content={contentValue}
placeholderText={strings.placeholderText}
autoFocus={autoFocus}
onChange={setContent}
onEnterKeyDown={sendMessageOnClick}
editorComponentRef={editorComponentRef}
Expand Down

0 comments on commit ded48cf

Please sign in to comment.