File size: 3,618 Bytes
4e1096a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import clsx from 'clsx';
import React, { useEffect, useRef, useImperativeHandle, forwardRef } from 'react';

interface TextEditorProps {
  value: string;
  onChange: (value: string) => void;
  onBlur?: () => void;
  onSave?: () => void;
  onEscape?: () => void;
  placeholder?: string;
  className?: string;
  autoFocus?: boolean;
  spellCheck?: boolean;
  disabled?: boolean;
  maxRows?: number;
  minRows?: number;
}

export interface TextEditorRef {
  focus: () => void;
  blur: () => void;
  getValue: () => string;
  setValue: (value: string) => void;
  getElement: () => HTMLTextAreaElement | null;
}

const TextEditor = forwardRef<TextEditorRef, TextEditorProps>(
  (
    {
      value,
      onChange,
      onBlur,
      onSave,
      onEscape,
      placeholder,
      className,
      autoFocus = false,
      spellCheck = false,
      disabled = false,
      maxRows,
      minRows = 1,
    },
    ref,
  ) => {
    const editorRef = useRef<HTMLTextAreaElement>(null);

    useImperativeHandle(ref, () => ({
      focus: () => {
        editorRef.current?.focus();
      },
      blur: () => {
        editorRef.current?.blur();
      },
      getValue: () => {
        return editorRef.current?.value || '';
      },
      setValue: (newValue: string) => {
        if (editorRef.current) {
          editorRef.current.value = newValue;
          adjustHeight();
        }
      },
      getElement: () => editorRef.current,
    }));

    useEffect(() => {
      if (autoFocus && editorRef.current) {
        editorRef.current.focus();
      }
    }, [autoFocus]);

    useEffect(() => {
      if (editorRef.current) {
        editorRef.current.value = value;
        adjustHeight();
      }
      // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [value]);

    const adjustHeight = () => {
      if (editorRef.current) {
        editorRef.current.style.height = 'auto';
        const scrollHeight = editorRef.current.scrollHeight;

        // Calculate max height based on maxRows
        let maxHeight = Infinity;
        if (maxRows) {
          const lineHeight = parseInt(getComputedStyle(editorRef.current).lineHeight);
          maxHeight = lineHeight * maxRows;
        }

        // Calculate min height based on minRows
        const lineHeight = parseInt(getComputedStyle(editorRef.current).lineHeight) || 20;
        const minHeight = lineHeight * minRows;

        const finalHeight = Math.min(Math.max(scrollHeight, minHeight), maxHeight);
        editorRef.current.style.height = `${finalHeight}px`;
      }
    };

    const handleChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
      adjustHeight();
      onChange(e.target.value);
    };

    const handleKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
      if (e.key === 'Escape' && onEscape) {
        onEscape();
        return;
      }

      if (e.key === 'Enter' && (e.metaKey || e.ctrlKey) && onSave) {
        e.preventDefault();
        onSave();
        return;
      }
    };

    return (
      <textarea
        ref={editorRef}
        className={clsx(
          'textarea textarea-ghost min-h-[1em] resize-none !outline-none',
          'inset-0 w-full rounded-none border-0 bg-transparent p-0',
          'content font-size-sm',
          className,
        )}
        dir='auto'
        rows={minRows}
        spellCheck={spellCheck}
        disabled={disabled}
        onChange={handleChange}
        onBlur={onBlur}
        onKeyDown={handleKeyDown}
        placeholder={placeholder || ''}
      />
    );
  },
);

TextEditor.displayName = 'TextEditor';

export default TextEditor;