Skip to content

Commit

Permalink
feat : TextArea 개발 완료 (#118)
Browse files Browse the repository at this point in the history
* feat : Text Area 개발 완료

* story : TextArea 스토리 작성
  • Loading branch information
HelloWook authored Nov 28, 2024
1 parent 04f04b4 commit bb6c2b0
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/components/Common/TextArea/TextArea.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { Meta, StoryObj } from '@storybook/react';

import { TextArea } from './TextArea';

const meta: Meta<typeof TextArea> = {
component: TextArea,
title: 'atoms/TextArea',
tags: ['autodocs'],
argTypes: {}
};
export default meta;

type Story = StoryObj<typeof TextArea>;

export const Default: Story = {
args: {}
};
48 changes: 48 additions & 0 deletions src/components/Common/TextArea/TextArea.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React, { useRef, useEffect } from 'react';

type TextAreaProps = {
value: string;
setValue: (value: string) => void;
};

export const TextArea = ({ value, setValue }: TextAreaProps) => {
const textAreaRef = useRef<HTMLTextAreaElement>(null);

const handleInputChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
let inputValue = e.target.value;

if (textAreaRef.current) {
const textAreaHeight = textAreaRef.current.scrollHeight;
const textAreaLineHeight = parseInt(
window.getComputedStyle(textAreaRef.current).lineHeight
);

const linesCount = Math.floor(textAreaHeight / textAreaLineHeight);

if (linesCount > 8) {
inputValue = value;
}
}

const lines = inputValue.split('\n');
if (lines.length <= 8) {
setValue(inputValue);
}
};

useEffect(() => {
if (textAreaRef.current) {
textAreaRef.current.style.height = 'auto';
textAreaRef.current.style.height = `${textAreaRef.current.scrollHeight}px`;
}
}, [value]);
return (
<textarea
className="absolute w-9/12 px-2 bg-transparent border-none resize-none mt-[42%] leading-[260%] overflow-hidden h-auto"
ref={textAreaRef}
placeholder="편지를 작성하세요..."
value={value}
onChange={handleInputChange}
/>
);
};

0 comments on commit bb6c2b0

Please sign in to comment.