Skip to content

Commit

Permalink
isSubmitDisabled prop to disable the submit button
Browse files Browse the repository at this point in the history
  • Loading branch information
jsartisan committed Oct 11, 2024
1 parent 55e2537 commit 90d4504
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import styles from "./styles.module.css";
import { ThreadMessage } from "./ThreadMessage";
import type { AIChatProps, ChatMessage } from "./types";

const MIN_PROMPT_LENGTH = 3;

const _AIChat = (props: AIChatProps, ref: ForwardedRef<HTMLDivElement>) => {
const {
// assistantName,
Expand Down Expand Up @@ -64,6 +66,7 @@ const _AIChat = (props: AIChatProps, ref: ForwardedRef<HTMLDivElement>) => {
>
<ChatInput
isLoading={isWaitingForResponse}
isSubmitDisabled={prompt.length < MIN_PROMPT_LENGTH}
onChange={onPromptChange}
onSubmit={onSubmit}
placeholder={promptInputPlaceholder}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export function ChatInput(props: ChatInputProps) {
isLoading,
isReadOnly,
isRequired,
isSubmitDisabled,
label,
onChange,
onSubmit,
Expand Down Expand Up @@ -91,12 +92,14 @@ export function ChatInput(props: ChatInputProps) {

const handleKeyDown = useCallback(
(event: React.KeyboardEvent<HTMLTextAreaElement>) => {
if (Boolean(isSubmitDisabled)) return;

if (event.key === "Enter" && (event.metaKey || event.ctrlKey)) {
event.preventDefault();
onSubmit?.();
}
},
[onSubmit],
[onSubmit, isSubmitDisabled],
);

useLayoutEffect(() => {
Expand All @@ -112,14 +115,18 @@ export function ChatInput(props: ChatInputProps) {
return (
<IconButton
icon="player-stop-filled"
isDisabled={isDisabled}
isDisabled={Boolean(isDisabled) || Boolean(isSubmitDisabled)}
onPress={onSubmit}
/>
);
}

return (
<IconButton icon="arrow-up" isDisabled={isDisabled} onPress={onSubmit} />
<IconButton
icon="arrow-up"
isDisabled={Boolean(isDisabled) || Boolean(isSubmitDisabled)}
onPress={onSubmit}
/>
);
})();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { TextAreaProps } from "@appsmith/wds";

export interface ChatInputProps extends TextAreaProps {
/** callback function when the user submits the chat input */
onSubmit?: () => void;
/** flag for disable the submit button */
isSubmitDisabled?: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,9 @@ export const Validation: Story = {
</Form>
),
};

export const SubmitDisabled: Story = {
args: {
isSubmitDisabled: true,
},
};

0 comments on commit 90d4504

Please sign in to comment.