UNPKG

ds-markdown

Version:

> πŸš€ React Markdown ζ‰“ε­—εŠ¨η”»η»„δ»ΆοΌŒζδΎ›ηŽ°δ»£θŠε€©η•Œι’ζ•ˆζžœ

46 lines β€’ 1.64 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 { // If onClick is not async, call directly 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