-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: new yorker text-area to initial
- Loading branch information
Showing
1 changed file
with
11 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,24 @@ | ||
import * as React from "react"; | ||
import { cn } from "@/lib/utils"; | ||
import { useRef, useEffect } from 'react' | ||
import { mergeRefs } from "react-merge-refs"; | ||
export interface TextareaProps extends React.TextareaHTMLAttributes<HTMLTextAreaElement> { } | ||
import * as React from "react" | ||
|
||
const Textarea = React.forwardRef<HTMLTextAreaElement, TextareaProps>( | ||
({ className, ...props }, ref) => { | ||
const texteAreaRef = useRef<HTMLTextAreaElement>(null); | ||
|
||
useEffect(() => { | ||
const ref = texteAreaRef?.current; | ||
|
||
const updateTextareaHeight = () => { | ||
if (ref) { | ||
ref.style.height = "auto"; | ||
ref.style.height = ref?.scrollHeight + "px"; | ||
} | ||
}; | ||
import { cn } from "@/lib/utils" | ||
|
||
updateTextareaHeight(); | ||
ref?.addEventListener("input", updateTextareaHeight); | ||
|
||
return () => ref?.removeEventListener("input", updateTextareaHeight); | ||
}, []); | ||
export interface TextareaProps | ||
extends React.TextareaHTMLAttributes<HTMLTextAreaElement> {} | ||
|
||
const Textarea = React.forwardRef<HTMLTextAreaElement, TextareaProps>( | ||
({ className, ...props }, ref) => { | ||
return ( | ||
<textarea | ||
className={cn( | ||
"flex min-h-[60px] w-full rounded-md border border-input bg-transparent px-3 py-2 text-sm shadow-sm placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50", | ||
className | ||
)} | ||
ref={mergeRefs([texteAreaRef, ref])} | ||
ref={ref} | ||
{...props} | ||
/> | ||
); | ||
) | ||
} | ||
); | ||
|
||
Textarea.displayName = "Textarea"; | ||
) | ||
Textarea.displayName = "Textarea" | ||
|
||
export { Textarea }; | ||
export { Textarea } |