ant-design/components/affix/index.tsx

208 lines
5.5 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
import * as ReactDOM from 'react-dom';
2019-03-03 10:04:21 +08:00
import { polyfill } from 'react-lifecycles-compat';
2018-08-07 21:07:52 +08:00
import * as PropTypes from 'prop-types';
2016-06-16 22:34:54 +08:00
import addEventListener from 'rc-util/lib/Dom/addEventListener';
import classNames from 'classnames';
2016-07-09 16:00:05 +08:00
import shallowequal from 'shallowequal';
2016-09-14 16:59:45 +08:00
import omit from 'omit.js';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
import getScroll from '../_util/getScroll';
import { throttleByAnimationFrameDecorator } from '../_util/throttleByAnimationFrame';
2015-08-04 14:57:18 +08:00
2019-03-03 10:04:21 +08:00
import warning from '../_util/warning';
import { addObserveTarget, removeObserveTarget, getTargetRect, getOffset } from './utils';
function getDefaultTarget() {
2018-05-23 21:03:32 +08:00
return typeof window !== 'undefined' ? window : null;
}
2016-06-22 13:18:43 +08:00
// Affix
export interface AffixProps {
/**
*
*/
2016-07-13 11:14:24 +08:00
offsetTop?: number;
offset?: number;
/** 距离窗口底部达到指定偏移量后触发 */
2016-07-13 11:14:24 +08:00
offsetBottom?: number;
style?: React.CSSProperties;
/** 固定状态改变时触发的回调函数 */
onChange?: (affixed?: boolean) => void;
/** 设置 Affix 需要监听其滚动事件的元素,值为一个返回对应 DOM 元素的函数 */
2017-11-22 11:49:35 +08:00
target?: () => Window | HTMLElement | null;
prefixCls?: string;
2019-01-31 11:20:58 +08:00
className?: string;
2019-03-03 10:04:21 +08:00
// TODO: Remove me!
debug?: boolean;
}
enum AffixStatus {
None,
Prepare,
2016-06-22 13:18:43 +08:00
}
2017-11-22 11:49:35 +08:00
export interface AffixState {
2019-03-03 10:04:21 +08:00
affixStyle?: React.CSSProperties;
placeholderStyle?: React.CSSProperties;
status: AffixStatus;
lastAffix: boolean;
2017-11-22 11:49:35 +08:00
}
2019-03-03 10:04:21 +08:00
class Affix extends React.Component<AffixProps, AffixState> {
static defaultProps = {
target: getDefaultTarget,
2016-08-23 21:00:35 +08:00
};
2015-09-01 16:18:46 +08:00
2018-11-01 02:46:42 +08:00
state: AffixState = {
2019-03-03 10:04:21 +08:00
status: AffixStatus.None,
lastAffix: false,
2018-11-01 02:46:42 +08:00
};
2016-07-13 17:22:23 +08:00
2018-11-01 02:46:42 +08:00
private timeout: number;
2019-03-03 10:04:21 +08:00
placeholderNode: HTMLDivElement;
fixedNode: HTMLDivElement;
2017-08-17 11:45:46 +08:00
2019-03-03 10:04:21 +08:00
// Event handler
componentDidMount() {
if (!this.props.debug) return;
const { target } = this.props;
if (target) {
// Wait for parent component ref has its value
this.timeout = setTimeout(() => {
addObserveTarget(target(), this);
});
2016-07-09 16:00:05 +08:00
}
}
2019-03-03 10:04:21 +08:00
componentDidUpdate(prevProps: AffixProps) {
if (!this.props.debug) return;
const { target } = this.props;
if (prevProps.target !== target) {
removeObserveTarget(this);
if (target) {
addObserveTarget(target(), this);
}
2016-07-11 14:52:21 +08:00
}
2019-03-03 10:04:21 +08:00
this.measure();
}
2019-03-03 10:04:21 +08:00
componentWillUnmount() {
clearTimeout(this.timeout);
removeObserveTarget(this);
}
2015-08-03 16:49:42 +08:00
2019-03-03 10:04:21 +08:00
savePlaceholderNode = (node: HTMLDivElement) => {
this.placeholderNode = node;
};
2019-03-03 10:04:21 +08:00
saveFixedNode = (node: HTMLDivElement) => {
this.fixedNode = node;
};
2015-08-03 16:49:42 +08:00
2019-03-03 10:04:21 +08:00
// =================== Measure ===================
// Handle realign logic
@throttleByAnimationFrameDecorator()
updatePosition(e: Event) {
console.log('???', e.target);
this.setState({
status: AffixStatus.Prepare,
affixStyle: undefined,
placeholderStyle: undefined,
});
}
2015-08-03 16:49:42 +08:00
2019-03-03 10:04:21 +08:00
measure = () => {
const { status } = this.state;
const { target, offset, offsetBottom } = this.props;
if (status !== AffixStatus.Prepare || !this.fixedNode || !this.placeholderNode || !target) {
return;
2015-08-04 15:03:00 +08:00
}
2019-03-03 10:04:21 +08:00
let { offsetTop } = this.props;
if (typeof offsetTop === 'undefined') {
offsetTop = offset;
warning(
typeof offset === 'undefined',
'Affix',
'`offset` is deprecated. Please use `offsetTop` instead.',
);
}
2019-03-03 10:04:21 +08:00
const targetNode = target();
if (!targetNode) {
return;
}
2019-03-03 10:04:21 +08:00
const newState: Partial<AffixState> = {
status: AffixStatus.None,
};
const targetRect = getTargetRect(targetNode);
const placeholderReact = getTargetRect(this.placeholderNode);
2019-03-03 10:04:21 +08:00
console.log('>>>', targetRect.top, placeholderReact.top, offsetTop);
// TODO: inner content scroll
// const scrollTop = getScroll(targetNode, true);
if (offsetTop !== undefined && targetRect.top > placeholderReact.top - offsetTop) {
newState.affixStyle = {
position: 'fixed',
top: offsetTop,
};
newState.placeholderStyle = {
width: placeholderReact.width,
height: placeholderReact.height,
};
} else if (offsetBottom !== undefined && targetRect.bottom < placeholderReact.bottom + offsetBottom) {
newState.affixStyle = {
position: 'fixed',
bottom: offsetBottom,
};
newState.placeholderStyle = {
width: placeholderReact.width,
height: placeholderReact.height,
};
}
2019-03-03 10:04:21 +08:00
this.setState(newState as AffixState);
}
2019-03-03 10:04:21 +08:00
// =================== Render ===================
renderAffix = ({ getPrefixCls }: ConfigConsumerProps) => {
2019-03-03 10:04:21 +08:00
const { affixStyle, placeholderStyle, status } = this.state;
const { prefixCls, style, children } = this.props;
const className = classNames({
2019-03-03 10:04:21 +08:00
[getPrefixCls('affix', prefixCls)]: affixStyle,
});
2015-08-03 16:49:42 +08:00
2018-12-07 20:02:01 +08:00
const props = omit(this.props, [
'prefixCls',
'offsetTop',
'offsetBottom',
'target',
'onChange',
2019-03-03 10:04:21 +08:00
'debug',
2018-12-07 20:02:01 +08:00
]);
2019-03-03 10:04:21 +08:00
const mergedPlaceholderStyle = {
...(status === AffixStatus.None ? placeholderStyle : null),
...style
};
2015-08-03 16:49:42 +08:00
return (
2019-03-03 10:04:21 +08:00
<div {...props} style={mergedPlaceholderStyle} ref={this.savePlaceholderNode}>
<div className={className} ref={this.saveFixedNode} style={this.state.affixStyle}>
2019-03-03 10:04:21 +08:00
{children}
2015-08-04 14:57:18 +08:00
</div>
2015-08-03 16:49:42 +08:00
</div>
);
};
render() {
2018-12-07 20:02:01 +08:00
return <ConfigConsumer>{this.renderAffix}</ConfigConsumer>;
}
}
2019-03-03 10:04:21 +08:00
polyfill(Affix);
export default Affix;