generated from storybookjs/addon-kit
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLogger.js
76 lines (63 loc) · 1.55 KB
/
Logger.js
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
import React, { Component } from "react";
import json from "format-json";
import PropTypes from "prop-types";
import { styled } from "@storybook/theming";
import EventEmitter from "eventemitter3";
import uuid from "uuid-browser/v4";
const Wrapper = styled.div({
padding: 20,
});
const Title = styled.h1({
margin: 0,
});
const Item = styled.div({
listStyle: "none",
marginBottom: 10,
});
export default class Logger extends Component {
state = {
events: [],
};
static propTypes = {
emitter: PropTypes.instanceOf(EventEmitter).isRequired,
title: PropTypes.string,
};
static defaultProps = {
title: "Logger",
};
componentDidMount() {
const { emitter } = this.props;
emitter.on(Logger.LOG_EVENT, this.onEventHandler);
}
componentWillUnmount() {
const { emitter } = this.props;
emitter.removeListener(Logger.LOG_EVENT, this.onEventHandler);
}
onEventHandler = ({ name, payload }) => {
this.setState(({ events }) => ({
events: [...events, { name, id: uuid(), payload }],
}));
};
static LOG_EVENT = "Logger:log";
render() {
const { events } = this.state;
const { title } = this.props;
return (
<Wrapper>
<Title>{title}</Title>
<dl>
{events.map(({ id, name, payload }) => (
<Item key={id}>
<dt>
<b>Event name:</b> {name}
</dt>
<dd>
<b>Event payload:</b> {json.plain(payload)}
</dd>
</Item>
))}
</dl>
</Wrapper>
);
}
}