rsuite
Version:
A suite of react components
99 lines • 2.59 kB
JavaScript
'use client';
import React from 'react';
import trim from 'lodash/trim';
import { KEY_VALUES } from "../constants/index.js";
import { findNodeOfTree } from "../Tree/utils/index.js";
import { reactToString } from "../utils/index.js";
const defaultNodeKeys = {
valueKey: 'value',
childrenKey: 'children'
};
export function createConcatChildrenFunction(node, nodeValue, nodeKeys = defaultNodeKeys) {
const {
valueKey,
childrenKey
} = nodeKeys;
return (data, children) => {
if (nodeValue) {
node = findNodeOfTree(data, item => nodeValue === item[valueKey]);
}
node[childrenKey] = children;
return data.concat([]);
};
}
export function shouldDisplay(label, searchKeyword) {
if (!trim(searchKeyword)) {
return true;
}
const keyword = searchKeyword.toLocaleLowerCase();
if (typeof label === 'string' || typeof label === 'number') {
return `${label}`.toLocaleLowerCase().indexOf(keyword) >= 0;
} else if (/*#__PURE__*/React.isValidElement(label)) {
const nodes = reactToString(label);
return nodes.join('').toLocaleLowerCase().indexOf(keyword) >= 0;
}
return false;
}
/**
* Handling keyboard events...
* @param event Keyboard event object
* @param events Event callback functions
*/
export function onMenuKeyDown(event, events) {
const {
down,
up,
enter,
del,
esc,
right,
left,
home,
end
} = events;
switch (event.key) {
// down
case KEY_VALUES.DOWN:
down === null || down === void 0 || down(event);
event.preventDefault();
break;
// up
case KEY_VALUES.UP:
up === null || up === void 0 || up(event);
event.preventDefault();
break;
// enter
case KEY_VALUES.ENTER:
enter === null || enter === void 0 || enter(event);
event.preventDefault();
break;
// delete
case KEY_VALUES.BACKSPACE:
del === null || del === void 0 || del(event);
break;
// esc | tab
case KEY_VALUES.ESC:
case KEY_VALUES.TAB:
esc === null || esc === void 0 || esc(event);
break;
// left arrow
case KEY_VALUES.LEFT:
left === null || left === void 0 || left(event);
break;
// right arrow
case KEY_VALUES.RIGHT:
right === null || right === void 0 || right(event);
break;
// home
case KEY_VALUES.HOME:
home === null || home === void 0 || home(event);
event.preventDefault();
break;
// end
case KEY_VALUES.END:
end === null || end === void 0 || end(event);
event.preventDefault();
break;
default:
}
}