@alifd/next
Version:
A configurable component library for web built on React.
80 lines (72 loc) • 1.85 kB
JavaScript
import PropTypes from 'prop-types';
/**
* Creates an object with the same values as object and keys
* generated by running each own enumerable string keyed property
* of object thru iteratee.
* @param {Object} obj
* @param {Function} fn
* @return {Object}
*/
var mapKeys = function mapKeys(obj, fn) {
var result = {};
for (var key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
var value = obj[key];
var newKey = fn(key, value);
result[newKey] = value;
}
}
return result;
};
/**
* Replace specific key with prefix `next`
* and lowercase first character of the result.
* @param {String} key
* @return {String}
*/
var replaceKey = function replaceKey(key) {
return key.replace(/^(next)([A-Z])/, function (match, p1, p2) {
return p2.toLowerCase();
});
};
/**
* @param {Object} source
* @return {Object}
*/
var transformContext = function transformContext(source) {
return mapKeys(source, replaceKey);
};
/**
* Consumer
* @param {Object} prop
* @param {Object} context
*/
var Consumer = function Consumer(_ref, context) {
var children = _ref.children;
return typeof children === 'function' ? children(transformContext(context)) : null;
};
/**
* PropTypes
* @type {Object}
* @static
*/
Consumer.propTypes = {
// Render context as function
// Function(context: object): ReactElement
children: PropTypes.func
};
/**
* ContextTypes (legacy context)
* @type {Object}
* @static
*/
Consumer.contextTypes = {
nextPrefix: PropTypes.string,
nextLocale: PropTypes.object,
nextPure: PropTypes.bool,
newRtl: PropTypes.bool,
nextWarning: PropTypes.bool,
nextDevice: PropTypes.oneOf(['tablet', 'desktop', 'phone']),
nextPopupContainer: PropTypes.any
};
export default Consumer;