Skip to content

Commit

Permalink
[added] affix state callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Nov 17, 2015
1 parent 72a351a commit 4eabbfc
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
22 changes: 20 additions & 2 deletions src/Affix.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,18 @@ class Affix extends React.Component {
return;
}

this.setState({affixed, position, top});
let upperName = affixed === 'affix'
? '' : affixed.charAt(0).toUpperCase() + affixed.substr(1);

if (this.props['onAffix' + upperName]) {
this.props['onAffix' + upperName]();
}

this.setState({affixed, position, top}, ()=>{
if (this.props['onAffixed' + upperName]) {
this.props['onAffixed' + upperName]();
}
});
}

updateStateAtBottom() {
Expand Down Expand Up @@ -201,7 +212,14 @@ Affix.propTypes = {
/**
* Style to apply when at bottom
*/
bottomStyle: React.PropTypes.object
bottomStyle: React.PropTypes.object,

onAffix: React.PropTypes.func,
onAffixed: React.PropTypes.func,
onAffixTop: React.PropTypes.func,
onAffixedTop: React.PropTypes.func,
onAffixBottom: React.PropTypes.func,
onAffixedBottom: React.PropTypes.func
};

Affix.defaultProps = {
Expand Down
36 changes: 30 additions & 6 deletions test/AffixSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { render } from './helpers';

describe('<Affix>', () => {
let mountPoint;
let handlers;

// This makes the window very tall; hopefully enough to exhibit the affix
// behavior. If this is insufficient, we should modify the Karma config to
Expand Down Expand Up @@ -41,9 +42,18 @@ describe('<Affix>', () => {
Content.renderCount = 0;
mountPoint = document.createElement('div');
document.body.appendChild(mountPoint);
handlers = {
onAffix: sinon.spy(),
onAffixed: sinon.spy(),
onAffixTop: sinon.spy(),
onAffixedTop: sinon.spy(),
onAffixBottom: sinon.spy(),
onAffixedBottom: sinon.spy()
};
});

afterEach(() => {
Object.keys(handlers).forEach(key => handlers[key].reset());
ReactDOM.unmountComponentAtNode(mountPoint);
document.body.removeChild(mountPoint);
window.scrollTo(0, 0);
Expand All @@ -58,6 +68,7 @@ describe('<Affix>', () => {

const content =
ReactTestUtils.findRenderedComponentWithType(instance, Content);

expect(content).to.exist;
});

Expand All @@ -76,6 +87,7 @@ describe('<Affix>', () => {
affixStyle={{color: 'white'}}
bottomClassName="affix-bottom"
bottomStyle={{color: 'blue'}}
{...handlers}
>
<Content style={{height: 100}} />
</Affix>
Expand All @@ -88,14 +100,19 @@ describe('<Affix>', () => {
});

it('should render correctly at top', (done) => {
window.scrollTo(0, 99);
window.scrollTo(0, 101);

requestAnimationFrame(() => {
expect(node.className).to.equal('affix-top');
expect(node.style.position).to.not.be.ok;
expect(node.style.top).to.not.be.ok;
expect(node.style.color).to.equal('red');
done();
window.scrollTo(0, 99);
requestAnimationFrame(() => {
expect(node.className).to.equal('affix-top');
expect(node.style.position).to.not.be.ok;
expect(node.style.top).to.not.be.ok;
expect(node.style.color).to.equal('red');
expect(handlers.onAffixTop).to.been.calledOnce;
expect(handlers.onAffixedTop).to.been.calledOnce;
done();
});
});
});

Expand All @@ -106,6 +123,9 @@ describe('<Affix>', () => {
expect(node.style.position).to.equal('fixed');
expect(node.style.top).to.not.be.ok;
expect(node.style.color).to.equal('white');

expect(handlers.onAffix).to.been.calledOnce;
expect(handlers.onAffixed).to.been.calledOnce;
done();
});
});
Expand All @@ -117,6 +137,9 @@ describe('<Affix>', () => {
expect(node.style.position).to.equal('absolute');
expect(node.style.top).to.equal('9900px');
expect(node.style.color).to.equal('blue');

expect(handlers.onAffixBottom).to.been.calledOnce;
expect(handlers.onAffixedBottom).to.been.calledOnce;
done();
});
});
Expand All @@ -131,6 +154,7 @@ describe('<Affix>', () => {
<Affix
offsetTop={100}
viewportOffsetTop={50}
{...handlers}
>
<Content />
</Affix>
Expand Down

0 comments on commit 4eabbfc

Please sign in to comment.