@@ -45,174 +45,150 @@ export const InputField: React.FC<InputFieldProps> = ({
4545 [ onChange ] ,
4646 ) ;
4747
48- useInput (
49- useCallback (
50- ( input , key ) => {
51- if ( ! showCursor ) return ;
48+ useInput ( ( input , key ) => {
49+ if ( ! showCursor ) return ;
5250
53- if ( key . return ) {
54- onSubmit ( ) ;
55- return ;
56- }
51+ if ( key . return ) {
52+ onSubmit ( ) ;
53+ return ;
54+ }
5755
58- if ( key . backspace ) {
59- if ( cursorPosition > 0 ) {
60- const newValue =
61- value . slice ( 0 , cursorPosition - 1 ) + value . slice ( cursorPosition ) ;
62- updateValue ( newValue , cursorPosition - 1 ) ;
63- }
64- return ;
65- }
56+ if ( key . backspace ) {
57+ if ( cursorPosition > 0 ) {
58+ const newValue =
59+ value . slice ( 0 , cursorPosition - 1 ) + value . slice ( cursorPosition ) ;
60+ updateValue ( newValue , cursorPosition - 1 ) ;
61+ }
62+ return ;
63+ }
6664
67- // Delete character at cursor position (Delete key and Ctrl+d)
68- if ( key . delete || ( key . ctrl && input === "d" ) ) {
69- if ( cursorPosition < value . length ) {
70- const newValue =
71- value . slice ( 0 , cursorPosition ) + value . slice ( cursorPosition + 1 ) ;
72- updateValue ( newValue , cursorPosition ) ;
73- }
74- return ;
75- }
65+ // Delete character at cursor position (Delete key and Ctrl+d)
66+ if ( key . delete || ( key . ctrl && input === "d" ) ) {
67+ if ( cursorPosition < value . length ) {
68+ const newValue =
69+ value . slice ( 0 , cursorPosition ) + value . slice ( cursorPosition + 1 ) ;
70+ updateValue ( newValue , cursorPosition ) ;
71+ }
72+ return ;
73+ }
7674
77- // Cursor movement
78- if ( key . leftArrow || ( key . ctrl && input === "b" ) ) {
79- setCursorPosition ( Math . max ( 0 , cursorPosition - 1 ) ) ;
80- return ;
81- }
75+ // Cursor movement
76+ if ( key . leftArrow || ( key . ctrl && input === "b" ) ) {
77+ setCursorPosition ( Math . max ( 0 , cursorPosition - 1 ) ) ;
78+ return ;
79+ }
8280
83- if ( key . rightArrow || ( key . ctrl && input === "f" ) ) {
84- setCursorPosition ( Math . min ( value . length , cursorPosition + 1 ) ) ;
85- return ;
86- }
81+ if ( key . rightArrow || ( key . ctrl && input === "f" ) ) {
82+ setCursorPosition ( Math . min ( value . length , cursorPosition + 1 ) ) ;
83+ return ;
84+ }
8785
88- if ( key . upArrow || ( key . ctrl && input === "p" ) ) {
89- if ( currentLineIndex > 0 ) {
90- const prevLineLength = lines [ currentLineIndex - 1 ] ?. length || 0 ;
91- const newColumnIndex = Math . min ( currentColumnIndex , prevLineLength ) ;
92- let newPos = 0 ;
93- for ( let i = 0 ; i < currentLineIndex - 1 ; i ++ ) {
94- newPos += ( lines [ i ] ?. length || 0 ) + 1 ;
95- }
96- newPos += newColumnIndex ;
97- setCursorPosition ( newPos ) ;
98- }
99- return ;
86+ if ( key . upArrow || ( key . ctrl && input === "p" ) ) {
87+ if ( currentLineIndex > 0 ) {
88+ const prevLineLength = lines [ currentLineIndex - 1 ] ?. length || 0 ;
89+ const newColumnIndex = Math . min ( currentColumnIndex , prevLineLength ) ;
90+ let newPos = 0 ;
91+ for ( let i = 0 ; i < currentLineIndex - 1 ; i ++ ) {
92+ newPos += ( lines [ i ] ?. length || 0 ) + 1 ;
10093 }
94+ newPos += newColumnIndex ;
95+ setCursorPosition ( newPos ) ;
96+ }
97+ return ;
98+ }
10199
102- if ( key . downArrow || ( key . ctrl && input === "n" ) ) {
103- if ( currentLineIndex < lines . length - 1 ) {
104- const nextLineLength = lines [ currentLineIndex + 1 ] ?. length || 0 ;
105- const newColumnIndex = Math . min ( currentColumnIndex , nextLineLength ) ;
106- let newPos = 0 ;
107- for ( let i = 0 ; i <= currentLineIndex ; i ++ ) {
108- newPos += ( lines [ i ] ?. length || 0 ) + 1 ;
109- }
110- newPos += newColumnIndex ;
111- setCursorPosition ( newPos ) ;
112- }
113- return ;
100+ if ( key . downArrow || ( key . ctrl && input === "n" ) ) {
101+ if ( currentLineIndex < lines . length - 1 ) {
102+ const nextLineLength = lines [ currentLineIndex + 1 ] ?. length || 0 ;
103+ const newColumnIndex = Math . min ( currentColumnIndex , nextLineLength ) ;
104+ let newPos = 0 ;
105+ for ( let i = 0 ; i <= currentLineIndex ; i ++ ) {
106+ newPos += ( lines [ i ] ?. length || 0 ) + 1 ;
114107 }
108+ newPos += newColumnIndex ;
109+ setCursorPosition ( newPos ) ;
110+ }
111+ return ;
112+ }
115113
116- // Home/End
117- if ( key . ctrl && input === "a" ) {
118- let lineStartPos = 0 ;
119- for ( let i = 0 ; i < currentLineIndex ; i ++ ) {
120- lineStartPos += ( lines [ i ] ?. length || 0 ) + 1 ;
121- }
122- setCursorPosition ( lineStartPos ) ;
123- return ;
124- }
114+ // Home/End
115+ if ( key . ctrl && input === "a" ) {
116+ let lineStartPos = 0 ;
117+ for ( let i = 0 ; i < currentLineIndex ; i ++ ) {
118+ lineStartPos += ( lines [ i ] ?. length || 0 ) + 1 ;
119+ }
120+ setCursorPosition ( lineStartPos ) ;
121+ return ;
122+ }
125123
126- if ( key . ctrl && input === "e" ) {
127- let lineEndPos = 0 ;
128- for ( let i = 0 ; i <= currentLineIndex ; i ++ ) {
129- lineEndPos += lines [ i ] ?. length || 0 ;
130- if ( i < currentLineIndex ) lineEndPos += 1 ;
131- }
132- setCursorPosition ( lineEndPos ) ;
133- return ;
134- }
124+ if ( key . ctrl && input === "e" ) {
125+ let lineEndPos = 0 ;
126+ for ( let i = 0 ; i <= currentLineIndex ; i ++ ) {
127+ lineEndPos += lines [ i ] ?. length || 0 ;
128+ if ( i < currentLineIndex ) lineEndPos += 1 ;
129+ }
130+ setCursorPosition ( lineEndPos ) ;
131+ return ;
132+ }
135133
136- // Kill line from cursor to beginning
137- if ( key . ctrl && input === "u" ) {
138- let lineStartPos = 0 ;
139- for ( let i = 0 ; i < currentLineIndex ; i ++ ) {
140- lineStartPos += ( lines [ i ] ?. length || 0 ) + 1 ;
141- }
142- const newValue =
143- value . slice ( 0 , lineStartPos ) + value . slice ( cursorPosition ) ;
144- updateValue ( newValue , lineStartPos ) ;
145- return ;
146- }
134+ // Kill line from cursor to beginning
135+ if ( key . ctrl && input === "u" ) {
136+ let lineStartPos = 0 ;
137+ for ( let i = 0 ; i < currentLineIndex ; i ++ ) {
138+ lineStartPos += ( lines [ i ] ?. length || 0 ) + 1 ;
139+ }
140+ const newValue =
141+ value . slice ( 0 , lineStartPos ) + value . slice ( cursorPosition ) ;
142+ updateValue ( newValue , lineStartPos ) ;
143+ return ;
144+ }
147145
148- // Kill word backward
149- if ( key . ctrl && input === "w" ) {
150- const beforeCursor = value . slice ( 0 , cursorPosition ) ;
151- // Find the start of the word to delete
152- let wordStart = cursorPosition ;
146+ // Kill word backward
147+ if ( key . ctrl && input === "w" ) {
148+ const beforeCursor = value . slice ( 0 , cursorPosition ) ;
149+ // Find the start of the word to delete
150+ let wordStart = cursorPosition ;
153151
154- // Skip trailing whitespace
155- while (
156- wordStart > 0 &&
157- / \s / . test ( beforeCursor [ wordStart - 1 ] || "" )
158- ) {
159- wordStart -- ;
160- }
152+ // Skip trailing whitespace
153+ while ( wordStart > 0 && / \s / . test ( beforeCursor [ wordStart - 1 ] || "" ) ) {
154+ wordStart -- ;
155+ }
161156
162- // Find the beginning of the word
163- while (
164- wordStart > 0 &&
165- ! / \s / . test ( beforeCursor [ wordStart - 1 ] || "" )
166- ) {
167- wordStart -- ;
168- }
157+ // Find the beginning of the word
158+ while ( wordStart > 0 && ! / \s / . test ( beforeCursor [ wordStart - 1 ] || "" ) ) {
159+ wordStart -- ;
160+ }
169161
170- const newValue =
171- value . slice ( 0 , wordStart ) + value . slice ( cursorPosition ) ;
172- updateValue ( newValue , wordStart ) ;
173- return ;
174- }
162+ const newValue = value . slice ( 0 , wordStart ) + value . slice ( cursorPosition ) ;
163+ updateValue ( newValue , wordStart ) ;
164+ return ;
165+ }
175166
176- // Kill line from cursor to end
177- if ( key . ctrl && input === "k" ) {
178- let lineEndPos = 0 ;
179- for ( let i = 0 ; i <= currentLineIndex ; i ++ ) {
180- lineEndPos += lines [ i ] ?. length || 0 ;
181- if ( i < currentLineIndex ) lineEndPos += 1 ;
182- }
183- const newValue =
184- value . slice ( 0 , cursorPosition ) + value . slice ( lineEndPos ) ;
185- updateValue ( newValue , cursorPosition ) ;
186- return ;
187- }
167+ // Kill line from cursor to end
168+ if ( key . ctrl && input === "k" ) {
169+ let lineEndPos = 0 ;
170+ for ( let i = 0 ; i <= currentLineIndex ; i ++ ) {
171+ lineEndPos += lines [ i ] ?. length || 0 ;
172+ if ( i < currentLineIndex ) lineEndPos += 1 ;
173+ }
174+ const newValue = value . slice ( 0 , cursorPosition ) + value . slice ( lineEndPos ) ;
175+ updateValue ( newValue , cursorPosition ) ;
176+ return ;
177+ }
188178
189- // Clear input (like screen clear in terminal)
190- if ( key . ctrl && input === "l" ) {
191- updateValue ( "" , 0 ) ;
192- return ;
193- }
179+ // Clear input (like screen clear in terminal)
180+ if ( key . ctrl && input === "l" ) {
181+ updateValue ( "" , 0 ) ;
182+ return ;
183+ }
194184
195- // Regular character input
196- if ( input && ! key . ctrl && ! key . meta ) {
197- const newValue =
198- value . slice ( 0 , cursorPosition ) +
199- input +
200- value . slice ( cursorPosition ) ;
201- updateValue ( newValue , cursorPosition + input . length ) ;
202- }
203- } ,
204- [
205- value ,
206- cursorPosition ,
207- currentLineIndex ,
208- currentColumnIndex ,
209- lines ,
210- showCursor ,
211- onSubmit ,
212- updateValue ,
213- ] ,
214- ) ,
215- ) ;
185+ // Regular character input
186+ if ( input && ! key . ctrl && ! key . meta ) {
187+ const newValue =
188+ value . slice ( 0 , cursorPosition ) + input + value . slice ( cursorPosition ) ;
189+ updateValue ( newValue , cursorPosition + input . length ) ;
190+ }
191+ } ) ;
216192
217193 const renderText = useMemo ( ( ) => {
218194 if ( ! showCursor ) {
0 commit comments