Skip to content

Commit a532221

Browse files
committed
fix(forms): use imported shouldShowQuestion
Removed the local shouldShowQuestion helper and imported it from StepperForm to centralize logic and reduce code duplication.
1 parent 041e4e5 commit a532221

File tree

1 file changed

+1
-32
lines changed

1 file changed

+1
-32
lines changed

components/forms/FormWrapper.tsx

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import DigitsOnlyField from './DigitsOnlyField';
1212
import type { FormQuestion } from '@/types';
1313
import { useMemo } from 'react';
1414
import { useFormContext } from 'react-hook-form';
15+
import { shouldShowQuestion } from '@/components/multi-step-form/StepperForm';
1516

1617
interface FormWrapperProps {
1718
form?: UseFormReturn<Record<string, unknown>>; // Replace any with more specific type
@@ -26,38 +27,6 @@ interface FormWrapperProps {
2627
hideSubmitButton?: boolean;
2728
}
2829

29-
// Helper function to check if a question should be shown based on dependencies
30-
const shouldShowQuestion = (
31-
question: FormQuestion,
32-
formValues: Record<string, unknown>
33-
): boolean => {
34-
// If the question has no showIf condition, always show it
35-
if (!question.showIf) return true;
36-
37-
// Check each condition to determine if the question should be shown
38-
for (const [dependentField, requiredValue] of Object.entries(
39-
question.showIf
40-
)) {
41-
const fieldValue = formValues[dependentField];
42-
43-
// If required value is an array, check if the current value is in that array
44-
if (Array.isArray(requiredValue)) {
45-
// Convert to string for comparison since form values are often strings
46-
const strValue =
47-
typeof fieldValue === 'string' ? fieldValue : String(fieldValue || '');
48-
if (!requiredValue.includes(strValue)) {
49-
return false;
50-
}
51-
}
52-
// Otherwise check if the current value equals the required value
53-
else if (fieldValue !== requiredValue) {
54-
return false;
55-
}
56-
}
57-
58-
return true;
59-
};
60-
6130
export default function FormWrapper({
6231
form: formProp,
6332
questions,

0 commit comments

Comments
 (0)