Skip to content

Commit

Permalink
feat : 최대 옵션 항목 수 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
hyesung99 committed Dec 1, 2023
1 parent 092c641 commit 3c4aa62
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/context/FormOptionContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FORM_OPTION } from '@/constants/form';
import useToast from '@/hooks/useToast';
import { FormOption } from '@/types/form';

import { createContext, useCallback } from 'react';
import { createContext, useCallback, useEffect } from 'react';
import { useMemo, useState } from 'react';

interface FormOptionContextProps {
Expand All @@ -21,6 +21,8 @@ interface FormContextOptionProviderProps {
children: React.ReactNode;
}

const MAX_OPTION_COUNT = 20;

const FormOptionContext = createContext<FormOptionContextProps>({
selectedOptions: [],
description: '',
Expand Down Expand Up @@ -54,6 +56,16 @@ const FormOptionContextProvider = ({ children }: FormContextOptionProviderProps)
);
}, []);

useEffect(() => {
if (MAX_OPTION_COUNT < selectedOptions.length) {
createToast({
message: `항목은 최대 ${MAX_OPTION_COUNT}개까지만 추가할 수 있습니다.`,
toastType: 'error',
});
setSelectedOptions((prev) => prev.slice(0, MAX_OPTION_COUNT));
}
}, [selectedOptions]);

const changeOptionTitle = useCallback((option: FormOption, title: string) => {
setSelectedOptions((prev) =>
prev.map((prevOption) => {
Expand Down

0 comments on commit 3c4aa62

Please sign in to comment.