Improve Affix target demo code style, close #4037

pull/4119/head
afc163 8 years ago
parent 82902855db
commit c7c886fda6

@ -16,36 +16,32 @@ Set a `target` for 'Affix', which is listen to scroll event of target element (d
````jsx ````jsx
import { Affix, Button } from 'antd'; import { Affix, Button } from 'antd';
const Demo = () => { class Demo extends React.Component {
return ( render() {
<div className="view-port"> return (
<div id="scrollable-container"> <div className="scrollable-container" ref={(node) => { this.container = node; }}>
<div className="background"> <div className="background">
<br /> <Affix target={() => this.container}>
<br /> <Button type="primary">
<br /> Fixed at the top of container
<Affix target={() => document.getElementById('scrollable-container')} offsetTop={20}> </Button>
<Button type="primary">Fixed at the top of container</Button>
</Affix> </Affix>
</div> </div>
</div> </div>
</div> );
); }
}; }
ReactDOM.render(<Demo />, mountNode); ReactDOM.render(<Demo />, mountNode);
```` ````
<style> <style>
#components-affix-demo-target .view-port { #components-affix-demo-target .scrollable-container {
height: 100px; height: 100px;
overflow: hidden;
}
#components-affix-demo-target #scrollable-container {
height: 100%;
overflow-y: scroll; overflow-y: scroll;
} }
#components-affix-demo-target .background { #components-affix-demo-target .background {
padding-top: 60px;
height: 300px; height: 300px;
background-image: url('https://zos.alipayobjects.com/rmsportal/RmjwQiJorKyobvI.jpg'); background-image: url('https://zos.alipayobjects.com/rmsportal/RmjwQiJorKyobvI.jpg');
} }

@ -64,6 +64,7 @@ export default class Affix extends React.Component<AffixProps, any> {
scrollEvent: any; scrollEvent: any;
resizeEvent: any; resizeEvent: any;
timeout: any;
refs: { refs: {
fixedNode: HTMLElement; fixedNode: HTMLElement;
}; };
@ -172,7 +173,10 @@ export default class Affix extends React.Component<AffixProps, any> {
componentDidMount() { componentDidMount() {
const target = this.props.target || getDefaultTarget; const target = this.props.target || getDefaultTarget;
this.setTargetEventListeners(target); // Wait for parent component ref has its value
this.timeout = setTimeout(() => {
this.setTargetEventListeners(target);
});
} }
componentWillReceiveProps(nextProps) { componentWillReceiveProps(nextProps) {
@ -187,10 +191,14 @@ export default class Affix extends React.Component<AffixProps, any> {
componentWillUnmount() { componentWillUnmount() {
this.clearScrollEventListeners(); this.clearScrollEventListeners();
clearTimeout(this.timeout);
} }
setTargetEventListeners(getTarget) { setTargetEventListeners(getTarget) {
const target = getTarget(); const target = getTarget();
if (!target) {
return;
}
this.scrollEvent = addEventListener(target, 'scroll', this.updatePosition); this.scrollEvent = addEventListener(target, 'scroll', this.updatePosition);
this.resizeEvent = addEventListener(target, 'resize', this.updatePosition); this.resizeEvent = addEventListener(target, 'resize', this.updatePosition);
} }

Loading…
Cancel
Save