@@ -10,6 +10,8 @@ export type Props = {
1010 text : string ,
1111 prompt : React . Component ,
1212 commands : any ,
13+ welcomeMessage ?: string ,
14+ autoFocus : boolean ,
1315}
1416
1517type State = {
@@ -19,37 +21,63 @@ type State = {
1921
2022export default class ReactConsole extends React . Component < Props , State > {
2123
22- formRef : any = null ;
24+ formRef : any = null ;
2325 inputRef : any = null ;
2426 wrapperRef : any = null ;
2527
2628 static defaultProps = {
2729 prompt : '$' ,
30+ autoFocus : false ,
2831 } ;
2932
3033 state = {
3134 output : [ ] ,
3235 commandInProgress : false ,
3336 } ;
3437
38+ componentDidMount ( ) {
39+ const { welcomeMessage} = this . props
40+ if ( welcomeMessage ) {
41+ this . setState ( {
42+ output : [ welcomeMessage ] ,
43+ } )
44+ }
45+ }
46+
47+
3548 scrollToBottom = ( ) => {
36- this . wrapperRef . scrollTo ( 0 , this . wrapperRef . scrollHeight )
49+ this . wrapperRef . scrollTo ( 0 , this . wrapperRef . scrollHeight )
3750 } ;
3851
3952 onSubmit = async ( e : any ) => {
40- const { prompt } = this . props
53+ const { prompt} = this . props
4154 e . preventDefault ( ) ;
4255 const data = new FormData ( e . target ) ;
4356 const inputString : string = data . get ( 'input' ) as string ;
4457 if ( inputString === null ) {
4558 return
4659 }
60+
61+ const log = `${ prompt } \xa0${ inputString } ` ;
62+
63+ if ( inputString === '' ) {
64+ this . setState ( { output : [ ...this . state . output , log ] } )
65+ this . formRef . reset ( )
66+ return
67+ }
68+
4769 const [ cmd , ...args ] = inputString . split ( " " ) ;
70+
71+ if ( cmd === 'clear' ) {
72+ this . formRef . reset ( )
73+ this . setState ( { output : [ ] } )
74+ return
75+ }
76+
4877 const command = this . props . commands [ cmd ] ;
4978
50- const log = `${ prompt } \xa0${ inputString } ` ;
5179
52- await this . setState ( { commandInProgress : true } ) ;
80+ await this . setState ( { commandInProgress : true } ) ;
5381
5482 if ( command ) {
5583 try {
@@ -67,25 +95,22 @@ export default class ReactConsole extends React.Component<Props, State> {
6795 output : [ ...this . state . output , log , `Command '${ cmd } ' does not exist` ]
6896 } , this . scrollToBottom )
6997 }
70- if ( this . formRef ) {
71- this . formRef . reset ( )
72- }
73- this . setState ( { commandInProgress : false } ) ;
98+ this . formRef . reset ( )
99+ this . setState ( { commandInProgress : false } ) ;
74100 this . inputRef . focus ( )
75101 } ;
76102
77103 render ( ) {
78104 const {
79105 prompt,
106+ autoFocus,
80107 } = this . props ;
81108
82109 return (
83110 < div className = { styles . wrapper } onClick = { this . focusConsole } ref = { ref => this . wrapperRef = ref } >
84- < div
85- className = { styles . output }
86- >
111+ < div >
87112 { this . state . output . map ( ( line , key ) =>
88- < div key = { key } > { line } </ div >
113+ < pre key = { key } className = { styles . line } > { line } </ pre >
89114 ) }
90115 </ div >
91116 < form
@@ -97,7 +122,7 @@ export default class ReactConsole extends React.Component<Props, State> {
97122 < input
98123 disabled = { this . state . commandInProgress }
99124 ref = { ref => this . inputRef = ref }
100- autoFocus
125+ autoFocus = { autoFocus }
101126 autoComplete = { 'off' }
102127 spellCheck = { false }
103128 autoCapitalize = { 'false' }
0 commit comments