UNPKG

jsx-if-else

Version:

A very lightweight and small react component that will let you write simple if and else statement with JSX syntax.

107 lines (91 loc) 4.13 kB
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } import React, { Component } from 'react'; import PropTypes from 'prop-types'; export var If = function (_Component) { _inherits(If, _Component); function If() { _classCallCheck(this, If); var _this = _possibleConstructorReturn(this, (If.__proto__ || Object.getPrototypeOf(If)).call(this)); _this.renderIf = _this.renderIf.bind(_this); _this.renderElse = _this.renderElse.bind(_this); return _this; } _createClass(If, [{ key: 'renderIf', value: function renderIf(children) { /* * If children is not an array there is no upcoming Else * so returning right the children away. */ if (!Array.isArray(children)) { return children; } var sliceIndex = children.findIndex(function (child) { if (Object.prototype.hasOwnProperty.call(child, 'props')) { return child.props.operator === 'Else'; } }); var result = null; if (sliceIndex) { result = children.slice(0, sliceIndex); } else { result = children; } return result; } }, { key: 'renderElse', value: function renderElse(children) { /* * If the component's children are not an array there is no Else content * so returning null */ if (!Array.isArray(children)) { return null; } // Find index to know where to slice var sliceIndex = children.findIndex(function (child) { if (Object.prototype.hasOwnProperty.call(child, 'props')) { return child.props.operator === 'Else'; } }); if (sliceIndex) { // Else were found so slicing return children.slice(sliceIndex + 1, children.length); } else { // Else were not found so returning null because the state ment is false return null; } } }, { key: 'render', value: function render() { if (this.props.condition === true) { return this.renderIf(this.props.children); } else { return this.renderElse(this.props.children); } } }]); return If; }(Component); If.propTypes = { condition: PropTypes.bool.isRequired }; export var Else = function (_React$Component) { _inherits(Else, _React$Component); function Else() { _classCallCheck(this, Else); return _possibleConstructorReturn(this, (Else.__proto__ || Object.getPrototypeOf(Else)).apply(this, arguments)); } _createClass(Else, [{ key: 'render', value: function render() { return null; } }]); return Else; }(React.Component); Else.propTypes = { operator: PropTypes.oneOf(['Else']) }; Else.defaultProps = { operator: 'Else' };