react-sortable-tree-node
Version:
react-sortable-tree-node
59 lines (46 loc) • 2.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.defaultGetNodeKey = defaultGetNodeKey;
exports.defaultSearchMethod = defaultSearchMethod;
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function defaultGetNodeKey(_ref) {
var treeIndex = _ref.treeIndex;
return treeIndex;
} // Cheap hack to get the text of a react object
function getReactElementText(parent) {
if (typeof parent === 'string') {
return parent;
}
if (_typeof(parent) !== 'object' || !parent.props || !parent.props.children || typeof parent.props.children !== 'string' && _typeof(parent.props.children) !== 'object') {
return '';
}
if (typeof parent.props.children === 'string') {
return parent.props.children;
}
return parent.props.children.map(function (child) {
return getReactElementText(child);
}).join('');
} // Search for a query string inside a node property
function stringSearch(key, searchQuery, node, path, treeIndex) {
if (typeof node[key] === 'function') {
// Search within text after calling its function to generate the text
return String(node[key]({
node: node,
path: path,
treeIndex: treeIndex
})).indexOf(searchQuery) > -1;
} else if (_typeof(node[key]) === 'object') {
// Search within text inside react elements
return getReactElementText(node[key]).indexOf(searchQuery) > -1;
} // Search within string
return node[key] && String(node[key]).indexOf(searchQuery) > -1;
}
function defaultSearchMethod(_ref2) {
var node = _ref2.node,
path = _ref2.path,
treeIndex = _ref2.treeIndex,
searchQuery = _ref2.searchQuery;
return stringSearch('title', searchQuery, node, path, treeIndex) || stringSearch('subtitle', searchQuery, node, path, treeIndex);
}