Skip to content

Commit

Permalink
feat(react:can): adds support for multiple <Can> children
Browse files Browse the repository at this point in the history
  • Loading branch information
stalniy committed Aug 31, 2018
1 parent 52bd87a commit c022b32
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions packages/casl-react/src/Can.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import React, { PureComponent } from 'react';
import React, { PureComponent, Fragment, createElement } from 'react';
import PropTypes from 'prop-types';
import { Ability } from '@casl/ability';

const noop = () => {};
const renderChildren = Fragment
? children => createElement.apply(null, [Fragment, null].concat(children))
: React.Children.only;
const REQUIRED_OBJECT_OR_STRING = PropTypes
.oneOfType([PropTypes.object, PropTypes.string])
.isRequired;
Expand Down Expand Up @@ -87,7 +90,10 @@ export default class Can extends PureComponent {

renderChildren() {
const { children } = this.props;
const elements = typeof children === 'function'
? children(this.state.ability)
: children;

return typeof children === 'function' ? children(this.state.ability) : React.Children.only(children);
return renderChildren(elements);
}
}

0 comments on commit c022b32

Please sign in to comment.