/* * @Author: yehuozhili * @Date: 2021-02-04 10:32:45 * @LastEditors: yehuozhili * @LastEditTime: 2021-07-12 17:12:35 * @FilePath: \dooringx\packages\dooringx-lib\src\components\leftConfig.tsx */ import React, { ReactNode, useEffect, useMemo, useState } from 'react'; import { Input, Menu } from 'antd'; import { dragEventResolve, LeftRegistComponentMapItem } from '../core/crossDrag'; import UserConfig from '../config'; import { DoubleLeftOutlined, DoubleRightOutlined, SearchOutlined } from '@ant-design/icons'; import styles from '../index.less'; interface LeftConfigProps { config: UserConfig; footerConfig?: ReactNode; } /** * * 注册加载左侧组件方法,由于异步拉取,所以要异步加载 * 不同tab页可以使用不同type区分 * @param {*} props * @returns */ function LeftConfig(props: LeftConfigProps) { const [menuSelect, setMenuSelect] = useState('0'); const [leftRender, setLeftRender] = useState(null); const leftMapRenderListCategory = useMemo(() => { return props.config.getConfig().leftRenderListCategory; }, [props.config]); const [search, setSearch] = useState(''); useEffect(() => { let cache: LeftRegistComponentMapItem[] = []; const type = leftMapRenderListCategory[parseInt(menuSelect, 10)]?.type; const isCustom = leftMapRenderListCategory[parseInt(menuSelect, 10)]?.custom; if (!isCustom) { const config = props.config.getConfig(); cache = config.leftAllRegistMap.filter((k) => k.type === type); cache.forEach((v) => props.config.asyncRegistComponent(v.component)); setLeftRender(
{leftMapRenderListCategory[parseInt(menuSelect, 10)].displayName}
{ setSearch(e.target.value); }} prefix={} />
{search && search !== '' && cache .reduce((prev, next) => { //筛选搜索条件,name或者displayName存在即显示 if (next.displayName.includes(search) || next.component.includes(search)) { prev.push(next); } return prev; }, []) .map((v, index) => (
{v.imgCustom ? v.imgCustom : component}
{v.displayName}
))} {(!search || search === '') && cache.map((v, index) => (
{v.imgCustom ? v.imgCustom : component}
{v.displayName}
))}
); } else { const render = leftMapRenderListCategory[parseInt(menuSelect, 10)]?.customRender; setLeftRender(
{render}
); } }, [menuSelect, props.config, leftMapRenderListCategory, search]); const [isCollapse, setCollapse] = useState(props.config.getCollapse()); const [renderCollapse, setRenderCollaspe] = useState(props.config.getCollapse()); return (
{leftMapRenderListCategory.map((v, i) => { return ( setMenuSelect(i + '')} icon={v.icon} className={`${styles.menuStyle}`} > {v.displayName} ); })}
{props.footerConfig}
setCollapse((pre) => { if (pre) { setTimeout(() => { props.config.collapsed = false; setRenderCollaspe(false); props.config.getStore().forceUpdate(); }, 300); return !pre; } else { props.config.collapsed = true; setRenderCollaspe(true); props.config.getStore().forceUpdate(); return !pre; } }) } className={styles.menu_footer} icon={isCollapse ? : } >
{!renderCollapse && leftRender}
); } export default LeftConfig;