-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* chore(#34): install zod, react-hook-form, framer-motion, @hookform/resolvers * feat(#34): LotusCodeInputFrom widget * feat(#34): LotusCodeInputSection widget * feat(#34): LotusCodeInput barrel file * feat(#34): LotusCodeInput dnd framer-motion을 사용한 dnd 컴포넌트
- Loading branch information
Showing
4 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
84 changes: 84 additions & 0 deletions
84
apps/frontend/src/widget/LotusCodeInput/LotusCodeInputForm.tsx
This file contains 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,84 @@ | ||
import { Button, Input, Text } from '@froxy/design/components'; | ||
import { cn } from '@froxy/design/utils'; | ||
import { zodResolver } from '@hookform/resolvers/zod'; | ||
import { Reorder } from 'framer-motion'; | ||
import { useFieldArray, useForm } from 'react-hook-form'; | ||
import { z } from 'zod'; | ||
|
||
export const lotusCodeInputValue = z.object({ | ||
items: z.array( | ||
z.object({ | ||
input: z.string().min(1, '입력값은 필수 항목입니다.') | ||
}) | ||
) | ||
}); | ||
|
||
export type LotusCodeFormValues = z.infer<typeof lotusCodeInputValue>; | ||
|
||
interface LotusCodeInputFormProps { | ||
className?: string; | ||
onSubmit?: (args: { data: LotusCodeFormValues; reset: () => void }) => void; | ||
} | ||
|
||
export function LotusCodeInputForm(props: LotusCodeInputFormProps) { | ||
const { | ||
register, | ||
reset, | ||
control, | ||
handleSubmit, | ||
formState: { errors } | ||
} = useForm<LotusCodeFormValues>({ | ||
defaultValues: lotusCodeInputValue.parse({ items: [] }), | ||
resolver: zodResolver(lotusCodeInputValue) | ||
}); | ||
|
||
const { fields, append, remove, swap } = useFieldArray({ | ||
control, | ||
name: 'items' | ||
}); | ||
|
||
const onSubmit = (data: LotusCodeFormValues) => props.onSubmit?.({ data, reset }); | ||
|
||
const handleReorder = (newFields: typeof fields) => { | ||
const firstDiffIndex = fields.findIndex((field, index) => field.id !== newFields[index].id); | ||
|
||
if (firstDiffIndex === -1) return; | ||
|
||
const newIndex = newFields.findIndex((field) => field.id === fields[firstDiffIndex].id); | ||
|
||
swap(firstDiffIndex, newIndex); | ||
}; | ||
|
||
return ( | ||
<form onSubmit={handleSubmit(onSubmit)} className={cn(props.className)}> | ||
<Reorder.Group drag="y" values={fields} onReorder={handleReorder}> | ||
{fields.map((field, index) => ( | ||
<Reorder.Item key={field.id} value={field} className="hover:cursor-move"> | ||
<div className="flex gap-2 items-center"> | ||
<Text size="lg" className="w-10"> | ||
{index + 1}번 | ||
</Text> | ||
<Input {...register(`items.${index}.input`)} placeholder={`Input ${index + 1}`} /> | ||
<Button onClick={() => remove(index)}>삭제</Button> | ||
</div> | ||
|
||
<Text variant="destructive" className="min-h-5 my-2 pl-12"> | ||
{errors.items?.[index]?.input && errors.items[index].input?.message} | ||
</Text> | ||
</Reorder.Item> | ||
))} | ||
</Reorder.Group> | ||
<Button variant={'outline'} className="block w-full my-5" onClick={() => append({ input: '' })}> | ||
새로운 항목 추가 | ||
</Button> | ||
<div className="flex gap-2"> | ||
<Button className="w-full" variant={'secondary'} onClick={() => reset()}> | ||
취소하기 | ||
</Button> | ||
<Button className="w-full" type="submit"> | ||
완료하기 | ||
</Button> | ||
</div> | ||
</form> | ||
); | ||
} |
22 changes: 22 additions & 0 deletions
22
apps/frontend/src/widget/LotusCodeInput/LotusCodeInputSection.tsx
This file contains 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,22 @@ | ||
import { Heading, Text } from '@froxy/design/components'; | ||
import { cn } from '@froxy/design/utils'; | ||
import { LotusCodeFormValues, LotusCodeInputForm } from './LotusCodeInputForm'; | ||
|
||
type LotusCodeInputSectionProps = { | ||
className?: string; | ||
}; | ||
|
||
export function LotusCodeInputSection({ className }: LotusCodeInputSectionProps) { | ||
const handelSubmit = ({ data, reset }: { data: LotusCodeFormValues; reset: () => void }) => { | ||
console.log(data); | ||
reset(); | ||
}; | ||
|
||
return ( | ||
<section className={cn('m-10', className)}> | ||
<Heading>실행 입력 코드</Heading> | ||
<Text variant="muted">다음의 코드가 실행 시 순서대로 입력됩니다. 드래그를 통해 순서를 변경할 수 있습니다.</Text> | ||
<LotusCodeInputForm className={'my-10'} onSubmit={handelSubmit} /> | ||
</section> | ||
); | ||
} |
This file contains 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,2 @@ | ||
export * from './LotusCodeInputForm'; | ||
export * from './LotusCodeInputSection'; |