@talend/react-components
Version:
Set of react components.
98 lines (93 loc) • 3.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
exports.isBranch = isBranch;
exports.isBranchOpened = isBranchOpened;
exports.isDeepNodeHighlighted = isDeepNodeHighlighted;
exports.isNodeHighlighted = isNodeHighlighted;
var _react = require("react");
var _propTypes = _interopRequireDefault(require("prop-types"));
var _lodash = require("lodash");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
/**
* Return the type of the current node.
* @param {string} type
*/
function isBranch(type) {
return type === 'array' || type === 'object';
}
/**
* Check if the branch is currently opened with the help of the jonspath.
* @param {boolean} expandAll
* @param {number} index
* @param {array} paths
* @param {string} jsonpath
*/
function isBranchOpened(expandAll, index, paths, jsonpath) {
const isPresent = (0, _lodash.get)(paths, index, []).indexOf(jsonpath) !== -1;
return expandAll ? !isPresent : isPresent;
}
/**
* Check if the node is highlight with the help of the jsonpath.
* @param {array} highlighted
* @param {string} jsonpath
*/
function isNodeHighlighted(highlighted, jsonpath) {
return !!highlighted.find(pattern => jsonpath.match(pattern));
}
/**
* Check if children's branch has been selected and branch is closed.
* if so the branch will be highlighted.
* Only for object type.
* @param {boolean} opened
* @param {boolean} nodeHighlighted
* @param {string} type
*/
function isDeepNodeHighlighted(opened, nodeHighlighted, type) {
if (type === 'object' || type === 'array') {
return !opened && nodeHighlighted;
}
return nodeHighlighted;
}
/**
* Render a branch or a leaf.
* It gives the recursiveTree func to the branch to help render another tree.
*/
class TreeNode extends _react.Component {
render() {
const type = this.props.getItemType(this.props.value);
const opened = isBranchOpened(this.props.isAllExpanded, this.props.index, this.props.paths, this.props.jsonpath);
const nodeHighlighted = isNodeHighlighted((0, _lodash.get)(this.props, 'highlighted', []), this.props.jsonpath);
if (isBranch(type)) {
return this.props.branch({
...this.props,
nodeHighlighted: isDeepNodeHighlighted(opened, nodeHighlighted, type),
opened,
recursive: this.props.recursive,
type
});
}
return this.props.leaf({
...this.props,
nodeHighlighted
});
}
}
exports.default = TreeNode;
_defineProperty(TreeNode, "propTypes", {
branch: _propTypes.default.func.isRequired,
getItemType: _propTypes.default.func.isRequired,
index: _propTypes.default.number,
isAllExpanded: _propTypes.default.bool,
jsonpath: _propTypes.default.string.isRequired,
leaf: _propTypes.default.func.isRequired,
paths: _propTypes.default.oneOfType([_propTypes.default.array, _propTypes.default.object]),
value: _propTypes.default.any,
recursive: _propTypes.default.func.isRequired
});
//# sourceMappingURL=TreeNode.component.js.map