2016-07-17 23:06:43 +00:00
|
|
|
import React, { PropTypes } from 'react';
|
2016-07-21 04:33:41 +00:00
|
|
|
import InlineSVG from 'react-inlinesvg';
|
2017-02-22 19:29:35 +00:00
|
|
|
import classNames from 'classnames';
|
|
|
|
|
2016-07-21 04:33:41 +00:00
|
|
|
const upArrowUrl = require('../../../images/up-arrow.svg');
|
|
|
|
const downArrowUrl = require('../../../images/down-arrow.svg');
|
2016-07-17 23:06:43 +00:00
|
|
|
|
|
|
|
class Console extends React.Component {
|
2016-07-21 04:05:47 +00:00
|
|
|
componentDidUpdate() {
|
2017-02-22 19:29:35 +00:00
|
|
|
this.consoleMessages.scrollTop = this.consoleMessages.scrollHeight;
|
2016-07-21 04:05:47 +00:00
|
|
|
}
|
|
|
|
|
2016-07-18 00:49:57 +00:00
|
|
|
render() {
|
2016-07-21 04:33:41 +00:00
|
|
|
const consoleClass = classNames({
|
|
|
|
'preview-console': true,
|
|
|
|
'preview-console--collapsed': !this.props.isExpanded
|
|
|
|
});
|
2016-07-17 23:06:43 +00:00
|
|
|
|
|
|
|
return (
|
2017-02-22 19:29:35 +00:00
|
|
|
<div className={consoleClass} role="main" tabIndex="0" title="console">
|
2016-07-21 04:33:41 +00:00
|
|
|
<div className="preview-console__header">
|
2017-01-11 19:13:49 +00:00
|
|
|
<h2 className="preview-console__header-title">Console</h2>
|
2017-01-09 23:09:23 +00:00
|
|
|
<div className="preview-console__header-buttons">
|
2017-02-22 19:29:35 +00:00
|
|
|
<button className="preview-console__clear" onClick={this.props.clearConsole} aria-label="clear console">
|
2017-01-09 23:09:23 +00:00
|
|
|
Clear
|
2017-02-22 19:29:35 +00:00
|
|
|
</button>
|
2017-06-06 02:33:32 +00:00
|
|
|
<button
|
|
|
|
className="preview-console__collapse"
|
|
|
|
onClick={this.props.collapseConsole}
|
|
|
|
aria-label="collapse console"
|
|
|
|
>
|
2017-01-09 23:09:23 +00:00
|
|
|
<InlineSVG src={downArrowUrl} />
|
|
|
|
</button>
|
|
|
|
<button className="preview-console__expand" onClick={this.props.expandConsole} aria-label="expand console">
|
|
|
|
<InlineSVG src={upArrowUrl} />
|
|
|
|
</button>
|
|
|
|
</div>
|
2016-07-21 04:33:41 +00:00
|
|
|
</div>
|
2017-02-22 19:29:35 +00:00
|
|
|
<div ref={(element) => { this.consoleMessages = element; }} className="preview-console__messages">
|
|
|
|
{this.props.consoleEvents.map((consoleEvent) => {
|
2017-01-11 19:13:49 +00:00
|
|
|
const args = consoleEvent.arguments;
|
|
|
|
const method = consoleEvent.method;
|
|
|
|
if (Object.keys(args).length === 0) {
|
|
|
|
return (
|
2017-02-22 19:29:35 +00:00
|
|
|
<div key={consoleEvent.id} className="preview-console__undefined">
|
|
|
|
<span key={`${consoleEvent.id}-0`}>{'undefined'}</span>
|
2017-01-11 19:13:49 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return (
|
2017-02-22 19:29:35 +00:00
|
|
|
<div key={consoleEvent.id} className={`preview-console__${method}`}>
|
|
|
|
{Object.keys(args).map(key => <span key={`${consoleEvent.id}-${key}`}>{args[key]}</span>)}
|
2017-01-11 19:13:49 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
})}
|
2016-07-21 04:05:47 +00:00
|
|
|
</div>
|
2016-07-17 23:06:43 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Console.propTypes = {
|
2017-02-22 19:29:35 +00:00
|
|
|
consoleEvents: PropTypes.arrayOf(PropTypes.shape({
|
|
|
|
method: PropTypes.string.isRequired,
|
|
|
|
args: PropTypes.arrayOf(PropTypes.string)
|
|
|
|
})),
|
2016-07-21 04:33:41 +00:00
|
|
|
isExpanded: PropTypes.bool.isRequired,
|
|
|
|
collapseConsole: PropTypes.func.isRequired,
|
2016-10-06 17:01:48 +00:00
|
|
|
expandConsole: PropTypes.func.isRequired,
|
2017-01-11 19:13:49 +00:00
|
|
|
clearConsole: PropTypes.func.isRequired
|
2016-07-17 23:06:43 +00:00
|
|
|
};
|
|
|
|
|
2017-02-22 19:29:35 +00:00
|
|
|
Console.defaultProps = {
|
|
|
|
consoleEvents: []
|
|
|
|
};
|
|
|
|
|
2016-07-17 23:06:43 +00:00
|
|
|
export default Console;
|