Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add passtrough params #122

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"presets": ["react", "es2015"],
"presets": ["react", "es2015", "stage-2"],
"plugins": ["add-module-exports"],
"env": {
"development": {
Expand Down
27 changes: 24 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ React-portal
- doesn't leave any mess in DOM after closing
- provides its child with **this.props.closePortal** callback
- provides **close on ESC** and **close on outside mouse click** out of the box
- the **close** can be bound to parameters
- supports absolute positioned components (great for tooltips)
- **no dependencies**
- **fully covered by tests**
Expand Down Expand Up @@ -106,8 +107,9 @@ This callback is called when the portal is opened and rendered (useful for anima
#### beforeClose: func(DOMNode, removeFromDOM)
This callback is called when the closing event is triggered but it prevents normal removal from the DOM. So, you can do some DOMNode animation first and then call removeFromDOM() that removes the portal from DOM.

#### onClose: func
#### onClose: func(params)
This callback is called when the portal closes and after beforeClose.
You can bind parameters to the onClose callback.

#### onUpdate: func
This callback is called when the portal is (re)rendered.
Expand Down Expand Up @@ -136,7 +138,7 @@ also need an access to `this.props.closePortal()`? You can't just use `{this.pro

#### Open modal programmatically

Sometimes you need to open your portal (e.g. modal) automatically. There is no button to click on. No problem, because the portal has the `isOpen` prop, so you can just set it to `true` or `false`. However, then it's completely up to you to take care about the portal closing (ESC, outside click, no `this.props.closePortal` callback...).
Sometimes you need to open your portal (e.g. modal) programmatically. There is no button to click on. No problem, because the portal has the `isOpen` prop, so you can just set it to `true` or `false`. However, then it's completely up to you to take care about the portal closing (ESC, outside click, no `this.props.closePortal` callback...).

However, there is a nice trick how to make this happen even without `isOpen`:

Expand All @@ -153,7 +155,26 @@ this.refs.myPortal.openPortal()
// opens the portal, yay!
```

## Contribution
#### Close portal programmatically with passtrough parameters

Sometimes you need to close your portal programmatically and pass trough parameters from your portal to the calling instance.
You have two ways you can do it.

##### Way 1
You can additionaly add a closure as a passtrough parameter to your onClose callback func.
```
render() {
const { isOpen, toPassTrough } = this.state;
<Portal
isOpen={isOpen}
onClose={() => onCloseFunc(toPassTrough)}
>
}
```
##### Way 2
Assuming you have passed the closePortal function to the Portal's children - you can call the closePortal func with parameters.

```<button onClick={() => closePortal({ addNew: true })}>Close</button>```## Contribution

Please, create issues and pull requests.

Expand Down
7 changes: 5 additions & 2 deletions lib/portal.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default class Portal extends React.Component {
super();
this.state = { active: false };
this.handleWrapperClick = this.handleWrapperClick.bind(this);
this.onClose = undefined;
this.closePortal = this.closePortal.bind(this);
this.handleOutsideMouseClick = this.handleOutsideMouseClick.bind(this);
this.handleKeydown = this.handleKeydown.bind(this);
Expand All @@ -19,6 +20,7 @@ export default class Portal extends React.Component {
}

componentDidMount() {
this.onClose = this.props.onClose;
if (this.props.closeOnEsc) {
document.addEventListener('keydown', this.handleKeydown);
}
Expand All @@ -44,6 +46,7 @@ export default class Portal extends React.Component {
}
}
if (!newProps.isOpen && this.state.active) {
this.onClose = newProps.onClose;
this.closePortal();
}
}
Expand Down Expand Up @@ -79,7 +82,7 @@ export default class Portal extends React.Component {
this.renderPortal(props, true);
}

closePortal(isUnmounted = false) {
closePortal({ isUnmounted = false, ...rest } = {}) {
const resetPortalState = () => {
if (this.node) {
ReactDOM.unmountComponentAtNode(this.node);
Expand All @@ -99,7 +102,7 @@ export default class Portal extends React.Component {
resetPortalState();
}

this.props.onClose();
this.onClose.call(null, rest);
}
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"babel-preset-es2015": "^6.3.13",
"babel-preset-react": "^6.3.13",
"babel-preset-react-hmre": "^1.0.1",
"babel-preset-stage-2": "^6.18.0",
"babel-register": "^6.8.0",
"cross-env": "^1.0.7",
"enzyme": "^2.3.0",
Expand Down
20 changes: 19 additions & 1 deletion test/portal_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,24 @@ describe('react-portal', () => {
assert(props.onClose.calledOnce);
});

it('onClose has named params when closePortal is called directly with named params', () => {
const props = { isOpen: true, onClose: spy() };
const wrapper = mount(<Portal {...props}><p>Hi</p></Portal>);
wrapper.instance().closePortal({ namedParam: { propA: 1, propB: 'test' } });
assert(props.onClose.calledWith({ namedParam: { propA: 1, propB: 'test' } }));
});

it('onClose has named params when named params were pre bound to onClose', () => {
const onCloseFunc = spy();
const props = { isOpen: true, onClose: () => onCloseFunc({ namedParam: 'test' }) };
const wrapper = mount(<Portal {...props}><p>Hi</p></Portal>);
assert.equal(document.body.lastElementChild, wrapper.instance().node);
assert.equal(document.body.childElementCount, 1);
wrapper.setProps({ isOpen: false, children: <p>Hi</p> });
assert.equal(document.body.childElementCount, 0);
assert(onCloseFunc.calledWith({ namedParam: 'test' }));
});

it('should call props.onClose() only once when portal closes and then is unmounted', () => {
const div = document.createElement('div');
const props = { isOpen: true, onClose: spy() };
Expand All @@ -177,8 +195,8 @@ describe('react-portal', () => {
const div = document.createElement('div');
const props = { isOpen: true };
const wrapper = render(<Portal {...props}><p>Hi</p></Portal>, div);
spy(wrapper, 'setState');
unmountComponentAtNode(div);
spy(wrapper, 'setState');
assert.equal(wrapper.setState.callCount, 0);
});
});
Expand Down