Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

πŸ› fix(shared-tool-ui): keep select-to-submit keyboard-only in AskUserQuestion (#18056)

YuTengjing6b55ea46c53f
diff --git a/packages/shared-tool-ui/src/AskUserQuestion/AskUserQuestionView.tsx b/packages/shared-tool-ui/src/AskUserQuestion/AskUserQuestionView.tsx
index 2d1550cc546..89076039ae1 100644
--- a/packages/shared-tool-ui/src/AskUserQuestion/AskUserQuestionView.tsx
+++ b/packages/shared-tool-ui/src/AskUserQuestion/AskUserQuestionView.tsx
@@ -171,7 +171,7 @@ export const AskUserQuestionView = memo<AskUserQuestionViewProps>((props) => {
if (idx < q.options.length) {
event.preventDefault();
setHighlight(q, idx);
- handleToggle(q, q.options[idx].label);
+ handleToggle(q, q.options[idx].label, { submitOnComplete: true });
} else if (idx === q.options.length) {
event.preventDefault();
setHighlight(q, q.options.length);
@@ -216,7 +216,7 @@ export const AskUserQuestionView = memo<AskUserQuestionViewProps>((props) => {
picks[q.question] !== q.options[highlightedIndex].label
) {
event.preventDefault();
- handleToggle(q, q.options[highlightedIndex].label);
+ handleToggle(q, q.options[highlightedIndex].label, { submitOnComplete: true });
return;
}
if (isSubmitDisabled) return;
diff --git a/packages/shared-tool-ui/src/AskUserQuestion/useAskUserForm.test.ts b/packages/shared-tool-ui/src/AskUserQuestion/useAskUserForm.test.ts
index 91feeca6330..3288c87e7cc 100644
--- a/packages/shared-tool-ui/src/AskUserQuestion/useAskUserForm.test.ts
+++ b/packages/shared-tool-ui/src/AskUserQuestion/useAskUserForm.test.ts
@@ -46,13 +46,37 @@ const setup = (args: AskUserQuestionArgs, persistedDraft?: unknown) => {
};
describe('useAskUserForm select-to-submit', () => {
- it('submits immediately when a single-select pick completes the form', () => {
+ it('submits immediately when a keyboard single-select pick completes the form', () => {
+ const { hook, onInteractionAction } = setup(singleQuestionArgs);
+
+ act(() => {
+ hook.result.current.handleToggle(singleQuestionArgs.questions[0], 'Full', {
+ submitOnComplete: true,
+ });
+ });
+
+ expect(onInteractionAction).toHaveBeenCalledExactlyOnceWith({
+ payload: { 'How broad?': 'Full' },
+ type: 'submit',
+ });
+ });
+
+ it('never submits on a plain (mouse-click) toggle, even when it completes the form', () => {
+ // Regression: clicking an option must only select it β€” accidental clicks
+ // were submitting the whole form when select-to-submit applied to clicks.
const { hook, onInteractionAction } = setup(singleQuestionArgs);
act(() => {
hook.result.current.handleToggle(singleQuestionArgs.questions[0], 'Full');
});
+ expect(onInteractionAction).not.toHaveBeenCalled();
+ expect(hook.result.current.picks['How broad?']).toBe('Full');
+
+ act(() => {
+ hook.result.current.handleSubmit();
+ });
+
expect(onInteractionAction).toHaveBeenCalledExactlyOnceWith({
payload: { 'How broad?': 'Full' },
type: 'submit',
@@ -63,14 +87,18 @@ describe('useAskUserForm select-to-submit', () => {
const { hook, onInteractionAction } = setup(twoQuestionArgs);
act(() => {
- hook.result.current.handleToggle(twoQuestionArgs.questions[0], 'Narrow');
+ hook.result.current.handleToggle(twoQuestionArgs.questions[0], 'Narrow', {
+ submitOnComplete: true,
+ });
});
expect(onInteractionAction).not.toHaveBeenCalled();
expect(hook.result.current.activeTab).toBe('1');
act(() => {
- hook.result.current.handleToggle(twoQuestionArgs.questions[1], 'Auto');
+ hook.result.current.handleToggle(twoQuestionArgs.questions[1], 'Auto', {
+ submitOnComplete: true,
+ });
});
expect(onInteractionAction).toHaveBeenCalledExactlyOnceWith({
@@ -87,7 +115,9 @@ describe('useAskUserForm select-to-submit', () => {
});
act(() => {
- hook.result.current.handleToggle(twoQuestionArgs.questions[0], 'Full');
+ hook.result.current.handleToggle(twoQuestionArgs.questions[0], 'Full', {
+ submitOnComplete: true,
+ });
});
expect(onInteractionAction).not.toHaveBeenCalled();
@@ -108,7 +138,7 @@ describe('useAskUserForm select-to-submit', () => {
const { hook, onInteractionAction } = setup(args);
act(() => {
- hook.result.current.handleToggle(args.questions[0], 'Narrow');
+ hook.result.current.handleToggle(args.questions[0], 'Narrow', { submitOnComplete: true });
});
expect(onInteractionAction).not.toHaveBeenCalled();
diff --git a/packages/shared-tool-ui/src/AskUserQuestion/useAskUserForm.ts b/packages/shared-tool-ui/src/AskUserQuestion/useAskUserForm.ts
index 278b49140fd..189e24295d9 100644
--- a/packages/shared-tool-ui/src/AskUserQuestion/useAskUserForm.ts
+++ b/packages/shared-tool-ui/src/AskUserQuestion/useAskUserForm.ts
@@ -32,7 +32,19 @@ export interface AskUserFormApi {
handleEscapeTextChange: (value: string) => void;
handleSkip: () => void;
handleSubmit: () => void;
- handleToggle: (q: AskUserQuestionItem, label: string) => void;
+ handleToggle: (
+ q: AskUserQuestionItem,
+ label: string,
+ options?: {
+ /**
+ * Allow the single-select "select-to-submit" fast path for this toggle.
+ * Only keyboard-driven picks (digits / Enter) opt in β€” a mouse click is
+ * too easy to land by accident to fire a submit on its own, so clicks
+ * just select and leave submission to the explicit Submit button/Enter.
+ */
+ submitOnComplete?: boolean;
+ },
+ ) => void;
isMulti: boolean;
isSubmitDisabled: boolean;
picks: Record<string, string | string[]>;
@@ -109,7 +121,7 @@ export const useAskUserForm = ({
);
const handleToggle = useCallback(
- (q: AskUserQuestionItem, label: string) => {
+ (q: AskUserQuestionItem, label: string, options?: { submitOnComplete?: boolean }) => {
let nextPicks: Record<string, string | string[]>;
if (q.multiSelect) {
const current = (picks[q.question] as string[] | undefined) ?? [];
@@ -137,13 +149,15 @@ export const useAskUserForm = ({
if (!q.multiSelect) {
// Codex-style select-to-submit: the pick that completes the form sends
- // it right away β€” no extra Submit press for single-select flows. Gated
- // on the question having been unanswered so revisiting an already
- // answered question only updates the pick and never fires a surprise
- // submit while the user is reviewing.
+ // it right away β€” no extra Submit press for single-select flows. Two
+ // gates: the caller must opt in via `submitOnComplete` (keyboard picks
+ // only β€” a stray mouse click must never submit on its own), and the
+ // question must have been unanswered so revisiting an already answered
+ // question only updates the pick and never fires a surprise submit
+ // while the user is reviewing.
const wasUnanswered = !isQuestionAnswered(q, picks, custom);
const allAnswered = questions.every((qq) => isQuestionAnswered(qq, nextPicks, nextCustom));
- if (wasUnanswered && allAnswered) {
+ if (options?.submitOnComplete && wasUnanswered && allAnswered) {
void submitWith(buildSubmitPayload(questions, nextPicks, nextCustom));
return;
}