-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
context.jsx
91 lines (79 loc) · 1.98 KB
/
context.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import React, {Component, createContext, useContext} from 'react';
import blessed from 'blessed';
import {render} from '../src';
// this is a bit weird, since the context provider & consumer and components are all in the same file
// normally these would all be in different places, so the provider's children can grab the context from anywhere
const DemoContext = createContext();
const {Provider} = DemoContext;
// app-level provider of demo context
class DemoProvider extends Component {
constructor(props) {
super(props);
this.state = {
demo: 0
};
this.setDemo = this.setDemo.bind(this);
}
setDemo(value) {
this.setState({...this.state, demo: value});
}
render() {
return (
<Provider value={{demo: this.state.demo, setDemo: this.setDemo}}>
{this.props.children}
</Provider>
);
}
}
// wrap a component with demo context consumer
const withDemo = Component => props => {
const context = useContext(DemoContext);
return <Component {...props} {...context} />;
};
class AppInner extends Component {
render() {
const {demo, setDemo} = this.props;
return (
<box
label="react-blessed context demo"
border={{type: 'line'}}
style={{border: {fg: 'cyan'}}}>
{demo}
<button
mouse
border={{type: 'line'}}
height={3}
width={3}
top={2}
left={4}
onPress={a => setDemo(demo + 1)}>
+
</button>
<button
mouse
border={{type: 'line'}}
height={3}
width={3}
top={2}
onPress={a => setDemo(demo - 1)}>
-
</button>
</box>
);
}
}
const App = withDemo(AppInner);
const screen = blessed.screen({
autoPadding: true,
smartCSR: true,
title: 'react-blessed context demo'
});
screen.key(['escape', 'q', 'C-c'], function (ch, key) {
return process.exit(0);
});
render(
<DemoProvider>
<App />
</DemoProvider>,
screen
);