import type { OpenTextQuestion } from "@formbricks/types/questions"; import { useEffect, useState } from "react"; import Headline from "./Headline"; import Subheader from "./Subheader"; import SubmitButton from "@/components/preview/SubmitButton"; import { Response } from "@formbricks/types/js"; import { BackButton } from "@/components/preview/BackButton"; import { TSurveyOpenTextQuestion } from "@formbricks/types/v1/surveys"; interface OpenTextQuestionProps { question: TSurveyOpenTextQuestion; onSubmit: (data: { [x: string]: any }) => void; lastQuestion: boolean; brandColor: string; storedResponseValue: string | null; goToNextQuestion: (answer: Response["data"]) => void; goToPreviousQuestion?: (answer: Response["data"]) => void; autoFocus?: boolean; } export default function OpenTextQuestion({ question, onSubmit, lastQuestion, brandColor, storedResponseValue, goToNextQuestion, goToPreviousQuestion, autoFocus = false, }: OpenTextQuestionProps) { const [value, setValue] = useState(""); useEffect(() => { setValue(storedResponseValue ?? ""); }, [storedResponseValue, question.id, question.longAnswer]); const handleSubmit = (value: string) => { const data = { [question.id]: value, }; if (storedResponseValue === value) { goToNextQuestion(data); return; } onSubmit(data); setValue(""); // reset value }; return (
{ e.preventDefault(); handleSubmit(value); }}>
{question.longAnswer === false ? ( setValue(e.target.value)} placeholder={!storedResponseValue ? question.placeholder : undefined} required={question.required} className="block w-full rounded-md border border-slate-100 bg-slate-50 p-2 shadow-sm focus:border-slate-500 focus:outline-none focus:ring-0 sm:text-sm" /> ) : (