|
| 1 | +import { FC, MutableRefObject, useCallback, useEffect, useRef } from "react"; |
| 2 | +import { debounce } from "lodash"; |
| 3 | +import { cn } from "../../utils/bem"; |
| 4 | +import { isMacOS } from "../../utils/utilities"; |
| 5 | + |
| 6 | +import "./TextArea.styl"; |
| 7 | +import mergeRefs from "../Utils/mergeRefs"; |
| 8 | + |
| 9 | +export type TextAreaProps = { |
| 10 | + value?: string|null, |
| 11 | + onSubmit?: () => void|Promise<void>, |
| 12 | + onChange?: (value: string) => void, |
| 13 | + onInput?: (value: string) => void, |
| 14 | + ref?: MutableRefObject<HTMLTextAreaElement>, |
| 15 | + actionRef?: MutableRefObject<{ update?: (text?: string) => void }>, |
| 16 | + rows?: number, |
| 17 | + maxRows?: number, |
| 18 | + autoSize?: boolean, |
| 19 | + className?: string, |
| 20 | + placeholder?: string, |
| 21 | + name?: string, |
| 22 | + id?: string, |
| 23 | +} |
| 24 | + |
| 25 | +export const TextArea: FC<TextAreaProps> = ({ |
| 26 | + ref, |
| 27 | + actionRef, |
| 28 | + onChange: _onChange, |
| 29 | + onInput: _onInput, |
| 30 | + onSubmit, |
| 31 | + value, |
| 32 | + autoSize = true, |
| 33 | + rows = 1, |
| 34 | + maxRows = 4, |
| 35 | + className, |
| 36 | + ...props |
| 37 | +}) => { |
| 38 | + |
| 39 | + const inlineAction = !!onSubmit; |
| 40 | + |
| 41 | + const rootClass = cn('textarea'); |
| 42 | + const classList = [ |
| 43 | + rootClass.mod({ inline: inlineAction, autosize: autoSize }), |
| 44 | + className, |
| 45 | + ].join(" ").trim(); |
| 46 | + |
| 47 | + const autoGrowRef = useRef({ |
| 48 | + rows, |
| 49 | + maxRows: Math.max(maxRows - 1, 1), |
| 50 | + lineHeight: 24, |
| 51 | + maxHeight: Infinity, |
| 52 | + }); |
| 53 | + const textAreaRef = useRef<HTMLTextAreaElement>(null); |
| 54 | + |
| 55 | + const resizeTextArea = useCallback(debounce(() => { |
| 56 | + const textarea = textAreaRef.current; |
| 57 | + |
| 58 | + if (!textarea || !autoGrowRef.current || !textAreaRef.current) return; |
| 59 | + |
| 60 | + if (autoGrowRef.current.maxHeight === Infinity) { |
| 61 | + textarea.style.height = 'auto'; |
| 62 | + const currentValue = textAreaRef.current.value; |
| 63 | + |
| 64 | + textAreaRef.current.value = ""; |
| 65 | + autoGrowRef.current.lineHeight = (textAreaRef.current.scrollHeight / autoGrowRef.current.rows); |
| 66 | + autoGrowRef.current.maxHeight = (autoGrowRef.current.lineHeight * autoGrowRef.current.maxRows); |
| 67 | + |
| 68 | + textAreaRef.current.value = currentValue; |
| 69 | + } |
| 70 | + |
| 71 | + let newHeight: number; |
| 72 | + |
| 73 | + if(textarea.scrollHeight > autoGrowRef.current.maxHeight){ |
| 74 | + textarea.style.overflowY = 'scroll'; |
| 75 | + newHeight = autoGrowRef.current.maxHeight; |
| 76 | + } else { |
| 77 | + textarea.style.overflowY = 'hidden'; |
| 78 | + textarea.style.height = 'auto'; |
| 79 | + newHeight = textarea.scrollHeight; |
| 80 | + } |
| 81 | + const contentLength = textarea.value.length; |
| 82 | + const cursorPosition = textarea.selectionStart; |
| 83 | + |
| 84 | + requestAnimationFrame(() => { |
| 85 | + textarea.style.height = `${newHeight}px`; |
| 86 | + |
| 87 | + if (contentLength === cursorPosition) { |
| 88 | + textarea.scrollTop = textarea.scrollHeight; |
| 89 | + } |
| 90 | + }); |
| 91 | + }, 10, { leading: true }), []); |
| 92 | + |
| 93 | + if (actionRef) { |
| 94 | + actionRef.current = { |
| 95 | + update: (text = "") => { |
| 96 | + if (!textAreaRef.current) return; |
| 97 | + |
| 98 | + textAreaRef.current.value = text; |
| 99 | + resizeTextArea(); |
| 100 | + }, |
| 101 | + }; |
| 102 | + } |
| 103 | + |
| 104 | + const onInput = useCallback((e: any) => { |
| 105 | + _onInput?.(e.target.value); |
| 106 | + resizeTextArea(); |
| 107 | + }, [_onInput]); |
| 108 | + |
| 109 | + const onChange = useCallback((e: any) => { |
| 110 | + _onChange?.(e.target.value); |
| 111 | + resizeTextArea(); |
| 112 | + }, [_onChange]); |
| 113 | + |
| 114 | + useEffect(() => { |
| 115 | + const resize = new ResizeObserver(resizeTextArea); |
| 116 | + |
| 117 | + resize.observe(textAreaRef.current as any); |
| 118 | + |
| 119 | + return () => { |
| 120 | + if (textAreaRef.current) { |
| 121 | + resize.unobserve(textAreaRef.current as any); |
| 122 | + } |
| 123 | + }; |
| 124 | + }, []); |
| 125 | + |
| 126 | + useEffect(() => { |
| 127 | + if (textAreaRef.current) { |
| 128 | + textAreaRef.current.value = value || ""; |
| 129 | + resizeTextArea(); |
| 130 | + } |
| 131 | + }, [value]); |
| 132 | + |
| 133 | + useEffect(() => { |
| 134 | + if (!onSubmit) return; |
| 135 | + |
| 136 | + const listener = (event: KeyboardEvent) => { |
| 137 | + if (!textAreaRef.current) return; |
| 138 | + if (event.key === "Enter" && (event.ctrlKey || isMacOS() && event.metaKey)) { |
| 139 | + onSubmit(); |
| 140 | + } |
| 141 | + }; |
| 142 | + |
| 143 | + if (textAreaRef.current) { |
| 144 | + textAreaRef.current.addEventListener("keydown", listener); |
| 145 | + } |
| 146 | + return () => { |
| 147 | + if (textAreaRef.current) { |
| 148 | + textAreaRef.current.removeEventListener("keydown", listener); |
| 149 | + } |
| 150 | + }; |
| 151 | + }, [onSubmit]); |
| 152 | + |
| 153 | + |
| 154 | + return ( |
| 155 | + <textarea ref={mergeRefs(textAreaRef, ref)} className={classList} rows={autoGrowRef.current.rows} onChange={onChange} onInput={onInput} {...props}></textarea> |
| 156 | + ); |
| 157 | +}; |
0 commit comments