Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Add unique keys for answers and questions #1430

Merged
merged 2 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,50 +29,53 @@ export const MultiInputSelection: React.FC<MultiInputSelectionProps> = ({
const { t } = useTranslation();
return (
<Stack>
{sortedOptions.map((option, i) => (
<StackItem key={`${questionFieldName}-${i}`} className="pf-v5-u-pb-xs">
<HookFormPFGroupController
control={control}
name={questionFieldName as `questions.${string}`}
fieldId={`${questionFieldName}-${i}`}
renderInput={({ field: { value, onChange } }) => (
<Radio
id={`${questionFieldName}-${option.order}`}
name={questionFieldName}
isChecked={value === option.text}
onChange={(checked, e) => {
onChange(option.text);
}}
aria-label={option.text}
label={
<>
{option.autoAnswered && option.autoAnswerFor?.length ? (
<Tooltip
content={t(
isArchetype
? "message.selectedBecauseArchetypeTags"
: "message.selectedBecauseAppOrArchetypeTags",
{
tags: option.autoAnswerFor
.map((t) => `"${t.tag}"`)
.join(", "),
}
)}
>
<Icon status="info">
<InfoCircleIcon />
</Icon>
</Tooltip>
) : null}{" "}
{option.text}
</>
}
value={option.text}
/>
)}
/>
</StackItem>
))}
{sortedOptions.map((option, i) => {
const answerUniqueId = `${questionFieldName}-${option.text}-${i}}`;
return (
<StackItem key={answerUniqueId} className="pf-v5-u-pb-xs">
<HookFormPFGroupController
control={control}
name={questionFieldName as `questions.${string}`}
fieldId={answerUniqueId}
renderInput={({ field: { value, onChange } }) => (
<Radio
id={answerUniqueId}
name={questionFieldName}
isChecked={value === option.text}
onChange={(checked, e) => {
onChange(option.text);
}}
aria-label={option.text}
label={
<>
{option.autoAnswered && option.autoAnswerFor?.length ? (
<Tooltip
content={t(
isArchetype
? "message.selectedBecauseArchetypeTags"
: "message.selectedBecauseAppOrArchetypeTags",
{
tags: option.autoAnswerFor
.map((t) => `"${t.tag}"`)
.join(", "),
}
)}
>
<Icon status="info">
<InfoCircleIcon />
</Icon>
</Tooltip>
) : null}{" "}
{option.text}
</>
}
value={option.text}
/>
)}
/>
</StackItem>
);
})}
</Stack>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -53,35 +53,40 @@ export const QuestionnaireForm: React.FC<QuestionnaireFormProps> = ({
<Text component="h1">{section.name}</Text>
</TextContent>
</StackItem>
{sortedQuestions.map((question) => (
<StackItem key={question.order}>
<Question cy-data="question">
<QuestionHeader>
<Split hasGutter>
<SplitItem>{question.text}</SplitItem>
<SplitItem>
<Popover bodyContent={<div>{question.explanation}</div>}>
<button
type="button"
aria-label="More info"
onClick={(e) => e.preventDefault()}
className="pf-v5-c-form__group-label-help"
>
<HelpIcon />
</button>
</Popover>
</SplitItem>
</Split>
</QuestionHeader>
<QuestionBody>
<MultiInputSelection
key={getQuestionFieldName(question, true)}
question={question}
/>
</QuestionBody>
</Question>
</StackItem>
))}
{sortedQuestions.map((question) => {
const questionUniqueKey = `${section.name}-${
question.order || "no-order"
}-${question.text || "no-text"}`;
return (
<StackItem key={questionUniqueKey}>
<Question cy-data="question">
<QuestionHeader>
<Split hasGutter>
<SplitItem>{question.text}</SplitItem>
<SplitItem>
<Popover bodyContent={<div>{question.explanation}</div>}>
<button
type="button"
aria-label="More info"
onClick={(e) => e.preventDefault()}
className="pf-v5-c-form__group-label-help"
>
<HelpIcon />
</button>
</Popover>
</SplitItem>
</Split>
</QuestionHeader>
<QuestionBody>
<MultiInputSelection
key={getQuestionFieldName(question, true)}
question={question}
/>
</QuestionBody>
</Question>
</StackItem>
);
})}
<StackItem>
<Question>
<QuestionHeader>
Expand Down
4 changes: 2 additions & 2 deletions client/src/app/pages/assessment/form-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ describe("Application assessment - form utils", () => {

it("getQuestionFieldName: fullName", () => {
const fieldName = getQuestionFieldName(question, true);
expect(fieldName).toBe("questions.question-1");
expect(fieldName).toBe("questions.question-1-Question 321");
});

it("getQuestionFieldName: singleName", () => {
const fieldName = getQuestionFieldName(question, false);
expect(fieldName).toBe("question-1");
expect(fieldName).toBe("question-1-Question 321");
});
});
2 changes: 1 addition & 1 deletion client/src/app/pages/assessment/form-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ export const getCommentFieldName = (section: Section, fullName: boolean) => {
};

export const getQuestionFieldName = (question: Question, fullName: boolean) => {
const fieldName = `question-${question.order}`;
const fieldName = `question-${question.order}-${question.text}`;
return fullName ? `${QUESTIONS_KEY}.${fieldName}` : fieldName;
};
Loading