UNPKG

ds-markdown

Version:

> 🚀 React Markdown 打字动画组件,提供现代聊天界面效果

46 lines 1.65 kB
import { jsx as _jsx } from "react/jsx-runtime"; import { useEffect, useRef, useState } from 'react'; import Button from '../../ui/Button/index.js'; import { CheckMarkIcon } from '../../Icon/index.js'; const SuccessButton = (props) => { const { onClick, icon, executeText, children, ...rest } = props; const [isLoading, setIsLoading] = useState(false); const [isSuccess, setIsSuccess] = useState(false); const isUnmounted = useRef(false); const handleClick = async () => { if (isLoading || isSuccess) { return; } try { // 如果onClick不是异步函数,则直接调用 const returnValue = onClick(); if (returnValue instanceof Promise) { setIsLoading(true); const result = await returnValue; if (result) { setIsSuccess(true); setTimeout(() => { if (!isUnmounted.current) { setIsSuccess(false); } }, 1000); } } } catch (error) { setIsSuccess(false); } finally { setIsLoading(false); } }; useEffect(() => { isUnmounted.current = false; return () => { isUnmounted.current = true; }; }, []); return (_jsx(Button, { ...rest, onClick: handleClick, icon: isSuccess ? _jsx(CheckMarkIcon, { size: 24 }) : icon, children: isSuccess ? executeText || children : children })); }; export default SuccessButton; //# sourceMappingURL=index.js.map