Skip to content

Commit 2d18dc3

Browse files
committed
Some details & styling
1 parent c4e1d38 commit 2d18dc3

File tree

4 files changed

+60
-20
lines changed

4 files changed

+60
-20
lines changed

example/src/App.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ export default class App extends Component {
77
return (
88
<div>
99
<ReactConsole
10+
autoFocus
11+
welcomeMessage="Welcome"
1012
commands={{
1113
echo: {
1214
description: 'Echo',
@@ -17,6 +19,16 @@ export default class App extends Component {
1719
}, 2000)
1820
})
1921
}
22+
},
23+
test: {
24+
description: 'Test',
25+
fn: (...args) => {
26+
return new Promise((resolve, reject) => {
27+
setTimeout(() => {
28+
resolve('Hello world \n\n hello \n')
29+
}, 2000)
30+
})
31+
}
2032
}
2133
}}
2234
/>

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "react-console",
3-
"version": "1.0.0",
2+
"name": "@webscopeio/react-console",
3+
"version": "1.0.3",
44
"description": "React component that emulates console behaviour",
55
"author": "jvorcak",
66
"license": "MIT",

src/index.tsx

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1517
type State = {
@@ -19,37 +21,63 @@ type State = {
1921

2022
export 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'}

src/styles.css

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,19 @@
1212
overflow-y: auto;
1313
}
1414

15-
.output {
16-
}
17-
1815
.promptWrapper {
1916
display: flex;
2017
}
2118

19+
.line {
20+
font-size: 13px;
21+
line-height: 13px;
22+
background: transparent !important; /* we want to make this transparent whatever happens in the app*/
23+
}
24+
2225
.input {
2326
flex: 1;
24-
background: transparent;
27+
background: transparent !important; /* we want to make this transparent whatever happens in the app*/
2528
border: none;
2629
outline: none;
2730
color: white;

0 commit comments

Comments
 (0)