UNPKG

react-hyperscript-wrapper

Version:

Wrap react component with hyperscript function

129 lines (94 loc) 3.88 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.wrapper = undefined; var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); var _react = require('react'); var _react2 = _interopRequireDefault(_react); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _toArray(arr) { return Array.isArray(arr) ? arr : Array.from(arr); } function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } var isChildren = function isChildren(value) { return Array.isArray(value) || _react2.default.isValidElement(value) || typeof value === 'string'; }; var filterArgument = function filterArgument(args) { switch (args.length) { case 3: return [].concat(_toConsumableArray(args)); case 2: // Check args[0] is identifies if (typeof args[0] === 'string') { // Check args[1] is children or text if (isChildren(args[1])) { return [args[0], {}, args[1]]; } // args[1] is property return [].concat(_toConsumableArray(args), [null]); } else { // Check args[1] is children if (isChildren(args[1])) { return [null].concat(_toConsumableArray(args)); } return [null, {}, null]; } case 1: if (typeof args[0] === 'string') { // Check args[0] is identifies if (args[0].startsWith('.') || args[0].startsWith('#')) { return [args[0], {}, null]; } // args[0] is the text as children return [null, {}, args[0]]; } else if (isChildren(args[0])) { return [null, {}, args[0]]; } return [null, args[0], null]; case 0: return [null, {}, null]; default: return [].concat(_toConsumableArray(args)); } }; var wrapComponent = function wrapComponent(Component) { return function () { var _filterArgument = filterArgument(arguments); var _filterArgument2 = _slicedToArray(_filterArgument, 3); var identifies = _filterArgument2[0]; var props = _filterArgument2[1]; var children = _filterArgument2[2]; if (identifies) { var identifiesList = identifies.split('#'); var id = null; var className = ''; identifiesList.forEach(function (entry) { if (entry.length > 0) { var _entry$split = entry.split('.'); var _entry$split2 = _toArray(_entry$split); var tmpId = _entry$split2[0]; var tmpClass = _entry$split2.slice(1); if (tmpId) { id = tmpId; } if (tmpClass) { className += tmpClass.join(' '); } } }); if (id) { props['id'] = id; } if (className) { props['className'] = className; } } return _react2.default.createElement(Component, props, children); }; }; var wrapper = exports.wrapper = function wrapper(components) { var hyperComponents = {}; Object.keys(components).forEach(function (key) { hyperComponents[key] = wrapComponent(components[key]); }); return hyperComponents; };