Skip to content

Commit 03ed56a

Browse files
committed
Made history property optional
1 parent 44da291 commit 03ed56a

File tree

4 files changed

+36
-12
lines changed

4 files changed

+36
-12
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ const App = () => {
9898
export default App
9999
```
100100

101+
## History implementation
102+
103+
You can provide your own history implementation by providing `onAddHistoryItem` and `history` properties.
104+
If you don't provide `history`, up/down arrows & reverse search won't work.
105+
106+
101107
## License
102108

103109
IT © [jvorcak](https://github.com/jvorcak)

example/src/App.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ const App = () => {
119119
resolve('No command found')
120120
})}
121121
/>
122+
<p>
123+
ctrl+r, ctrl+c shortcuts as well as up/down arrows are supported.
124+
</p>
122125
<table>
123126
<tbody>
124127
<tr>
@@ -133,6 +136,10 @@ const App = () => {
133136
<td><code>history</code></td>
134137
<td>Shows a history</td>
135138
</tr>
139+
<tr>
140+
<td><code>clear</code></td>
141+
<td>Clear the terminal screen</td>
142+
</tr>
136143
</tbody>
137144
</table>
138145
</div>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@webscopeio/react-console",
3-
"version": "1.0.12",
3+
"version": "1.0.13",
44
"description": "React component that emulates console behaviour",
55
"author": "jvorcak",
66
"license": "MIT",

src/index.tsx

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ export type Props = {
2222
wrapperStyle?: object,
2323
promptStyle?: object,
2424
inputStyle?: object,
25-
history: Array<string>,
26-
onAddHistoryItem: (entry: string) => void,
25+
history?: Array<string>,
26+
onAddHistoryItem?: (entry: string) => void,
2727
}
2828

2929
type State = {
@@ -48,7 +48,6 @@ export default class ReactConsole extends React.Component<Props, State> {
4848
wrapperStyle: {},
4949
promptStyle: {},
5050
inputStyle: {},
51-
history: [],
5251
};
5352

5453
state = {
@@ -67,9 +66,11 @@ export default class ReactConsole extends React.Component<Props, State> {
6766
output: [welcomeMessage],
6867
})
6968
}
70-
this.setState({
71-
historyPosition: this.props.history.length,
72-
})
69+
if (this.props.history !== undefined) {
70+
this.setState({
71+
historyPosition: this.props.history.length,
72+
})
73+
}
7374
}
7475

7576
public clear = () => {
@@ -87,15 +88,17 @@ export default class ReactConsole extends React.Component<Props, State> {
8788
*/
8889
private getReverseHistory = (): Array<boolean> => {
8990
const {reverseSearchString} = this.state;
90-
return this.props.history.map(entry => (reverseSearchString === undefined || reverseSearchString === '') ?
91-
// @ts-ignore
92-
false : entry.includes(reverseSearchString))
91+
return this.props.history === undefined ?
92+
[]
93+
: this.props.history.map(entry => (reverseSearchString === undefined || reverseSearchString === '') ?
94+
// @ts-ignore
95+
false : entry.includes(reverseSearchString))
9396
};
9497

9598
/**
9699
* Takes current text of a main input and generates a string that will be outputted as a log.
97100
*/
98-
private getCurrentTextSnapshot = () : string => {
101+
private getCurrentTextSnapshot = (): string => {
99102
const {prompt} = this.props;
100103
const inputString: string = this.state.input;
101104
return `${prompt}\xa0${inputString}`;
@@ -269,6 +272,9 @@ export default class ReactConsole extends React.Component<Props, State> {
269272
* @param historyPosition
270273
*/
271274
private setPreviewPosition = (historyPosition: number) => {
275+
if(this.props.history === undefined) {
276+
return
277+
}
272278
this.setState({
273279
historyPosition,
274280
input: this.props.history[historyPosition] || '', // if an element history is out of bounds, we just set ''
@@ -354,11 +360,16 @@ export default class ReactConsole extends React.Component<Props, State> {
354360
this.setPreviewPosition(historyPosition);
355361
event.preventDefault()
356362
} else if (event.which === 40) {
363+
if(this.props.history === undefined) {
364+
return
365+
}
357366
const historyPosition = Math.min(this.props.history.length, this.state.historyPosition + 1);
358367
this.setPreviewPosition(historyPosition);
359368
event.preventDefault()
360369
} else if (event.which === 82 && event.ctrlKey) { // ctrl + r
361-
console.log('reverse search mode');
370+
if(this.props.history === undefined) {
371+
return
372+
}
362373
this.onReverseSearch()
363374
} else if (event.which === 67 && event.ctrlKey) { // ctrl + c
364375
this.setState({

0 commit comments

Comments
 (0)