add dont care option to make survey UX more understandable
This commit is contained in:
parent
4f6882492d
commit
78e61293ee
@ -12,6 +12,7 @@ export interface Question<T> {
|
|||||||
for: string,
|
for: string,
|
||||||
header: string,
|
header: string,
|
||||||
question: string,
|
question: string,
|
||||||
|
maxSelect?: number,
|
||||||
optional: true,
|
optional: true,
|
||||||
options: Option<T>[]
|
options: Option<T>[]
|
||||||
}
|
}
|
||||||
@ -35,14 +36,19 @@ const GreetingPage = ({updatePage}: {
|
|||||||
/**
|
/**
|
||||||
* Single question component for a guided survey
|
* Single question component for a guided survey
|
||||||
*/
|
*/
|
||||||
const QuestionPage = ({isLastPage, page, question, updatePage, formData, updateFormData}: {
|
const QuestionPage = ({isLastPage, page, question, updatePage, formData, updateFormData, dontCareData, setDontCareData}: {
|
||||||
isLastPage: boolean,
|
isLastPage: boolean,
|
||||||
page: number,
|
page: number,
|
||||||
question: Question<QuestionTypes>,
|
question: Question<QuestionTypes>,
|
||||||
updatePage: (pageNumber: number) => void,
|
updatePage: (pageNumber: number) => void,
|
||||||
formData: Record<string, QuestionTypes[]>,
|
formData: Record<string, QuestionTypes[]>,
|
||||||
updateFormData: Dispatch<SetStateAction<Record<string, QuestionTypes[]>>>,
|
updateFormData: Dispatch<SetStateAction<Record<string, QuestionTypes[]>>>,
|
||||||
|
dontCareData: Record<string, boolean>,
|
||||||
|
setDontCareData: Dispatch<SetStateAction<Record<string, boolean>>>,
|
||||||
|
|
||||||
}) => {
|
}) => {
|
||||||
|
const dontCare = dontCareData[question.for] ?? false;
|
||||||
|
|
||||||
const OptionToggle = ({option}: {option: Option<QuestionTypes>}) => {
|
const OptionToggle = ({option}: {option: Option<QuestionTypes>}) => {
|
||||||
const selected = formData[question.for]?.includes(option.value) ?? false;
|
const selected = formData[question.for]?.includes(option.value) ?? false;
|
||||||
|
|
||||||
@ -64,6 +70,14 @@ const QuestionPage = ({isLastPage, page, question, updatePage, formData, updateF
|
|||||||
updateFormData(newFormData);
|
updateFormData(newFormData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (dontCare) {
|
||||||
|
return (
|
||||||
|
<button disabled type="button" onClick={handleToggle} className={"mx-auto w-64 py-2 shadow rounded-lg border border-neutral-400 " + (selected ? "bg-amber-200" : "bg-white")}>
|
||||||
|
{option.label}
|
||||||
|
</button>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<button type="button" onClick={handleToggle} className={"mx-auto w-64 py-2 shadow rounded-lg border border-neutral-400 " + (selected ? "bg-amber-200" : "bg-white")}>
|
<button type="button" onClick={handleToggle} className={"mx-auto w-64 py-2 shadow rounded-lg border border-neutral-400 " + (selected ? "bg-amber-200" : "bg-white")}>
|
||||||
{option.label}
|
{option.label}
|
||||||
@ -80,6 +94,24 @@ const QuestionPage = ({isLastPage, page, question, updatePage, formData, updateF
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const dontCareToggle = () => {
|
||||||
|
if (formData[question.for]) {
|
||||||
|
const newFormData = {
|
||||||
|
...formData
|
||||||
|
};
|
||||||
|
|
||||||
|
newFormData[question.for] = [];
|
||||||
|
updateFormData(newFormData);
|
||||||
|
}
|
||||||
|
|
||||||
|
const newDontCareData = {
|
||||||
|
...dontCareData
|
||||||
|
}
|
||||||
|
|
||||||
|
newDontCareData[question.for] = !dontCare;
|
||||||
|
setDontCareData(newDontCareData);
|
||||||
|
}
|
||||||
|
|
||||||
const nextClick = () => {
|
const nextClick = () => {
|
||||||
updatePage(page + 1);
|
updatePage(page + 1);
|
||||||
}
|
}
|
||||||
@ -120,6 +152,11 @@ const QuestionPage = ({isLastPage, page, question, updatePage, formData, updateF
|
|||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
</section>
|
</section>
|
||||||
|
{question.optional ?
|
||||||
|
<button type="button" onClick={dontCareToggle} className={"mx-auto w-64 py-2 shadow rounded-lg border border-neutral-400 " + (dontCare ? "bg-amber-200" : "bg-white")}>
|
||||||
|
No Preference
|
||||||
|
</button>
|
||||||
|
: undefined}
|
||||||
<AdvanceButtons />
|
<AdvanceButtons />
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
@ -152,6 +189,7 @@ const GuidedSurvey = ({questions}: {
|
|||||||
}) => {
|
}) => {
|
||||||
const [page, setPage] = useState<number>(0);
|
const [page, setPage] = useState<number>(0);
|
||||||
const [formData, setFormData] = useState<(Record<string, QuestionTypes[]>)>({});
|
const [formData, setFormData] = useState<(Record<string, QuestionTypes[]>)>({});
|
||||||
|
const [dontCareData, setDoneCareData] = useState<(Record<string, boolean>)>({});
|
||||||
const [previousPage, setPreviousPage] = useState<number | undefined>(undefined);
|
const [previousPage, setPreviousPage] = useState<number | undefined>(undefined);
|
||||||
|
|
||||||
const updatePage = (pageNumber: number) => {
|
const updatePage = (pageNumber: number) => {
|
||||||
@ -180,7 +218,7 @@ const GuidedSurvey = ({questions}: {
|
|||||||
const isLastPage = pageNumber === questions.length
|
const isLastPage = pageNumber === questions.length
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<QuestionPage isLastPage={isLastPage} page={page} formData={formData} updateFormData={setFormData} updatePage={updatePage} question={question} />
|
<QuestionPage dontCareData={dontCareData} setDontCareData={setDoneCareData} isLastPage={isLastPage} page={page} formData={formData} updateFormData={setFormData} updatePage={updatePage} question={question} />
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ const questions: Question<QuestionTypes>[] = [
|
|||||||
for: "ages",
|
for: "ages",
|
||||||
header: "Age of Patient",
|
header: "Age of Patient",
|
||||||
question: "How old is the patient?",
|
question: "How old is the patient?",
|
||||||
|
maxSelect: 1,
|
||||||
optional: true,
|
optional: true,
|
||||||
options: [
|
options: [
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user