mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 12:50:38 +00:00
WIP Version 0.0.1
This commit is contained in:
33
client/src/components/Utils/Choose.js
Normal file
33
client/src/components/Utils/Choose.js
Normal file
@@ -0,0 +1,33 @@
|
||||
import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
import If from './if';
|
||||
|
||||
const Choose = props => {
|
||||
let when = null;
|
||||
let otherwise = null;
|
||||
|
||||
React.Children.forEach(props.children, children => {
|
||||
if (children.props.condition === undefined) {
|
||||
otherwise = children;
|
||||
} else if (!when && children.props.condition === true) {
|
||||
when = children;
|
||||
}
|
||||
});
|
||||
|
||||
return when || otherwise;
|
||||
};
|
||||
|
||||
Choose.propTypes = {
|
||||
children: PropTypes.node
|
||||
};
|
||||
|
||||
Choose.When = If;
|
||||
|
||||
Choose.Otherwise = ({render, children}) => render ? render() : children;
|
||||
|
||||
Choose.Otherwise.propTypes = {
|
||||
children: PropTypes.node,
|
||||
render: PropTypes.func
|
||||
};
|
||||
|
||||
export default Choose;
|
||||
11
client/src/components/Utils/For.js
Normal file
11
client/src/components/Utils/For.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
|
||||
const For = ({render, of}) => of.map((item, index) => render(item, index));
|
||||
|
||||
For.propTypes = {
|
||||
of: PropTypes.array.isRequired,
|
||||
render: PropTypes.func.isRequired
|
||||
};
|
||||
|
||||
export default For;
|
||||
13
client/src/components/Utils/If.js
Normal file
13
client/src/components/Utils/If.js
Normal file
@@ -0,0 +1,13 @@
|
||||
import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
|
||||
const If = props => props.condition
|
||||
? (props.render ? props.render() : props.children) : null;
|
||||
|
||||
If.propTypes = {
|
||||
condition: PropTypes.bool.isRequired,
|
||||
children: PropTypes.node,
|
||||
render: PropTypes.func
|
||||
};
|
||||
|
||||
export default If;
|
||||
Reference in New Issue
Block a user