From 16cbc3365b9778b325198af1ee0e556762069176 Mon Sep 17 00:00:00 2001 From: Shubham Palriwala Date: Tue, 30 Jan 2024 14:51:01 +0530 Subject: [PATCH] feat: custom placeholder label for other option in single & multi select (#1971) Co-authored-by: Johannes <72809645+jobenjada@users.noreply.github.com> --- .../components/MultipleChoiceMultiForm.tsx | 59 +++++++++++------- .../components/MultipleChoiceSingleForm.tsx | 61 ++++++++++++------- .../questions/MultipleChoiceMultiQuestion.tsx | 2 +- .../MultipleChoiceSingleQuestion.tsx | 2 +- packages/types/surveys.ts | 2 + 5 files changed, 79 insertions(+), 47 deletions(-) diff --git a/apps/web/app/(app)/environments/[environmentId]/surveys/[surveyId]/edit/components/MultipleChoiceMultiForm.tsx b/apps/web/app/(app)/environments/[environmentId]/surveys/[surveyId]/edit/components/MultipleChoiceMultiForm.tsx index c2e868f7a1..bf4913f25b 100644 --- a/apps/web/app/(app)/environments/[environmentId]/surveys/[surveyId]/edit/components/MultipleChoiceMultiForm.tsx +++ b/apps/web/app/(app)/environments/[environmentId]/surveys/[surveyId]/edit/components/MultipleChoiceMultiForm.tsx @@ -217,29 +217,44 @@ export default function MultipleChoiceMultiForm({ {question.choices && question.choices.map((choice, choiceIdx) => (
- updateChoice(choiceIdx, { label: e.target.value })} - onBlur={() => { - const duplicateLabel = findDuplicateLabel(); - if (duplicateLabel) { - setisInvalidValue(duplicateLabel); - } else if (findEmptyLabel()) { - setisInvalidValue(""); - } else { - setisInvalidValue(null); +
+ updateChoice(choiceIdx, { label: e.target.value })} + onBlur={() => { + const duplicateLabel = findDuplicateLabel(); + if (duplicateLabel) { + setisInvalidValue(duplicateLabel); + } else if (findEmptyLabel()) { + setisInvalidValue(""); + } else { + setisInvalidValue(null); + } + }} + isInvalid={ + (isInvalidValue === "" && choice.label.trim() === "") || + (isInvalidValue !== null && choice.label.trim() === isInvalidValue.trim()) } - }} - isInvalid={ - (isInvalidValue === "" && choice.label.trim() === "") || - (isInvalidValue !== null && choice.label.trim() === isInvalidValue.trim()) - } - /> + /> + {choice.id === "other" && ( + { + if (e.target.value.trim() == "") e.target.value = ""; + updateQuestion(questionIdx, { otherOptionPlaceholder: e.target.value }); + }} + /> + )} +
{question.choices && question.choices.length > 2 && ( {question.choices && question.choices.map((choice, choiceIdx) => ( -
- { - const duplicateLabel = findDuplicateLabel(); - if (duplicateLabel) { - setisInvalidValue(duplicateLabel); - } else if (findEmptyLabel()) { - setisInvalidValue(""); - } else { - setisInvalidValue(null); +
+
+ { + const duplicateLabel = findDuplicateLabel(); + if (duplicateLabel) { + setisInvalidValue(duplicateLabel); + } else if (findEmptyLabel()) { + setisInvalidValue(""); + } else { + setisInvalidValue(null); + } + }} + onChange={(e) => updateChoice(choiceIdx, { label: e.target.value })} + isInvalid={ + (isInvalidValue === "" && choice.label.trim() === "") || + (isInvalidValue !== null && choice.label.trim() === isInvalidValue.trim()) } - }} - onChange={(e) => updateChoice(choiceIdx, { label: e.target.value })} - isInvalid={ - (isInvalidValue === "" && choice.label.trim() === "") || - (isInvalidValue !== null && choice.label.trim() === isInvalidValue.trim()) - } - /> + /> + {choice.id === "other" && ( + { + if (e.target.value.trim() == "") e.target.value = ""; + updateQuestion(questionIdx, { otherOptionPlaceholder: e.target.value }); + }} + /> + )} +
{question.choices && question.choices.length > 2 && ( ; @@ -286,6 +287,7 @@ export const ZSurveyMultipleChoiceMultiQuestion = ZSurveyQuestionBase.extend({ choices: z.array(ZSurveyChoice), logic: z.array(ZSurveyMultipleChoiceMultiLogic).optional(), shuffleOption: z.enum(["none", "all", "exceptLast"]).optional(), + otherOptionPlaceholder: z.string().optional(), }); export type TSurveyMultipleChoiceMultiQuestion = z.infer;