2017-05-03 01:04:16 +01:00
|
|
|
import React from 'react';
|
2017-02-05 00:58:25 +00:00
|
|
|
import ColumnHeader from './column_header';
|
2017-04-21 19:05:35 +01:00
|
|
|
import PropTypes from 'prop-types';
|
2016-08-24 16:56:44 +01:00
|
|
|
|
2016-09-18 17:18:46 +01:00
|
|
|
const easingOutQuint = (x, t, b, c, d) => c*((t=t/d-1)*t*t*t*t + 1) + b;
|
|
|
|
|
|
|
|
const scrollTop = (node) => {
|
|
|
|
const startTime = Date.now();
|
|
|
|
const offset = node.scrollTop;
|
|
|
|
const targetY = -offset;
|
|
|
|
const duration = 1000;
|
|
|
|
let interrupt = false;
|
|
|
|
|
|
|
|
const step = () => {
|
|
|
|
const elapsed = Date.now() - startTime;
|
|
|
|
const percentage = elapsed / duration;
|
|
|
|
|
|
|
|
if (percentage > 1 || interrupt) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-06 15:18:32 +01:00
|
|
|
node.scrollTop = easingOutQuint(0, elapsed, offset, targetY, duration);
|
2016-09-18 17:18:46 +01:00
|
|
|
requestAnimationFrame(step);
|
|
|
|
};
|
|
|
|
|
|
|
|
step();
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
interrupt = true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-04-21 19:05:35 +01:00
|
|
|
class Column extends React.PureComponent {
|
2016-08-31 15:15:12 +01:00
|
|
|
|
2017-05-12 13:44:10 +01:00
|
|
|
static propTypes = {
|
|
|
|
heading: PropTypes.string,
|
|
|
|
icon: PropTypes.string,
|
|
|
|
children: PropTypes.node,
|
|
|
|
active: PropTypes.bool,
|
2017-05-20 16:31:47 +01:00
|
|
|
hideHeadingOnMobile: PropTypes.bool,
|
2017-05-12 13:44:10 +01:00
|
|
|
};
|
2016-08-31 15:15:12 +01:00
|
|
|
|
2017-05-12 13:44:10 +01:00
|
|
|
handleHeaderClick = () => {
|
2017-05-03 01:04:16 +01:00
|
|
|
const scrollable = this.node.querySelector('.scrollable');
|
2017-04-11 20:58:28 +01:00
|
|
|
if (!scrollable) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._interruptScrollAnimation = scrollTop(scrollable);
|
2017-04-21 19:05:35 +01:00
|
|
|
}
|
2016-09-18 17:18:46 +01:00
|
|
|
|
2017-05-12 13:44:10 +01:00
|
|
|
handleWheel = () => {
|
2016-09-18 17:18:46 +01:00
|
|
|
if (typeof this._interruptScrollAnimation !== 'undefined') {
|
|
|
|
this._interruptScrollAnimation();
|
|
|
|
}
|
2017-04-21 19:05:35 +01:00
|
|
|
}
|
2016-09-18 17:18:46 +01:00
|
|
|
|
2017-05-12 13:44:10 +01:00
|
|
|
setRef = (c) => {
|
2017-05-03 01:04:16 +01:00
|
|
|
this.node = c;
|
|
|
|
}
|
|
|
|
|
2016-08-31 15:15:12 +01:00
|
|
|
render () {
|
2017-04-21 17:17:55 +01:00
|
|
|
const { heading, icon, children, active, hideHeadingOnMobile } = this.props;
|
2017-02-05 00:58:25 +00:00
|
|
|
|
2017-05-20 16:31:47 +01:00
|
|
|
let columnHeaderId = null;
|
2016-09-10 17:36:48 +01:00
|
|
|
let header = '';
|
|
|
|
|
2017-02-05 00:58:25 +00:00
|
|
|
if (heading) {
|
2017-05-20 16:31:47 +01:00
|
|
|
columnHeaderId = heading.replace(/ /g, '-');
|
2017-04-22 16:30:35 +01:00
|
|
|
header = <ColumnHeader icon={icon} active={active} type={heading} onClick={this.handleHeaderClick} hideOnMobile={hideHeadingOnMobile} columnHeaderId={columnHeaderId}/>;
|
2016-09-10 17:36:48 +01:00
|
|
|
}
|
2017-02-05 03:11:14 +00:00
|
|
|
return (
|
2017-05-03 01:04:16 +01:00
|
|
|
<div
|
|
|
|
ref={this.setRef}
|
|
|
|
role='region'
|
|
|
|
aria-labelledby={columnHeaderId}
|
|
|
|
className='column'
|
|
|
|
onWheel={this.handleWheel}>
|
2016-09-10 17:36:48 +01:00
|
|
|
{header}
|
2017-02-05 00:58:25 +00:00
|
|
|
{children}
|
2016-08-24 16:56:44 +01:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2016-08-31 15:15:12 +01:00
|
|
|
|
2017-04-21 19:05:35 +01:00
|
|
|
}
|
|
|
|
|
2017-02-05 03:11:14 +00:00
|
|
|
export default Column;
|