Skip to content

Can we disable submit button if user enters only spaces? #164

@sehrishnaveed

Description

@sehrishnaveed

Is there a way to disable the "Submit" button if user enters only empty spaces?

There is disableSubmitButton option under ComposerOptions but we need latest prompt text typed in the prompt box.
I have verified that useAsStreamAdapter and useAiChatApi doesn't return prompt text. We can use disableSubmitButton only if current prompt text is available.
So, is there a way to get current prompt text or disable "submit" button if user enters only empty spaces?

Image
const sendMessage = async (prompt, observer, extras) => {
const userPrompt = prompt?.trim();

  try {
    const response = await fetch('api/chat',
      {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
          prompt: userPrompt,
        }),
      },
    );

    if (response.status !== 200) {
      observer.error(new Error('Unable to connect to the API.'));
      return;
    }

    if (!response.body) {
      observer.error(new Error('Empty response body.'));
      return;
    }

    // Read a stream of server-sent events and feed them to the observer as they are being generated
    const reader = response.body.getReader();
    const textDecoder = new TextDecoder();

    /* eslint-disable no-await-in-loop */
    while (true) {
      const { value, done } = await reader.read();
      if (done) {
        observer.complete();
        break;
      }

      const content = textDecoder.decode(value, { stream: true });

      if (content?.trim()) {
        observer.next(content);
      }
    }
  } catch (error) {
    observer.error(error);
  }
};
 const adapter = useAsStreamAdapter(sendMessage, []);
const chatApi = useAiChatApi();

return (
      <AiChat
        api={chatApi}
        adapter={adapter}
        displayOptions={{ colorScheme: 'light', themeId: 'nova' }}
        conversationOptions={{ layout: 'bubbles' }}
        messageOptions={{
          waitTimeBeforeStreamCompletion: 3000,
        }}
        composerOptions={{
          autoFocus: true,
        }}
        events={{
          error: error => {
            // eslint-disable-next-line no-console
            console.log('Error Occurred!', error);
          },
        }}
      />
);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions