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