1
0
mirror of https://github.com/ascribe/onion.git synced 2025-01-03 18:35:09 +01:00

Pass props through AclProxy

This commit is contained in:
Brett Sun 2016-06-17 12:44:26 +02:00
parent f9bacec62a
commit 6eac3663d0

View File

@ -1,7 +1,8 @@
'use strict';
import React from 'react'; import React from 'react';
import { omitFromObject } from '../utils/general';
/** /**
* This component can easily be used to present another component conditionally * This component can easily be used to present another component conditionally
* - dependent on their acl. * - dependent on their acl.
@ -9,47 +10,48 @@ import React from 'react';
* In order to do that, just wrap AclProxy around the component, add aclObject and * In order to do that, just wrap AclProxy around the component, add aclObject and
* the acl name you're looking for. * the acl name you're looking for.
*/ */
let AclProxy = React.createClass({ const AclProxy = React.createClass({
propTypes: { propTypes: {
children: React.PropTypes.oneOfType([ children: React.PropTypes.node.isRequired,
React.PropTypes.arrayOf(React.PropTypes.element),
React.PropTypes.element
]).isRequired,
aclObject: React.PropTypes.object,
aclName: React.PropTypes.string, aclName: React.PropTypes.string,
aclObject: React.PropTypes.object,
show: React.PropTypes.bool show: React.PropTypes.bool
}, },
getChildren() { getChildren() {
if (React.Children.count(this.props.children) > 1){ const childProps = omitFromObject(this.props, ['aclName', 'aclObject', 'children', 'show']);
/* const children = React.Children.map(
This might ruin styles for header items in the navbar etc this.props.children,
*/ (child) => React.cloneElement(child, childProps)
return (
<span>
{this.props.children}
</span>
); );
if (children.length > 1) {
// This has the potential to ruin some styling, but React doesn't let us just return an
// array of components.
return (<span>{children}</span>);
} else {
return children[0];
} }
/* can only do this when there is only 1 child, but will preserve styles */
return this.props.children;
}, },
render() { render() {
if(this.props.show) { const { aclName, aclObject, show } = this.props;
if (show) {
return this.getChildren(); return this.getChildren();
} else { } else if (aclObject) {
if(this.props.aclObject) { if (aclObject[aclName]) {
if(this.props.aclObject[this.props.aclName]) {
return this.getChildren(); return this.getChildren();
} else { } else if (false && process.env.NODE_ENV !== 'production') {
/* if(typeof this.props.aclObject[this.props.aclName] === 'undefined') { // Warning currently disabled because the main app's acls don't include whitelabel
console.warn('The aclName you\'re filtering for was not present (or undefined) in the aclObject.'); // specific acls, causing a lot of warnings
} */ // eslint-disable-next-line no-console
return null; console.warn(`The aclName (${aclName}) you're filtering for was not present (or ` +
} 'undefined) in the aclObject.');
} }
} }
return null; return null;
} }
}); });