@@ -27,6 +27,10 @@ export interface IBaseInputProps {
27
27
onToolbarClick ?: ( addon ) => void ;
28
28
}
29
29
30
+ /**
31
+ * Mock an Input by textarea
32
+ * 'Cause we have to achieve text wrap and input cannot achieve it
33
+ */
30
34
function Input ( props : IBaseInputProps ) {
31
35
const {
32
36
className,
@@ -38,11 +42,11 @@ function Input(props: IBaseInputProps) {
38
42
} = props ;
39
43
40
44
const [ focusStatus , setFocus ] = React . useState ( false ) ;
41
- const inputRef = React . useRef < HTMLInputElement > ( null ) ;
45
+ const textareaRef = React . useRef < HTMLTextAreaElement > ( null ) ;
42
46
43
47
const onToolbarClick = ( e , item ) => {
44
48
// toolbar click can trigger input focus
45
- inputRef . current ?. focus ( ) ;
49
+ textareaRef . current ?. focus ( ) ;
46
50
props . onToolbarClick ?.( item ) ;
47
51
} ;
48
52
@@ -67,18 +71,46 @@ function Input(props: IBaseInputProps) {
67
71
setFocus ( false ) ;
68
72
} ;
69
73
74
+ const handleInputChange = ( e ) => {
75
+ if ( textareaRef . current ) {
76
+ // base height
77
+ textareaRef . current . style . height = '24px' ;
78
+ const curretnScollerHeight = textareaRef . current . scrollHeight ;
79
+ // count the lines
80
+ const lines = curretnScollerHeight / 24 ;
81
+ const maxLines = 5 ;
82
+ if ( lines > maxLines ) {
83
+ textareaRef . current . style . height = `${ 24 * maxLines } px` ;
84
+ } else {
85
+ textareaRef . current . style . height = `${ textareaRef . current . scrollHeight } px` ;
86
+ }
87
+ }
88
+ onChange ?.( e . target . value || '' ) ;
89
+ } ;
90
+
91
+ const handleInputKeyPress = ( e ) => {
92
+ // detect Enter press
93
+ if ( e . keyCode === 13 ) {
94
+ onChange ?.( e . target . value || '' ) ;
95
+ e . preventDefault ( ) ;
96
+ }
97
+ } ;
98
+
70
99
return (
71
100
< div className = { className } >
72
- < input
73
- ref = { inputRef }
101
+ < textarea
102
+ ref = { textareaRef }
103
+ spellCheck = { false }
104
+ autoCorrect = "off"
105
+ autoCapitalize = "off"
74
106
className = { classNames ( getInfoClassName ( info ?. type || '' ) ) }
75
107
value = { value || '' }
76
108
placeholder = { placeholder }
109
+ title = { placeholder }
110
+ onKeyDown = { handleInputKeyPress }
77
111
onFocus = { handleInputFocus }
78
112
onBlur = { handleInputBlur }
79
- onChange = { ( e ) => {
80
- onChange ?.( e . target . value || '' ) ;
81
- } }
113
+ onChange = { handleInputChange }
82
114
/>
83
115
{ info && focusStatus && (
84
116
< div
0 commit comments