import React, { useEffect, useRef, useState } from 'react'; import { EngineInterface } from '@aomao/engine'; import classnames from 'classnames-es-ts'; import Button from '../button'; import { useRight } from '../hooks'; import CollapseGroup, { CollapseGroupProps } from './group'; import './index.css'; export type CollapseProps = { header?: React.ReactNode; groups: Array; engine?: EngineInterface; className?: string; icon?: React.ReactNode; content?: React.ReactNode | ((engine?: EngineInterface) => React.ReactNode); onSelect?: (event: React.MouseEvent, name: string) => void | boolean; }; const Collapse: React.FC = ({ icon, content, header, groups, engine, className, onSelect, }) => { const isCustomize = !!!(icon || content); const [visible, setVisible] = useState(isCustomize); const collapseRef = useRef(null); const isRight = useRight(collapseRef); useEffect(() => { if (!isCustomize) return () => document.removeEventListener('click', hide); return; }, [isCustomize]); const show = () => { setVisible(true); setTimeout(() => { document.addEventListener('click', hide); }, 10); }; const hide = (event?: MouseEvent) => { if (event) { let node = event.target; // while (node) { // if (node === collapseRef.current) { // return; // } // node = (node as Element).parentNode; // } } document.removeEventListener('click', hide); setVisible(false); }; const toggle = () => { if (visible) { hide(); } else { show(); } }; return (
{!isCustomize && (