UNPKG

@cainiaofe/cn-ui-m

Version:
60 lines (59 loc) 2.77 kB
import { __assign, __spreadArray } from "tslib"; import { formatMenuButtonToButton } from '@cainiaofe/cn-ui-common'; import React, { useMemo } from 'react'; export function useToolbarButtons(props) { var toolbar = props.toolbar, selectedKeys = props.selectedKeys, selectedDataSource = props.selectedDataSource; return useMemo(function () { var temp = (toolbar === null || toolbar === void 0 ? void 0 : toolbar.buttons) || __spreadArray(__spreadArray([], ((toolbar === null || toolbar === void 0 ? void 0 : toolbar.toolArea) || []), true), __spreadArray([], ((toolbar === null || toolbar === void 0 ? void 0 : toolbar.batchArea) || []), true).reverse(), true); return temp .map(function (item) { try { if (typeof item === 'function') { return item(selectedKeys, selectedDataSource); } return item; } catch (e) { console.error(e); } return null; }) .map(function (item) { var _a, _b; try { // 如果是 ReactElement 直接返回 if (React.isValidElement(item)) { // @ts-ignore 处理buttonGroup if (((_a = item.props) === null || _a === void 0 ? void 0 : _a.optType) === 'buttonGroup') { return __assign(__assign({}, item), { props: __assign(__assign({}, item.props), { children: formatMenuButtonToButton((_b = item.props) === null || _b === void 0 ? void 0 : _b.children) }) }); } else { return item; } } if (typeof (item === null || item === void 0 ? void 0 : item.disabled) === 'function') { var disabled = item.disabled(__spreadArray([], Array.from(selectedKeys), true), selectedDataSource); return __assign(__assign({}, item), { disabled: disabled }); } return item; } catch (e) { console.error(e); } return null; }) .filter(function (item) { var _a; if (React.isValidElement(item)) { return true; } // 无 children 时隐藏不展示 if (!(item === null || item === void 0 ? void 0 : item.children)) return false; // @ts-ignore hack 去掉导入导出组件 if ((_a = item.props) === null || _a === void 0 ? void 0 : _a.createService) return false; return true; }); }, [selectedDataSource, selectedKeys, toolbar]); }