
const handleTextInput = (newMessage: string) => {
handleResizeHeight();
//...
};
const handleResizeHeight = () => {
textArea.current.style.height = 'auto';
let scrollHeight = textArea.current.scrollHeight;
if (scrollHeight >= maxTextInputHeight) {
scrollHeight = maxTextInputHeight;
}
textArea.current.style.height = `${scrollHeight}px`;
};
<TextFieldInput
value={textDisabled ? '' : message}
onChange={(e) => handleTextInput(e.target.value)}
/>
<TextFieldInput
rows={1}
...
/>
const textAreaRef = useRef<HTMLTextAreaElement>(null);
<TextFieldInput
rows={1}
ref={textAreaRef}
value={message}
onChange={(e) => handleTextInput(e.target.value)}
/>
useRef를 이용해서 textarea를 가지고 있는다.
그리고 textAreaRef의 초기값이 null이기 때문에
const handleResizeHeight = () => {
if (textAreaRef.current) {
textAreaRef.current.style.height = 'auto';
let scrollHeight = textAreaRef.current.scrollHeight;
if (scrollHeight >= maxTextInputHeight) {
scrollHeight = maxTextInputHeight;
}
textAreaRef.current.style.height = `${scrollHeight}px`;
}
};
<TextFieldBox>
<TextFieldInputWrapper>
<TextFieldInput
rows={1}
value={textDisabled ? '' : message}
onChange={(e) => handleTextInput(e.target.value)}
/>
<SendButtonWrapper />
</TextFieldInputWrapper>
<SendButtonBox>
<ArrowUpIcon />
</SendButtonBox>
</TextFieldBox>
const TextFieldBox = styled.div<{
backgroundColor: string;
border: BorderProps;
}>`
max-height: 174px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: flex-start;
position: relative;
//...
`;
const TextFieldInputWrapper = styled.div`
display: flex;
justify-content: center;
align-items: center;
//...
`;
const TextFieldInput = styled(Typography).attrs({ as: 'textarea' })<{
placeholderColor: string;
textColor: string;
}>`
flex: 1;
//....
`;
const SendButtonWrapper = styled.div`
width: 30px;
height: 30px;
`;
const SendButtonBox = styled.div<{ backgroundColor: string }>`
width: 30px;
height: 30px;
position: absolute;
right: 4px;
bottom: 6px;
//...
`;
