Skip to content

Commit

Permalink
Merge branch 'IPradyumnaDebnath-Back-button-issue-Option-to-user-to-p…
Browse files Browse the repository at this point in the history
…ush-or-replace-history'
  • Loading branch information
fisshy committed Aug 11, 2020
2 parents f182f3c + 10bd86e commit 8e5b058
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 42 deletions.
2 changes: 1 addition & 1 deletion examples/with-hash/app.js
Expand Up @@ -50,7 +50,7 @@ class Section extends React.Component{
<div className="container-fluid">
<div className="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul className="nav navbar-nav">
<li><Link containerId="scroll-container" activeClass="active" className="test1" to="test1" spy={true} hashSpy={true} duration={500} >Test 1</Link></li>
<li><Link containerId="scroll-container" activeClass="active" className="test1" to="test1" spy={true} hashSpy={true} duration={500} saveHashHistory={false} >Test 1</Link></li>
<li><Link containerId="scroll-container" activeClass="active" className="test2" to="test2" spy={true} hashSpy={true} duration={500}>Test 2</Link></li>
<li><Link containerId="scroll-container" activeClass="active" className="test3" to="test3" spy={true} hashSpy={true} duration={500} smooth={true}>Test 3</Link></li>
<li><Link containerId="scroll-container" activeClass="active" className="test4" to="test4" spy={true} hashSpy={true} spy={true} duration={500} smooth={true}>Test 4</Link></li>
Expand Down
37 changes: 25 additions & 12 deletions modules/__tests__/link-test.js
Expand Up @@ -4,8 +4,8 @@ import expect from 'expect'
import assert from 'assert';
import Link from '../components/Link';

describe('Link', function() {
describe('Link', function () {

let node
beforeEach(function () {
node = document.createElement('div')
Expand All @@ -14,28 +14,41 @@ describe('Link', function() {
afterEach(function () {
unmountComponentAtNode(node)
})

it('renders only one component', function (done) {

var component = <Link to="test1" spy={true} smooth={true} duration={500}>Test 1</Link>;

render(component, node, function() {
expect(node.textContent).toEqual('Test 1');
done();
render(component, node, function () {
expect(node.textContent).toEqual('Test 1');
done();
});

})

it('renders two components', function (done) {

var component = <div>
<Link to="test1" spy={true} smooth={true} duration={500}>A</Link>;
<Link to="test1" spy={true} smooth={true} duration={500}>A</Link>;
<Link to="test1" spy={true} smooth={true} duration={500}>B</Link>;
</div>

render(component, node, function() {
expect(node.textContent).toEqual('A;B;');
done();
render(component, node, function () {
expect(node.textContent).toEqual('A;B;');
done();
});

})
it('renders two components with hash replaced', function (done) {

var component = <div>
<Link to="test1" spy={true} smooth={true} hashSpy={true} saveHashHistory={false} duration={500}>A</Link>;
<Link to="test1" spy={true} smooth={true} saveHashHistory={false} duration={500}>B</Link>;
</div>

render(component, node, function () {
expect(node.textContent).toEqual('A;B;');
done();
});

})
Expand Down
4 changes: 2 additions & 2 deletions modules/mixins/scroll-hash.js
Expand Up @@ -54,9 +54,9 @@ const scrollHash = {
return utils.getHash();
},

changeHash(to) {
changeHash(to, saveHashHistory) {
if (this.isInitialized() && utils.getHash() !== to) {
utils.pushHash(to);
utils.updateHash(to, saveHashHistory);
}
},

Expand Down
10 changes: 6 additions & 4 deletions modules/mixins/scroll-link.js
Expand Up @@ -22,7 +22,8 @@ const protoTypes = {
onSetActive: PropTypes.func,
onSetInactive: PropTypes.func,
ignoreCancelEvents: PropTypes.bool,
hashSpy: PropTypes.bool
hashSpy: PropTypes.bool,
saveHashHistory: PropTypes.bool
};

export default (Component, customScroller) => {
Expand Down Expand Up @@ -132,7 +133,8 @@ export default (Component, customScroller) => {
}

if (this.props.hashSpy && scrollHash.getHash() === to) {
scrollHash.changeHash();
const { saveHashHistory = false } = this.props
scrollHash.changeHash("", saveHashHistory);
}

if (this.props.spy && this.state.active) {
Expand All @@ -144,8 +146,8 @@ export default (Component, customScroller) => {

if (isInside && (activeLink !== to || this.state.active === false)) {
scroller.setActiveLink(to);

this.props.hashSpy && scrollHash.changeHash(to);
const { saveHashHistory = false } = this.props
this.props.hashSpy && scrollHash.changeHash(to, saveHashHistory);

if (this.props.spy) {
this.setState({ active: true });
Expand Down
40 changes: 17 additions & 23 deletions modules/mixins/utils.js
@@ -1,26 +1,20 @@
const pushHash = hash => {
hash = hash ? (hash.indexOf("#") === 0 ? hash : "#" + hash) : "";

if (history.pushState) {
let loc = window.location;
history.pushState(
null,
null,
hash
? loc.pathname + loc.search + hash
: // remove hash
loc.pathname + loc.search
);
} else {
location.hash = hash;
}
const updateHash = (hash, historyUpdate) => {
const hashVal = hash.indexOf("#") === 0 ? hash.substring(1) : hash;
const hashToUpdate = hashVal ? `#${hashVal}` : "";
const curLoc = window && window.location;
const urlToPush = hashToUpdate
? curLoc.pathname + curLoc.search + hashToUpdate
: curLoc.pathname + curLoc.search;
historyUpdate
? history.pushState(null, "", urlToPush)
: history.replaceState(null, "", urlToPush);
};

const getHash = () => {
return window.location.hash.replace(/^#/, "");
};

const filterElementInContainer = container => element =>
const filterElementInContainer = (container) => (element) =>
container.contains
? container != element && container.contains(element)
: !!(container.compareDocumentPosition(element) & 16);
Expand All @@ -30,19 +24,19 @@ const scrollOffset = (c, t, horizontal) => {
return c === document
? t.getBoundingClientRect().left + (window.scrollX || window.pageXOffset)
: getComputedStyle(c).position !== "static"
? t.offsetLeft
: t.offsetLeft - c.offsetLeft;
? t.offsetLeft
: t.offsetLeft - c.offsetLeft;
} else {
return c === document
? t.getBoundingClientRect().top + (window.scrollY || window.pageYOffset)
: getComputedStyle(c).position !== "static"
? t.offsetTop
: t.offsetTop - c.offsetTop;
? t.offsetTop
: t.offsetTop - c.offsetTop;
}
};
export default {
pushHash,
updateHash,
getHash,
filterElementInContainer,
scrollOffset
scrollOffset,
};

0 comments on commit 8e5b058

Please sign in to comment.