Agent questions

Slant / People & chat

Questions an agent asks before it goes on: choices with one recommended, an answer of your own, and submit.

Demo

Usage

@tuttn/slant
import { AgentQuestions } from '@tuttn/slant'

// Uncontrolled: each question starts on its recommended choice.
<AgentQuestions title="Before I start" onSubmit={send} onSkip={skip} questions={[
  { id: 'scope', question: 'Which tests should I run?', options: [
    { value: 'changed', label: 'Only the changed files', recommended: true },
    { value: 'all', label: 'The whole suite', description: 'About four minutes' },
  ], other: true },
  { id: 'browsers', question: 'Which browsers?', multiple: true, required: false, options: [
    { value: 'chromium', label: 'Chromium' }, { value: 'webkit', label: 'WebKit' },
  ] },
]} />
// send({ scope: { selected: ['changed'] }, browsers: { selected: [] } })
// Other: send({ scope: { selected: [], other: 'Only the cart tests' }, … })

// Controlled answers and sent state, answered only in words.
const [answers, setAnswers] = useState<AgentAnswers>({})
const [sent, setSent] = useState(false)
<AgentQuestions value={answers} onValueChange={setAnswers} submitted={sent} onSubmittedChange={setSent}
  onSubmit={send} heading="h4"
  questions={[{ id: 'goal', question: 'What should the summary focus on?', options: [], other: true }]} />

Props

PropTypeDefaultDescription
questionsreadonly AgentQuestion[]—The questions, in order.
titleReactNodemessages.questions ("Questions")The heading over the questions.
valueAgentAnswers—The answers so far, for a controlled form.
defaultValueAgentAnswerseach question's recommended choicesThe answers it starts with when uncontrolled.
onValueChange(answers: AgentAnswers) => void—Called with every answer each time one changes.
onSubmit(answers: AgentAnswers) => void—Called with the answers when they are sent and every required question is answered.
onSkip() => void—Adds a Skip button, which calls this.
submittedboolean—Whether the answers have been sent: the form becomes a read-only summary. Pass it with onSubmittedChange to control it.
defaultSubmittedbooleanfalseWhether it starts sent, when uncontrolled.
onSubmittedChange(submitted: boolean) => void—Called with true when the answers are sent.
heading'h3' | 'h4' | 'div''div'The element wrapping the title, so the questions sit in the page's outline.
AgentQuestion{ id: string; question: ReactNode; description?: ReactNode; options: AgentQuestionOption[]; multiple?: boolean; other?: boolean | { label?: string; placeholder?: string }; required?: boolean }—One question. required defaults to true; options may be empty with other, for an answer only in words.
AgentQuestionOption{ value: string; label: ReactNode; description?: ReactNode; recommended?: boolean; disabled?: boolean }—One choice. A recommended one carries the Recommended badge and, uncontrolled, starts chosen.
AgentAnswersRecord<string, { selected: string[]; other?: string }>—The answers, keyed by question id. other is present only while Other is chosen; in a single question it replaces the choice.
…restFormHTMLAttributes<HTMLFormElement>—className, style, the ref and other attributes go on the root <form>, which carries data-submitted once sent.

Keyboard

Tabmoves from question to question, into an Other field, and to Skip and Send answers
↑ ↓ ← →choose among one question’s radios; choosing Other moves focus into its field
Spacechecks or unchecks a box in a question with several answers
Enterin an Other field, sends the answers, as in any form

Accessibility

  • The root is a form named by the title. Each question is a fieldset whose legend holds its number (messages.questionNumber, “Question 1 of 3”, shown with two or more questions) and the question; the choices inside are a radio group or a group of checkboxes named by the question and described by its description.
  • A recommended choice’s badge (messages.questionRecommended) is part of its label, so it is read with the choice: “Only the changed files Recommended”.
  • Other is the last choice, named messages.questionOther or other.label. Its field is labelled by that choice, with messages.questionOtherPlaceholder or other.placeholder as a placeholder; a question with no options has the field alone, labelled by the question.
  • Sending with something missing shows the error under its question, in the group’s aria-describedby: messages.questionRequired on the group (with aria-invalid), or messages.questionOtherRequired on the Other field. messages.questionsIncomplete(count) is announced, and focus goes to the first invalid question’s first control (its Other field, for an empty Other).
  • Sent, the form is a summary: each question over its answer, messages.questionNoAnswer for an empty optional one, and messages.answersSent as a status line, which is announced. Focus moves to the summary. A form that mounts sent says nothing.

Answers

Each answer is { selected, other? }: the chosen values, and the reader’s own words while Other is chosen. Uncontrolled, every question starts on its recommended choices (the first, for a single choice), unless defaultValue says otherwise. Pass value and onValueChange to hold them yourself. In a single question Other replaces the other choices, and typing in its field chooses it; choosing another option and coming back to Other brings the words back.

Sending

Send answers checks every question. A required one (the default) needs a choice, and Other needs words. Errors show from the first attempt and clear as soon as their question is answered. With nothing missing, onSubmit receives every question’s answer, the words trimmed, and the form turns into a read-only summary. Control that with submitted and onSubmittedChange to keep the form open until your request succeeds. onSkip adds a Skip button and leaves what follows to you.

In a conversation

Put it inside the assistant’s Message. The choices are RadioGroup and CheckboxGroup cards, so they look and behave as everywhere else in Slant, and mirror under dir="rtl".