Skip to content

Commit

Permalink
Merge branch 'master' into dev
Browse files Browse the repository at this point in the history
Conflicts:
	lib/TagCloud.js
	lib/defaultRenderer.js
	lib/helpers.js
	src/defaultRenderer.js
  • Loading branch information
madox2 committed Jun 10, 2016
2 parents 8b0c577 + f9b4969 commit 28aab3d
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 43 deletions.
13 changes: 11 additions & 2 deletions __tests__/defaultRenderer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,21 @@ describe('defaultRenderer', () => {
expect(tag.props.style).toEqual(objectContaining({ color: 'custom' }));
});

it('should disable random color', () => {
it('should disable random color using custom props', () => {
// deprecated
const render = defaultRenderer({
props: { disableRandomColor: true }
});
const tag = render({ value: 'tag1', count: 33 }, 18, 1);
expect(tag.props.style).toEqual(objectContaining({ color: 'black' }));
expect(tag.props.style.color).not.toBeDefined();
});

it('should disable random color using options', () => {
const render = defaultRenderer({
disableRandomColor: true
});
const tag = render({ value: 'tag1', count: 33 }, 18, 1);
expect(tag.props.style.color).not.toBeDefined();
});

});
57 changes: 25 additions & 32 deletions lib/TagCloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,14 @@ var _helpers = require('./helpers');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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); } }

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; }

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 createTags = function createTags(tags, props) {
var minSize = props.minSize;
var maxSize = props.maxSize;
var renderer = props.renderer;

var handlers = (0, _helpers.includeProps)(props, eventHandlers);
var counts = tags.map(function (tag) {
return tag.count;
}),
min = Math.min.apply(Math, _toConsumableArray(counts)),
max = Math.max.apply(Math, _toConsumableArray(counts));
var computeFontSize = function computeFontSize(tag) {
return {
tag: tag,
fontSize: (0, _helpers.fontSizeConverter)(tag.count, min, max, minSize, maxSize)
};
};
var createComponent = function createComponent(_ref, key) {
var tag = _ref.tag;
var fontSize = _ref.fontSize;
return renderer(tag, fontSize, key, handlers);
};
return tags.map(computeFontSize).map(createComponent);
};

var eventHandlers = ['onClick', 'onDoubleClick', 'onMouseMove'];
var cloudProps = ['tags', 'shuffle', 'renderer', 'maxSize', 'minSize'];

Expand All @@ -68,9 +43,9 @@ var TagCloud = exports.TagCloud = function (_React$Component) {

_createClass(TagCloud, [{
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(_ref2) {
var shuffle = _ref2.shuffle;
var tags = _ref2.tags;
value: function componentWillReceiveProps(_ref) {
var shuffle = _ref.shuffle;
var tags = _ref.tags;

if (!shuffle) {
this._tags = tags;
Expand All @@ -97,13 +72,31 @@ var TagCloud = exports.TagCloud = function (_React$Component) {
key: 'render',
value: function render() {
var props = (0, _helpers.omitProps)(this.props, [].concat(cloudProps, eventHandlers));
var tags = createTags(this._tags, this.props);
return _react2.default.createElement(
'div',
props,
tags
this._createTags()
);
}
}, {
key: '_createTags',
value: function _createTags() {
var _props2 = this.props;
var minSize = _props2.minSize;
var maxSize = _props2.maxSize;
var renderer = _props2.renderer;

var handlers = (0, _helpers.includeProps)(this.props, eventHandlers);
var counts = this._tags.map(function (tag) {
return tag.count;
}),
min = Math.min.apply(Math, _toConsumableArray(counts)),
max = Math.max.apply(Math, _toConsumableArray(counts));
return this._tags.map(function (tag) {
var fontSize = (0, _helpers.fontSizeConverter)(tag.count, min, max, minSize, maxSize);
return renderer(tag, fontSize, handlers);
});
}
}]);

return TagCloud;
Expand Down
18 changes: 13 additions & 5 deletions lib/defaultRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,30 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de

var defaultRenderer = exports.defaultRenderer = function defaultRenderer() {
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
return function (tag, size, key) {
var handlers = arguments.length <= 3 || arguments[3] === undefined ? {} : arguments[3];
return function (tag, size) {
var handlers = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
var _options$tagRenderer = options.tagRenderer;
var tagRenderer = _options$tagRenderer === undefined ? function (tag) {
return tag.value;
} : _options$tagRenderer;
var _options$colorOptions = options.colorOptions;
var colorOptions = _options$colorOptions === undefined ? {} : _options$colorOptions;
var _options$disableRando = options.disableRandomColor;
var disableRandomColor = _options$disableRando === undefined ? false : _options$disableRando;
var _options$props = options.props;
var props = _options$props === undefined ? {} : _options$props;


if (props.disableRandomColor) {
// eslint-disable-next-line no-console
console.warn("Using deprecated property 'disableRandomColor' passed to prop option of defaultRenderer. " + "It will be removed in the next major release and replaced with it's own option 'disableRandomColor'.");
}

var className = 'tag-cloud-tag';
var fontSize = size + 'px';
var color = props.disableRandomColor ? tag.color || 'black' : (0, _randomcolor2.default)(colorOptions);
var color = props.disableRandomColor || disableRandomColor ? tag.color || undefined : (0, _randomcolor2.default)(colorOptions);
var key = tag.key || tag.value;
var style = (0, _objectAssign2.default)({}, styles, { color: color }, props.style, { fontSize: fontSize });

var eventHandlers = {};
Object.keys(handlers).forEach(function (key) {
Expand All @@ -44,8 +53,7 @@ var defaultRenderer = exports.defaultRenderer = function defaultRenderer() {
});
});

var elementProps = (0, _objectAssign2.default)({}, { className: className }, eventHandlers, props, { key: key });
elementProps.style = (0, _objectAssign2.default)({}, styles, { color: color }, props.style, { fontSize: fontSize });
var elementProps = (0, _objectAssign2.default)({}, { className: className }, eventHandlers, props, { style: style }, { key: key });

return _react2.default.createElement(
'span',
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var _defaultRenderer = require('./defaultRenderer');

var deprecatedRendererExport = function deprecatedRendererExport() {
// eslint-disable-next-line no-console
console.warn('Using deprecated \'DefaultRenderer\' import, it will be removed in the next release and replaced with \'defaultRenderer\'.');
console.warn('Using deprecated \'DefaultRenderer\' import, it will be removed in the next major release and replaced with \'defaultRenderer\'.');
return _defaultRenderer.defaultRenderer.apply(undefined, arguments);
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-tagcloud",
"version": "0.4.1",
"version": "0.4.2",
"description": "Tag/word cloud component for react",
"main": "lib/index.js",
"scripts": {
Expand Down
9 changes: 8 additions & 1 deletion src/defaultRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@ export const defaultRenderer = (options = {}) => (tag, size, handlers = {}) => {
const {
tagRenderer = tag => tag.value,
colorOptions = {},
disableRandomColor = false,
props = {}
} = options;

if (props.disableRandomColor) {
// eslint-disable-next-line no-console
console.warn("Using deprecated property 'disableRandomColor' passed to prop option of defaultRenderer. "
+ "It will be removed in the next major release and replaced with it's own option 'disableRandomColor'.");
}

const className = 'tag-cloud-tag';
const fontSize = size + 'px';
const color = props.disableRandomColor ? tag.color || 'black' : randomColor(colorOptions);
const color = (props.disableRandomColor || disableRandomColor) ? tag.color || undefined : randomColor(colorOptions);
const key = tag.key || tag.value;
const style = objectAssign({}, styles, {color}, props.style, {fontSize});

Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { defaultRenderer } from './defaultRenderer';

const deprecatedRendererExport = (...args) => {
// eslint-disable-next-line no-console
console.warn(`Using deprecated 'DefaultRenderer' import, it will be removed in the next release and replaced with 'defaultRenderer'.`);
console.warn(`Using deprecated 'DefaultRenderer' import, it will be removed in the next major release and replaced with 'defaultRenderer'.`);
return defaultRenderer(...args);
};

Expand Down

0 comments on commit 28aab3d

Please sign in to comment.