-
Notifications
You must be signed in to change notification settings - Fork 2
Feat: 투표 작성 폼 애니메이션 추가 및 UX 개선, 커스텀 체크박스 생성 #92
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
720f67f
feat: 투표 옵션 필드 애니메이션 추가
youdaeng2 7268ec1
feat: 투표 옵션 추가 및 삭제 애니메이션 추가
youdaeng2 5d74b0d
feat: 커스텀 체크박스 컴포넌트 생성 및 투표 생성 페이지 적용
youdaeng2 46ea19b
fix: 투표 생성 옵션 필드 개수 제한 RHF 구독 방식으로 변경
youdaeng2 6984638
Merge branch 'dev' into feat/SOS-49-vote-create-page-animation
youdaeng2 6daee64
Merge branch 'dev' into feat/SOS-49-vote-create-page-animation
youdaeng2 10ff252
Merge branch 'dev' into feat/SOS-49-vote-create-page-animation
youdaeng2 8e230cf
fix: 디자인 수정사항에 맞춰 스타일 수정
youdaeng2 26916db
feat: pnpm-lock
youdaeng2 1fdd810
fix: 옵션 추가/삭제 이벤트 핸들러 분리 및 불필요한 useCallback 제거
youdaeng2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| import React, { useMemo, useState } from 'react'; | ||
| import { useForm, useFieldArray, Controller } from 'react-hook-form'; | ||
| import { zodResolver } from '@hookform/resolvers/zod'; | ||
| import { motion, AnimatePresence } from 'motion/react'; | ||
| import Input from '@/components/inputs/Input'; | ||
| import TextArea from '@/components/inputs/TextArea'; | ||
| import { Button } from '@/components/buttons/Button'; | ||
|
|
@@ -17,6 +18,7 @@ import { VoteboardOptionField } from './VoteoptionField'; | |
| import { CATEGORIES, Category } from '../../constants/categories'; | ||
| import { ImageUploader } from '@/components/ImageUploader'; | ||
| import { Select } from '@/components/select/Select'; | ||
| import { RoundCheckbox } from '@/components/inputs/RoundCheckbox'; | ||
|
|
||
| export interface VoteboardFormProps { | ||
| /** 수정할 투표 게시글 ID (없으면 생성 모드) */ | ||
|
|
@@ -75,6 +77,8 @@ export function VoteboardForm({ | |
| control, | ||
| setValue, | ||
| handleSubmit, | ||
| watch, | ||
| getValues, | ||
| formState: { errors, touchedFields, isValid }, | ||
| } = useForm<VoteboardFormData>({ | ||
| resolver: zodResolver(voteboardSchema), | ||
|
|
@@ -102,6 +106,15 @@ export function VoteboardForm({ | |
| name: 'voteOptions', | ||
| }); | ||
|
|
||
| const watchedOptions = watch('voteOptions'); | ||
| const optionCount = watchedOptions?.length ?? fields.length; | ||
|
|
||
| const MAX_OPTIONS = 5; | ||
| const MIN_OPTIONS = 2; | ||
|
|
||
| // 옵션 추가 가능 여부 (생성 모드 + 최대 5개) | ||
| const canAddMore = !isEdit && optionCount < MAX_OPTIONS; | ||
|
|
||
| // 생성/수정 mutation 훅 | ||
| const { submitPost, isPending } = useVoteboardMutation(voteboardId); | ||
|
|
||
|
|
@@ -117,11 +130,11 @@ export function VoteboardForm({ | |
| }; | ||
|
|
||
| return ( | ||
| <div className="relative flex flex-col h-full w-full "> | ||
| <div className="relative flex flex-col h-full w-full overflow-y-auto"> | ||
| <form | ||
| id="vote-form" | ||
| aria-label={isEdit ? '투표 게시글 수정' : '투표 게시글 작성'} | ||
| className="flex flex-col gap-4 w-full flex-1 overflow-auto p-1 transition-transform duration-300 ease-in-out pb-16" | ||
| className="flex flex-col gap-4 w-full p-1 transition-transform duration-300 ease-in-out" | ||
| onSubmit={handleSubmit(onSubmit)} | ||
| > | ||
| <div> | ||
|
|
@@ -240,35 +253,63 @@ export function VoteboardForm({ | |
| * | ||
| </span> | ||
| </label> | ||
| {!isEdit && ( | ||
| <button | ||
| type="button" | ||
| className="text-xs text-soso-500" | ||
| onClick={() => { | ||
| if (fields.length >= 5) return; | ||
| append({ content: '' }); | ||
| }} | ||
| > | ||
| <Plus className="inline-block w-3 h-3 mr-1" /> | ||
| </button> | ||
| )} | ||
|
|
||
| <AnimatePresence initial={false}> | ||
| {canAddMore && ( | ||
| <motion.button | ||
| key="add-option" | ||
| type="button" | ||
| className="text-xs text-soso-500" | ||
| aria-label="투표 옵션 추가" | ||
| onClick={() => { | ||
| const current = getValues('voteOptions') ?? []; | ||
| if (current.length >= MAX_OPTIONS) return; | ||
| append({ content: '' }); | ||
| }} | ||
| initial={{ opacity: 0, y: -4 }} | ||
| animate={{ opacity: 1, y: 0 }} | ||
| exit={{ opacity: 0, y: -4 }} | ||
| transition={{ duration: 0.15 }} | ||
| whileTap={{ scale: 1.3 }} | ||
| whileHover={{ scale: 1.05 }} | ||
| > | ||
| <Plus className="inline-block w-3 h-3 mr-1" /> | ||
| </motion.button> | ||
| )} | ||
| </AnimatePresence> | ||
| </div> | ||
|
|
||
| {/* 옵션 필드 */} | ||
| <div className="flex flex-col gap-2"> | ||
| {fields.map((field, index) => ( | ||
| <VoteboardOptionField | ||
| key={field.id} | ||
| index={index} | ||
| register={register} | ||
| errorMessage={ | ||
| errors.voteOptions?.[index]?.content?.message | ||
| } | ||
| editable={!isEdit} | ||
| canRemove={!isEdit && fields.length > 2} | ||
| onRemove={() => remove(index)} | ||
| /> | ||
| ))} | ||
| <AnimatePresence initial={false}> | ||
| {fields.map((field, index) => ( | ||
| <motion.div | ||
| key={field.id} | ||
| layout | ||
| initial={{ opacity: 0, y: -6 }} | ||
| animate={{ opacity: 1, y: 0 }} | ||
| exit={{ opacity: 0, y: -6 }} | ||
| transition={{ duration: 0.18 }} | ||
| > | ||
| <VoteboardOptionField | ||
| index={index} | ||
| register={register} | ||
| errorMessage={ | ||
| errors.voteOptions?.[index]?.content?.message | ||
| } | ||
| editable={!isEdit} | ||
| canRemove={!isEdit && optionCount > MIN_OPTIONS} | ||
| onRemove={() => { | ||
| const current = getValues('voteOptions') ?? []; | ||
| if (current.length <= MIN_OPTIONS) return; | ||
| remove(index); | ||
|
||
| }} | ||
| /> | ||
| </motion.div> | ||
| ))} | ||
| </AnimatePresence> | ||
| </div> | ||
|
|
||
| {typeof errors.voteOptions?.message === 'string' && ( | ||
| <p className="text-xs text-red-500"> | ||
| {errors.voteOptions?.message} | ||
|
|
@@ -278,22 +319,14 @@ export function VoteboardForm({ | |
|
|
||
| {/* 설정 (복수 선택 / 재투표) */} | ||
| <div className="flex flex-col gap-2 text-sm"> | ||
| <label className="flex items-center gap-2"> | ||
| <input | ||
| type="checkbox" | ||
| className="w-4 h-4" | ||
| {...register('allowMultipleChoice')} | ||
| /> | ||
| <span>복수 선택 허용</span> | ||
| </label> | ||
| <label className="flex items-center gap-2"> | ||
| <input | ||
| type="checkbox" | ||
| className="w-4 h-4" | ||
| {...register('allowRevote')} | ||
| /> | ||
| <span>재투표 허용</span> | ||
| </label> | ||
| <RoundCheckbox | ||
| label="복수 선택 허용" | ||
| {...register('allowMultipleChoice')} | ||
| /> | ||
| <RoundCheckbox | ||
| label="재투표 허용" | ||
| {...register('allowRevote')} | ||
| /> | ||
| </div> | ||
|
|
||
| {/* 이미지 업로드 */} | ||
|
|
@@ -307,17 +340,19 @@ export function VoteboardForm({ | |
| onDeleteExisting={handleDeleteExisting} | ||
| /> | ||
| </div> | ||
| </form> | ||
|
|
||
| <Button | ||
| type="submit" | ||
| form="vote-form" | ||
| disabled={!isValid || isPending} | ||
| isLoading={isPending} | ||
| className="absolute bottom-0 w-full" | ||
| > | ||
| 저장하기 | ||
| </Button> | ||
| {/* 버튼 */} | ||
| <div className="sticky bottom-0 left-0 right-0 bg-white/90 dark:bg-neutral-900/90 pt-2"> | ||
| <Button | ||
| type="submit" | ||
| disabled={!isValid || isPending} | ||
| isLoading={isPending} | ||
| className="w-full" | ||
| > | ||
| 저장하기 | ||
| </Button> | ||
| </div> | ||
| </form> | ||
| </div> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ import { X } from 'lucide-react'; | |
| import Input from '@/components/inputs/Input'; | ||
| import type { VoteboardFormData } from '../schema/voteboardSchema'; | ||
| import type { UseFormRegister } from 'react-hook-form'; | ||
| import { motion, AnimatePresence } from 'motion/react'; | ||
|
|
||
| interface VoteboardOptionFieldProps { | ||
| /** 옵션 인덱스 (0부터 시작) */ | ||
|
|
@@ -37,25 +38,43 @@ export function VoteboardOptionField({ | |
| onRemove, | ||
| }: VoteboardOptionFieldProps) { | ||
| return ( | ||
| <div className="flex items-center gap-2"> | ||
| <Input | ||
| id={`option-${index}`} | ||
| placeholder="투표 옵션을 입력하세요" | ||
| isError={!!errorMessage} | ||
| errorMessage={errorMessage} | ||
| disabled={!editable} | ||
| {...register(`voteOptions.${index}.content` as const)} | ||
| /> | ||
| {canRemove && ( | ||
| <button | ||
| type="button" | ||
| className="text-xs text-neutral-400" | ||
| onClick={onRemove} | ||
| aria-label={`옵션 ${index + 1} 삭제`} | ||
| > | ||
| <X className="inline-block w-4 h-4" /> | ||
| </button> | ||
| )} | ||
| </div> | ||
| <motion.div | ||
| className="flex items-start gap-2" | ||
| layout | ||
| transition={{ duration: 0.2 }} | ||
| > | ||
| {/* 인풋 + 에러 메시지 영역 */} | ||
| <motion.div className="flex-1" layout> | ||
| <Input | ||
| id={`option-${index}`} | ||
| placeholder="투표 옵션을 입력하세요" | ||
| isError={!!errorMessage} | ||
| errorMessage={errorMessage} | ||
| disabled={!editable} | ||
| {...register(`voteOptions.${index}.content` as const)} | ||
| /> | ||
| </motion.div> | ||
|
|
||
| {/* X 버튼: 높이 46px 박스 안에서 세로 가운데 정렬 */} | ||
| <div className="h-[46px] flex items-center"> | ||
| <AnimatePresence initial={false}> | ||
| {canRemove && ( | ||
| <motion.button | ||
| key="remove" | ||
| type="button" | ||
| onClick={onRemove} | ||
| aria-label={`옵션 ${index + 1} 삭제`} | ||
| className="text-xs text-neutral-400" | ||
| initial={{ opacity: 0, x: 8 }} | ||
| animate={{ opacity: 1, x: 0 }} | ||
| exit={{ opacity: 0, x: 8 }} | ||
| transition={{ duration: 0.15 }} | ||
| > | ||
|
Comment on lines
+60
to
+72
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 접근성과 상세한 애니메이션 설정이 대단하군요 |
||
| <X className="inline-block w-4 h-4" /> | ||
| </motion.button> | ||
| )} | ||
| </AnimatePresence> | ||
| </div> | ||
| </motion.div> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import React, { InputHTMLAttributes } from 'react'; | ||
| import { Check } from 'lucide-react'; | ||
| import { twMerge } from 'tailwind-merge'; | ||
|
|
||
| export interface RoundCheckboxProps | ||
| extends InputHTMLAttributes<HTMLInputElement> { | ||
| /** 체크박스 오른쪽에 표시할 라벨 텍스트 */ | ||
| label?: string; | ||
| } | ||
|
|
||
| /** | ||
| * 동그란 디자인의 커스텀 체크박스 | ||
| * | ||
| * - 비활성: 흰 배경, 뉴트럴 테두리, 뉴트럴 텍스트 | ||
| * - 활성: SOSO 메인 배경, 흰 아이콘, 검정 텍스트 | ||
| */ | ||
| export const RoundCheckbox = React.forwardRef< | ||
| HTMLInputElement, | ||
| RoundCheckboxProps | ||
| >(function RoundCheckbox( | ||
| { label, id, name, className, ...inputProps }, | ||
| ref, | ||
| ) { | ||
| const inputId = id ?? (typeof name === 'string' ? name : undefined); | ||
|
|
||
| const boxClassName = twMerge( | ||
| // 기본 모양 | ||
| 'flex items-center justify-center w-4 h-4 rounded-full border transition-colors', | ||
| // 비활성 상태 | ||
| 'border-neutral-100 bg-white text-transparent', | ||
| // 활성(체크) 상태 | ||
| 'peer-checked:bg-soso-500 peer-checked:border-soso-500 peer-checked:text-white', | ||
| // 포커스 | ||
| 'peer-focus-visible:outline peer-focus-visible:outline-2 peer-focus-visible:outline-offset-2 peer-focus-visible:outline-soso-500', | ||
| ); | ||
|
|
||
| return ( | ||
| <label | ||
| htmlFor={inputId} | ||
| className={twMerge( | ||
| 'inline-flex h-5 items-center gap-2 cursor-pointer text-sm leading-none', | ||
| className, | ||
| )} | ||
| > | ||
| {/* 실제 체크박스 */} | ||
| <input | ||
| id={inputId} | ||
| name={name} | ||
| type="checkbox" | ||
| ref={ref} | ||
| className="peer sr-only" | ||
| {...inputProps} | ||
| /> | ||
|
|
||
| {/* 커스텀 체크박스 */} | ||
| <span className={boxClassName}> | ||
| <Check className="w-3 h-3" /> | ||
| </span> | ||
|
|
||
| {/* 라벨 텍스트 */} | ||
| {label && <span className="text-neutral-900">{label}</span>} | ||
| </label> | ||
| ); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
복잡한 클릭 이벤트 핸들러는 분리하는게 좋아보여요
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
또, useCallback의 필요성도 확인해주세요
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
리뷰 감사합니다.
주신 피드백 반영하여 인라인으로 있던 핸들러는 분리하였고,
useCallback에 대해서도 검토해보았습니다!
useCallback은
주로 사용하는데 현재는 해당하는 부분이 없어 제거하였습니다.