Skip to content

Commit

Permalink
Merge pull request #1278 from FormidableLabs/feature/blacklist-with-r…
Browse files Browse the repository at this point in the history
…egex

Feature/blacklist with regex
  • Loading branch information
boygirl authored Mar 18, 2019
2 parents 6935283 + 28cbb3a commit 842145a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
12 changes: 11 additions & 1 deletion packages/victory-core/src/victory-util/prop-types.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*eslint no-magic-numbers: ["error", { "ignore": [-1, 0, 1, 2] }]*/
import { isFunction, find } from "lodash";
import { isFunction, find, isRegExp } from "lodash";
import Log from "./log";
import PropTypes from "prop-types";

Expand Down Expand Up @@ -209,5 +209,15 @@ export default {
return new Error(`Length of data and ${propName} arrays must match.`);
}
return undefined;
}),

/**
* Check that the value is a regular expression
*/
regExp: makeChainable((props, propName, componentName) => {
if (props[propName] && !isRegExp(props[propName])) {
return new Error(`\`${propName}\` in \`${componentName}\` must be a regular expression.`);
}
return undefined;
})
};
2 changes: 1 addition & 1 deletion packages/victory-voronoi-container/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ _example:_ `radius={25}`

### voronoiBlacklist

`type: array[string]`
`type: array[string || regex]`

The `voronoiBlacklist` prop is used to specify a list of components to ignore when calculating a shared voronoi diagram. Components with a `name` prop matching an element in the `voronoiBlacklist` array will be ignored by `VictoryVoronoiContainer`. Ignored components will never be flagged as active, and will not contribute date to shared tooltips or labels.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import PropTypes from "prop-types";
import React from "react";
import { defaults, isFunction, pick } from "lodash";
import { VictoryTooltip } from "victory-tooltip";
import { VictoryContainer, Helpers, TextSize } from "victory-core";
import { VictoryContainer, Helpers, TextSize, PropTypes as CustomPropTypes } from "victory-core";
import VoronoiHelpers from "./voronoi-helpers";

export const voronoiContainerMixin = (base) =>
Expand All @@ -18,7 +18,9 @@ export const voronoiContainerMixin = (base) =>
onActivated: PropTypes.func,
onDeactivated: PropTypes.func,
radius: PropTypes.number,
voronoiBlacklist: PropTypes.arrayOf(PropTypes.string),
voronoiBlacklist: PropTypes.arrayOf(
PropTypes.oneOfType([PropTypes.string, CustomPropTypes.regExp])
),
voronoiDimension: PropTypes.oneOf(["x", "y"]),
voronoiPadding: PropTypes.number
};
Expand Down
17 changes: 15 additions & 2 deletions packages/victory-voronoi-container/src/voronoi-helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { Selection, Data, Helpers } from "victory-core";
import { assign, throttle, isFunction, isEmpty, groupBy, keys, includes } from "lodash";
import {
assign,
throttle,
isFunction,
isEmpty,
groupBy,
keys,
includes,
isString,
isRegExp
} from "lodash";
import isEqual from "react-fast-compare";
import { voronoi as d3Voronoi } from "d3-voronoi";
import React from "react";
Expand Down Expand Up @@ -54,7 +64,10 @@ const VoronoiHelpers = {
const childProps = child.props || {};
const name = childProps.name || childName;
const blacklist = props.voronoiBlacklist || [];
if (!Data.isDataComponent(child) || includes(blacklist, name)) {
const blacklistStr = blacklist.filter(isString);
const blacklistRegExp = blacklist.filter(isRegExp);
const isRegExpMatch = blacklistRegExp.some((regExp) => regExp.test(name));
if (!Data.isDataComponent(child) || includes(blacklistStr, name) || isRegExpMatch) {
return null;
}
const getChildData =
Expand Down

0 comments on commit 842145a

Please sign in to comment.