@@ -17,11 +17,11 @@ export type Props = {
1717type  State  =  { 
1818  output : Array < string > , 
1919  commandInProgress : boolean , 
20+   input : string , 
2021} 
2122
2223export  default  class  ReactConsole  extends  React . Component < Props ,  State >  { 
2324
24-   formRef : any  =  null ; 
2525  inputRef : any  =  null ; 
2626  wrapperRef : any  =  null ; 
2727
@@ -31,6 +31,7 @@ export default class ReactConsole extends React.Component<Props, State> {
3131  } ; 
3232
3333  state  =  { 
34+     input : '' , 
3435    output : [ ] , 
3536    commandInProgress : false , 
3637  } ; 
@@ -44,33 +45,29 @@ export default class ReactConsole extends React.Component<Props, State> {
4445    } 
4546  } 
4647
47- 
48-   scrollToBottom  =  ( )  =>  { 
49-     this . wrapperRef . scrollTo ( 0 ,  this . wrapperRef . scrollHeight ) 
50-   } ; 
51- 
5248  onSubmit  =  async  ( e : any )  =>  { 
53-     const  { prompt}  =  this . props 
49+     const  { prompt}  =  this . props ; 
5450    e . preventDefault ( ) ; 
55-      const   data   =   new   FormData ( e . target ) ; 
56-     const  inputString : string  =  data . get ( ' input' )   as   string ; 
51+ 
52+     const  inputString : string  =  this . state . input 
5753    if  ( inputString  ===  null )  { 
5854      return 
5955    } 
6056
6157    const  log  =  `${ prompt } ${ inputString }  ; 
6258
6359    if ( inputString  ===  '' )  { 
64-       this . setState ( {  output : [ ...this . state . output ,  log ] } ) 
65-       this . formRef . reset ( ) 
60+       this . setState ( { 
61+         output : [ ...this . state . output ,  log ] , 
62+         input : '' , 
63+       } ) ; 
6664      return 
6765    } 
6866
6967    const  [ cmd ,  ...args ]  =  inputString . split ( " " ) ; 
7068
7169    if ( cmd  ===  'clear' )  { 
72-       this . formRef . reset ( ) 
73-       this . setState ( { output : [ ] } ) 
70+       this . setState ( { output : [ ] ,  input : '' } ) 
7471      return 
7572    } 
7673
@@ -88,15 +85,13 @@ export default class ReactConsole extends React.Component<Props, State> {
8885      }  catch  ( err )  { 
8986
9087      } 
91-       this . scrollToBottom ( ) 
9288
9389    }  else  { 
9490      this . setState ( { 
9591        output : [ ...this . state . output ,  log ,  `Command '${ cmd }  ] 
96-       } ,   this . scrollToBottom ) 
92+       } ) 
9793    } 
98-     this . formRef . reset ( ) 
99-     this . setState ( { commandInProgress : false } ) ; 
94+     this . setState ( { commandInProgress : false ,  input : '' } ) ; 
10095    this . inputRef . focus ( ) 
10196  } ; 
10297
@@ -114,7 +109,6 @@ export default class ReactConsole extends React.Component<Props, State> {
114109          ) } 
115110        </ div > 
116111        < form 
117-           ref = { ref  =>  this . formRef  =  ref } 
118112          onSubmit = { this . onSubmit } 
119113        > 
120114          < div  className = { styles . promptWrapper } > 
@@ -123,6 +117,8 @@ export default class ReactConsole extends React.Component<Props, State> {
123117              disabled = { this . state . commandInProgress } 
124118              ref = { ref  =>  this . inputRef  =  ref } 
125119              autoFocus = { autoFocus } 
120+               value = { this . state . input } 
121+               onChange = { this . onInputChange } 
126122              autoComplete = { 'off' } 
127123              spellCheck = { false } 
128124              autoCapitalize = { 'false' } 
@@ -135,6 +131,12 @@ export default class ReactConsole extends React.Component<Props, State> {
135131    ) 
136132  } 
137133
134+   onInputChange  =  ( e : any )  =>  { 
135+     this . setState ( { 
136+       input : e . target . value , 
137+     } ) 
138+   } 
139+ 
138140  focusConsole  =  ( )  =>  { 
139141    this . inputRef . focus ( ) 
140142  } 
0 commit comments