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