all files / src/ anchor-element.js

100% Statements 139/139
98.68% Branches 75/76
100% Functions 32/32
100% Lines 13/13
34 statements, 10 functions, 34 branches Ignored     
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47                                                                     
/**
 * Provides the anchor-panel which can be linked to by `anchor`-augmented componented
 */
import React, { PropTypes as PT, Component } from 'react';
import scroller from './scroller';
import CtxTypes from './ctx-types';
import { filterUndefined } from './utils';
 
class AnchorElement extends Component {
    componentDidMount() {
        scroller.registerElementPanel(this.props.id, this);
    }
 
    componentWillUnmount() {
        scroller.unregisterElementPanel(this.props.id);
    }
 
    getConfig() {
        const containerConfig = { ...CtxTypes.defaultConfig, ...filterUndefined(this.context) };
 
        return {
            ...containerConfig,
            ...this.props
        };
    }
 
    render() {
        return React.Children.only(this.props.children);
    }
}
 
AnchorElement.defaultProps = {
    isInside: (scrollOffset, elemTopBound, elemBottomBound) =>
        (scrollOffset >= elemTopBound && scrollOffset <= elemBottomBound)
};
 
AnchorElement.propTypes = {
    id: PT.string.isRequired,
    offset: PT.number,
    children: PT.element.isRequired,
    isInside: PT.func
};
 
AnchorElement.contextTypes = CtxTypes.contextTypes;
 
export default AnchorElement;