Skip to content

Commit

Permalink
[#3] 커스텀 textarea
Browse files Browse the repository at this point in the history
  • Loading branch information
03hoho03 committed Apr 6, 2024
1 parent 3e213bf commit 73e1ac1
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
.textarea_container {
position: relative;
padding: 12px;
padding-bottom: 28px;
border: 1px solid #ccc;
border-radius: 8px;
}
.textarea_container:focus-within {
border: 1px solid #000000;
}
.textarea_container > textarea {
width: 100%;
height: 80px;
border:none;
outline: none;
resize: none;
}
.letter_count_container {
position: absolute;
font-size: 12px;
bottom: 8px;
right: 8px;
}
.letter_max {
color: rgba(255, 0, 0, 0.8);
}
62 changes: 62 additions & 0 deletions app/(route)/verification/ibulsin/_components/Textarea/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
'use client'

import React from 'react'
import S from './index.module.css'
import cn from 'classnames'
import { useForm } from 'react-hook-form'

interface TextAreaProps {
fieldKey: 'outline' | 'why'
}

function Textarea({ fieldKey }: TextAreaProps) {
const { register, watch } = useForm()

const outlineField = register('outline', {
required: '',
maxLength: { value: 500, message: '최대 500자까지 작성가능합니다.' },
})
const whyField = register('whyField', {
required: '',
maxLength: { value: 500, message: '최대 500자까지 작성가능합니다.' },
})
const fields = {
outline: {
register: outlineField,
MaxLength: 500,
},
why: {
register: whyField,
MaxLength: 500,
},
}

const countCharacters = () => {
const content = watch(fieldKey)
if (!content) {
return 0
}
return content.length
}

return (
<div className={S.textarea_container}>
<textarea
{...fields[fieldKey].register}
maxLength={fields[fieldKey].MaxLength - 1}
/>
<div className={S.letter_count_container}>
<span
className={cn({
[S.letter_max]: countCharacters() >= fields[fieldKey].MaxLength,
})}
>
{countCharacters()}
</span>
<span>{`/${fields[fieldKey].MaxLength}`}</span>
</div>
</div>
)
}

export default Textarea

0 comments on commit 73e1ac1

Please sign in to comment.