Skip to content

Commit

Permalink
feature: implements FormFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
toothlessdev committed Nov 21, 2024
1 parent 3d1eddc commit f65d079
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 50 deletions.
Empty file added src/@types/index.d.ts
Empty file.
10 changes: 6 additions & 4 deletions src/components/forms/CheckBoxWithLabelForm.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Checkbox } from "@/components/ui/checkbox";

import { FormState, Question } from "./Form";
import { FormState, Question } from "./FormRenderer";

// prettier-ignore
export type CheckBoxWithLabelFormProps =
Omit<Question<string>, "questionType" | "dataType"> &
Omit<Question, "questionType" | "dataType"> &
FormState;

export const CheckBoxWithLabelForm = ({
Expand All @@ -24,7 +24,9 @@ export const CheckBoxWithLabelForm = ({
data-testid={`checkbox_${option}`}
id={`checkbox_${option}`}
className="block w-5 h-5"
checked={(formState[questionText] as Array<string>).includes(option)}
checked={(formState[questionText] as Array<string>).includes(
option as string,
)}
onCheckedChange={(isChecked) => {
if (isChecked) {
setFormState({
Expand All @@ -44,7 +46,7 @@ export const CheckBoxWithLabelForm = ({
}
}}
/>
<label htmlFor={`checkbox_${option}`}>{option}</label>
<label htmlFor={`checkbox_${option}`}>{option as string}</label>
</div>
);
})}
Expand Down
36 changes: 0 additions & 36 deletions src/components/forms/Form.tsx

This file was deleted.

7 changes: 3 additions & 4 deletions src/components/forms/FormFactory.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { CheckBoxWithLabelForm } from "./CheckBoxWithLabelForm";
import { QuestionTypes } from "./Form";
import { QuestionTypes } from "./FormRenderer";
import { SelectorWithLabelForm } from "./SelectorWithLabelForm";
import { DoubleSliderWithLabelForm, SliderWithLabelForm } from "./SliderWithLabelForm";

export interface FormFactoryProps {
questionType: keyof typeof QuestionTypes;
Expand All @@ -10,9 +9,9 @@ export interface FormFactoryProps {
export const FormFactory = ({ questionType }: FormFactoryProps) => {
const FormElements = {
[QuestionTypes.selector]: SelectorWithLabelForm,
[QuestionTypes.slider]: SliderWithLabelForm,
[QuestionTypes.doubleSlider]: DoubleSliderWithLabelForm,
[QuestionTypes.checkbox]: CheckBoxWithLabelForm,
// [QuestionTypes.slider]: SliderWithLabelForm,
// [QuestionTypes.doubleSlider]: DoubleSliderWithLabelForm,
};

return FormElements[questionType];
Expand Down
12 changes: 6 additions & 6 deletions src/components/forms/SelectorWithLabelForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import {
SelectValue,
} from "@/components/ui/select";

import { FormState, Question } from "./Form";
import { FormState, Question } from "./FormRenderer";

// prettier-ignore
export type SelectorWithLabelFormProps =
Omit<Question<string>, "questionType" | "dataType"> &
Omit<Question, "questionType" | "dataType"> &
FormState

export const SelectorWithLabelForm = ({
Expand All @@ -33,22 +33,22 @@ export const SelectorWithLabelForm = ({
onValueChange={(value) =>
setFormState({
...formState,
[questionText]: value,
[questionText]: [value],
})
}
>
<SelectTrigger className="w-full rounded-xl" asChild={false}>
<SelectValue placeholder="옵션을 선택해주세요" />
<SelectValue placeholder="옵션을 선택해주세요"></SelectValue>
</SelectTrigger>
<SelectContent id={htmlFor}>
{options.map((option, index) => {
return (
<SelectItem
key={index}
value={option}
value={option as string}
data-testid={`select-options_${option}`}
>
{option}
{option as string}
</SelectItem>
);
})}
Expand Down

0 comments on commit f65d079

Please sign in to comment.