semantic-ui-react
Version:
The official Semantic-UI-React integration.
167 lines (126 loc) • 6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createHTMLLabel = exports.createHTMLInput = exports.createHTMLImage = undefined;
var _extends2 = require('babel-runtime/helpers/extends');
var _extends3 = _interopRequireDefault(_extends2);
var _isFunction2 = require('lodash/isFunction');
var _isFunction3 = _interopRequireDefault(_isFunction2);
var _isArray2 = require('lodash/isArray');
var _isArray3 = _interopRequireDefault(_isArray2);
var _isPlainObject2 = require('lodash/isPlainObject');
var _isPlainObject3 = _interopRequireDefault(_isPlainObject2);
var _isNumber2 = require('lodash/isNumber');
var _isNumber3 = _interopRequireDefault(_isNumber2);
var _isString2 = require('lodash/isString');
var _isString3 = _interopRequireDefault(_isString2);
exports.createShorthand = createShorthand;
exports.createShorthandFactory = createShorthandFactory;
var _classnames = require('classnames');
var _classnames2 = _interopRequireDefault(_classnames);
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// ============================================================
// Factories
// ============================================================
/**
* A more robust React.createElement. It can create elements from primitive values.
*
* @param {function|string} Component A ReactClass or string
* @param {function} mapValueToProps A function that maps a primitive value to the Component props
* @param {string|object|function} val The value to create a ReactElement from
* @param {object|function} [defaultProps={}] Default props object or function (called with regular props).
* @param {boolean} [generateKey=false] Whether or not to generate a child key, useful for collections.
* @returns {object|null}
*/
function createShorthand(Component, mapValueToProps, val) {
var defaultProps = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
var generateKey = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
if (typeof Component !== 'function' && typeof Component !== 'string') {
throw new Error('createShorthandFactory() Component must be a string or function.');
}
// short circuit for disabling shorthand
if (val === null) return null;
var valIsString = (0, _isString3.default)(val);
var valIsNumber = (0, _isNumber3.default)(val);
var isReactElement = (0, _react.isValidElement)(val);
var isPropsObject = (0, _isPlainObject3.default)(val);
var isPrimitiveValue = valIsString || valIsNumber || (0, _isArray3.default)(val);
// ----------------------------------------
// Build up props
// ----------------------------------------
// User's props
var usersProps = isReactElement && val.props || isPropsObject && val || isPrimitiveValue && mapValueToProps(val);
// Default props
defaultProps = (0, _isFunction3.default)(defaultProps) ? defaultProps(usersProps) : defaultProps;
// Merge props
/* eslint-disable react/prop-types */
var props = (0, _extends3.default)({}, defaultProps, usersProps);
// Merge className
if (usersProps.className && defaultProps.className) {
props.className = (0, _classnames2.default)(defaultProps.className, usersProps.className);
}
// Merge style
if (usersProps.style && defaultProps.style) {
props.style = (0, _extends3.default)({}, defaultProps.style, usersProps.style);
}
// ----------------------------------------
// Get key
// ----------------------------------------
// Use key, childKey, or generate key
if (!props.key) {
var childKey = props.childKey;
if (childKey) {
// apply and consume the childKey
props.key = typeof childKey === 'function' ? childKey(props) : childKey;
delete props.childKey;
} else if (generateKey && (valIsString || valIsNumber)) {
// use string/number shorthand values as the key
props.key = val;
}
}
/* eslint-enable react/prop-types */
// ----------------------------------------
// Create Element
// ----------------------------------------
// Clone ReactElements
if (isReactElement) return (0, _react.cloneElement)(val, props);
// Create ReactElements from built up props
if (isPrimitiveValue || isPropsObject) return _react2.default.createElement(Component, props);
// Otherwise null
return null;
}
// ============================================================
// Factory Creators
// ============================================================
/**
* Creates a `createShorthand` function that is waiting for a value and defaultProps.
*
* @param {function|string} Component A ReactClass or string
* @param {function} mapValueToProps A function that maps a primitive value to the Component props
* @param {boolean} [generateKey] Whether or not to generate a child key, useful for collections.
* @returns {function} A shorthand factory function waiting for `val` and `defaultProps`.
*/
createShorthand.handledProps = [];
function createShorthandFactory(Component, mapValueToProps, generateKey) {
if (typeof Component !== 'function' && typeof Component !== 'string') {
throw new Error('createShorthandFactory() Component must be a string or function.');
}
return function (val, defaultProps) {
return createShorthand(Component, mapValueToProps, val, defaultProps, generateKey);
};
}
// ============================================================
// HTML Factories
// ============================================================
var createHTMLImage = exports.createHTMLImage = createShorthandFactory('img', function (value) {
return { src: value };
});
var createHTMLInput = exports.createHTMLInput = createShorthandFactory('input', function (value) {
return { type: value };
});
var createHTMLLabel = exports.createHTMLLabel = createShorthandFactory('label', function (value) {
return { children: value };
});