ocrcc-chatbox/src/components/widget.js

99 lines
2.1 KiB
JavaScript
Raw Normal View History

2018-05-29 02:49:38 +00:00
import React, { Component } from 'react';
import PropTypes from 'prop-types';
2018-05-28 01:09:44 +00:00
import { Transition } from 'react-transition-group';
import './widget.scss';
2018-05-26 18:53:54 +00:00
2018-05-29 02:49:38 +00:00
class Widget extends Component {
2018-05-26 18:53:54 +00:00
state = {
opened: false,
2018-05-28 01:09:44 +00:00
showDock: true,
2018-05-26 18:53:54 +00:00
}
2018-05-28 01:09:44 +00:00
handleToggleOpen = () => {
this.setState((prev) => {
2018-05-29 02:49:38 +00:00
let { showDock } = prev;
2018-05-28 01:09:44 +00:00
if (!prev.opened) {
showDock = false;
}
return {
showDock,
opened: !prev.opened,
};
});
2018-05-26 18:53:54 +00:00
}
2018-05-28 01:09:44 +00:00
handleWidgetExit = () => {
this.setState({
showDock: true,
});
}
renderBody = () => {
const { showDock } = this.state;
if (!showDock) return '';
return (
<button
type="button"
className="dock"
onClick={this.handleToggleOpen}
onKeyPress={this.handleToggleOpen}
>
^ OPEN ^
</button>
);
2018-05-28 01:09:44 +00:00
}
render() {
const { opened } = this.state;
2018-05-29 02:49:38 +00:00
const body = this.renderBody();
const { bodyText, headerText, footerText } = this.props;
2018-05-28 01:09:44 +00:00
2018-05-26 18:53:54 +00:00
return (
2018-05-28 01:09:44 +00:00
<div className="docked-widget">
<Transition in={opened} timeout={250} onExited={this.handleWidgetExit}>
2018-05-29 02:49:38 +00:00
{status => (
2018-05-28 01:09:44 +00:00
<div className={`widget widget-${status}`}>
<div className="widget-header">
<div className="widget-header-title">
{headerText}
2018-05-28 01:09:44 +00:00
</div>
<button
type="button"
className="widget-header-icon"
onClick={this.handleToggleOpen}
onKeyPress={this.handleToggleOpen}
>
2018-05-28 01:09:44 +00:00
X
</button>
2018-05-28 01:09:44 +00:00
</div>
<div className="widget-body">
{bodyText}
2018-05-28 01:09:44 +00:00
</div>
<div className="widget-footer">
{footerText}
2018-05-28 01:09:44 +00:00
</div>
</div>
)}
</Transition>
2018-05-26 18:53:54 +00:00
{body}
</div>
);
}
}
Widget.propTypes = {
headerText: PropTypes.string,
bodyText: PropTypes.string,
footerText: PropTypes.string,
};
Widget.defaultProps = {
headerText: 'Header',
bodyText: 'Body',
footerText: 'Footer',
};
2018-05-26 18:53:54 +00:00
export default Widget;