Skip to content

Commit

Permalink
feat (ui): make event in handleSubmit optional (#2073)
Browse files Browse the repository at this point in the history
  • Loading branch information
lgrammel committed Jun 24, 2024
1 parent 5be9e7e commit d42b890
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 42 deletions.
9 changes: 9 additions & 0 deletions .changeset/brave-nails-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@ai-sdk/svelte': patch
'@ai-sdk/react': patch
'@ai-sdk/solid': patch
'ai': patch
'@ai-sdk/vue': patch
---

feat (ui): make event in handleSubmit optional
2 changes: 1 addition & 1 deletion content/docs/07-reference/ai-sdk-ui/01-use-chat.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ Allows you to easily create a conversational user interface for your chatbot app
},
{
name: 'handleSubmit',
type: '(event: React.FormEvent<HTMLFormElement>, chatRequestOptions?: ChatRequestOptions) => void',
type: '(event?: { preventDefault?: () => void }, chatRequestOptions?: ChatRequestOptions) => void',
description:
'Form submission handler that automatically resets the input field and appends a user message. You can use the `options` parameter to send additional data, headers and more to the server.',
properties: [
Expand Down
2 changes: 1 addition & 1 deletion content/docs/07-reference/ai-sdk-ui/02-use-completion.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ Allows you to create text completion based capabilities for your application. It
},
{
name: 'handleSubmit',
type: '(event: React.FormEvent<HTMLFormElement>) => void',
type: '(event?: { preventDefault?: () => void }) => void',
description:
'Form submission handler that automatically resets the input field and appends a user message.',
},
Expand Down
15 changes: 12 additions & 3 deletions packages/core/svelte/use-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,13 @@ export type UseChatHelpers = {
setMessages: (messages: Message[]) => void;
/** The current value of the input */
input: Writable<string>;

/** Form submission handler to automatically reset input and append a user message */
handleSubmit: (e: any, chatRequestOptions?: ChatRequestOptions) => void;
handleSubmit: (
event?: { preventDefault?: () => void },
chatRequestOptions?: ChatRequestOptions,
) => void;

metadata?: Object;
/** Whether the API request is in progress */
isLoading: Readable<boolean | undefined>;
Expand Down Expand Up @@ -341,8 +346,12 @@ export function useChat({

const input = writable(initialInput);

const handleSubmit = (e: any, options: ChatRequestOptions = {}) => {
e.preventDefault();
const handleSubmit = (
event?: { preventDefault?: () => void },
options: ChatRequestOptions = {},
) => {
event?.preventDefault?.();

const inputValue = get(input);
if (!inputValue) return;

Expand Down
12 changes: 7 additions & 5 deletions packages/core/svelte/use-completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export type UseCompletionHelpers = {
setCompletion: (completion: string) => void;
/** The current value of the input */
input: Writable<string>;

/**
* Form submission handler to automatically reset input and append a user message
* @example
Expand All @@ -40,7 +41,8 @@ export type UseCompletionHelpers = {
* </form>
* ```
*/
handleSubmit: (e: any) => void;
handleSubmit: (event?: { preventDefault?: () => void }) => void;

/** Whether the API request is in progress */
isLoading: Readable<boolean | undefined>;

Expand Down Expand Up @@ -146,11 +148,11 @@ export function useCompletion({

const input = writable(initialInput);

const handleSubmit = (e: any) => {
e.preventDefault();
const handleSubmit = (event?: { preventDefault?: () => void }) => {
event?.preventDefault?.();

const inputValue = get(input);
if (!inputValue) return;
return complete(inputValue);
return inputValue ? complete(inputValue) : undefined;
};

const isLoading = derived(
Expand Down
7 changes: 4 additions & 3 deletions packages/react/src/use-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export type UseChatHelpers = {
) => void;
/** Form submission handler to automatically reset input and append a user message */
handleSubmit: (
e: React.FormEvent<HTMLFormElement>,
event?: { preventDefault?: () => void },
chatRequestOptions?: ChatRequestOptions,
) => void;
metadata?: Object;
Expand Down Expand Up @@ -465,7 +465,7 @@ By default, it's set to 0, which will disable the feature.

const handleSubmit = useCallback(
(
e: React.FormEvent<HTMLFormElement>,
event?: { preventDefault?: () => void },
options: ChatRequestOptions = {},
metadata?: Object,
) => {
Expand All @@ -476,7 +476,8 @@ By default, it's set to 0, which will disable the feature.
};
}

e.preventDefault();
event?.preventDefault?.();

if (!input) return;

append(
Expand Down
13 changes: 7 additions & 6 deletions packages/react/src/use-completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ export type UseCompletionHelpers = {
* ```
*/
handleInputChange: (
e:
event:
| React.ChangeEvent<HTMLInputElement>
| React.ChangeEvent<HTMLTextAreaElement>,
) => void;

/**
* Form submission handler to automatically reset input and append a user message
* @example
Expand All @@ -54,7 +55,8 @@ export type UseCompletionHelpers = {
* </form>
* ```
*/
handleSubmit: (e: React.FormEvent<HTMLFormElement>) => void;
handleSubmit: (event?: { preventDefault?: () => void }) => void;

/** Whether the API request is in progress */
isLoading: boolean;
/** Additional data added on the server via StreamData */
Expand Down Expand Up @@ -175,10 +177,9 @@ export function useCompletion({
const [input, setInput] = useState(initialInput);

const handleSubmit = useCallback(
(e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
if (!input) return;
return complete(input);
(event?: { preventDefault?: () => void }) => {
event?.preventDefault?.();
return input ? complete(input) : undefined;
},
[input, complete],
);
Expand Down
12 changes: 9 additions & 3 deletions packages/solid/src/use-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ export type UseChatHelpers = {
/** Signal setter to update the input value */
setInput: Setter<string>;
/** Form submission handler to automatically reset input and append a user message */
handleSubmit: (e: any, chatRequestOptions?: ChatRequestOptions) => void;
handleSubmit: (
event?: { preventDefault?: () => void },
chatRequestOptions?: ChatRequestOptions,
) => void;
/** Whether the API request is in progress */
isLoading: Accessor<boolean>;
/** Additional data added on the server via StreamData */
Expand Down Expand Up @@ -250,8 +253,11 @@ export function useChat({

const [input, setInput] = createSignal(initialInput);

const handleSubmit = (e: any, options: ChatRequestOptions = {}) => {
e.preventDefault();
const handleSubmit = (
event?: { preventDefault?: () => void },
options: ChatRequestOptions = {},
) => {
event?.preventDefault?.();
const inputValue = input();
if (!inputValue) return;

Expand Down
10 changes: 5 additions & 5 deletions packages/solid/src/use-completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export type UseCompletionHelpers = {
* </form>
* ```
*/
handleSubmit: (e: any) => void;
handleSubmit: (event?: { preventDefault?: () => void }) => void;
/** Whether the API request is in progress */
isLoading: Accessor<boolean>;
/** Additional data added on the server via StreamData */
Expand Down Expand Up @@ -145,11 +145,11 @@ export function useCompletion({

const [input, setInput] = createSignal(initialInput);

const handleSubmit = (e: any) => {
e.preventDefault();
const handleSubmit = (event?: { preventDefault?: () => void }) => {
event?.preventDefault?.();

const inputValue = input();
if (!inputValue) return;
return complete(inputValue);
return inputValue ? complete(inputValue) : undefined;
};

return {
Expand Down
12 changes: 9 additions & 3 deletions packages/svelte/src/use-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ export type UseChatHelpers = {
/** The current value of the input */
input: Writable<string>;
/** Form submission handler to automatically reset input and append a user message */
handleSubmit: (e: any, chatRequestOptions?: ChatRequestOptions) => void;
handleSubmit: (
event?: { preventDefault?: () => void },
chatRequestOptions?: ChatRequestOptions,
) => void;
metadata?: Object;
/** Whether the API request is in progress */
isLoading: Readable<boolean | undefined>;
Expand Down Expand Up @@ -338,8 +341,11 @@ export function useChat({

const input = writable(initialInput);

const handleSubmit = (e: any, options: ChatRequestOptions = {}) => {
e.preventDefault();
const handleSubmit = (
event?: { preventDefault?: () => void },
options: ChatRequestOptions = {},
) => {
event?.preventDefault?.();
const inputValue = get(input);
if (!inputValue) return;

Expand Down
10 changes: 5 additions & 5 deletions packages/svelte/src/use-completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export type UseCompletionHelpers = {
* </form>
* ```
*/
handleSubmit: (e: any) => void;
handleSubmit: (event?: { preventDefault?: () => void }) => void;
/** Whether the API request is in progress */
isLoading: Readable<boolean | undefined>;

Expand Down Expand Up @@ -143,11 +143,11 @@ export function useCompletion({

const input = writable(initialInput);

const handleSubmit = (e: any) => {
e.preventDefault();
const handleSubmit = (event?: { preventDefault?: () => void }) => {
event?.preventDefault?.();

const inputValue = get(input);
if (!inputValue) return;
return complete(inputValue);
return inputValue ? complete(inputValue) : undefined;
};

const isLoading = derived(
Expand Down
8 changes: 6 additions & 2 deletions packages/vue/src/use-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,12 @@ export function useChat({

const input = ref(initialInput);

const handleSubmit = (e: any, options: ChatRequestOptions = {}) => {
e.preventDefault();
const handleSubmit = (
event?: { preventDefault?: () => void },
options: ChatRequestOptions = {},
) => {
event?.preventDefault?.();

const inputValue = input.value;
if (!inputValue) return;
append(
Expand Down
9 changes: 4 additions & 5 deletions packages/vue/src/use-completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export type UseCompletionHelpers = {
* </form>
* ```
*/
handleSubmit: (e: any) => void;
handleSubmit: (event?: { preventDefault?: () => void }) => void;
/** Whether the API request is in progress */
isLoading: Ref<boolean | undefined>;

Expand Down Expand Up @@ -155,11 +155,10 @@ export function useCompletion({

const input = ref(initialInput);

const handleSubmit = (e: any) => {
e.preventDefault();
const handleSubmit = (event?: { preventDefault?: () => void }) => {
event?.preventDefault?.();
const inputValue = input.value;
if (!inputValue) return;
return complete(inputValue);
return inputValue ? complete(inputValue) : undefined;
};

return {
Expand Down

0 comments on commit d42b890

Please sign in to comment.