import React, { useEffect, useState, forwardRef, useContext, ReactNode, CSSProperties } from 'react'; import { Button, Spin, ConfigProvider } from 'antd'; import { ButtonHTMLType } from 'antd/es/button/button'; import {ComponentContext} from '../component-context'; export interface DrawerContentProps { prefixCls?: string, // 是否全屏 fullScreen?: boolean, // 是否加载中 loading?: boolean, // 加载中提示文案 loadingTip?: string, // 底部 默认 确定、取消 footer?: ReactNode // 确定按钮类型 okHtmlType?: ButtonHTMLType // 确定按钮文案 okText?: ReactNode // 确定事件 onOk?: () => void // 取消按钮文案 cancelText?: any // 取消事件 onCancel?: () => void // 最外层容器样式 style?: CSSProperties // 内容容器样式 bodyStyle?: CSSProperties } const DrawerContent = forwardRef((props, ref) => { const context = useContext(ComponentContext); const antdContext = useContext(ConfigProvider.ConfigContext); const antdPrefixCls = antdContext.getPrefixCls(); const { loading: propsLoading = false, style = {}, bodyStyle = {}, onOk = () => undefined, onCancel = () => undefined, okHtmlType, children, loadingTip = context.loadingTip, prefixCls = context.prefixCls, fullScreen, footer, okText = context.okText, cancelText = context.cancelText, ...others } = props; const [ loading, setLoading ] = useState(propsLoading); // 多次连续设置loading时,保值loading不间断显示 useEffect(() => { if (propsLoading) { setLoading(true); return null; } const timer = setTimeout(() => { setLoading(false); }, 100); return () => clearTimeout(timer); }, [ propsLoading ]); // 延迟加载内容,解决 内部 input autoFocus 不生效问题 const [ inMounted, setIsMounted ] = useState(false); useEffect(() => { setTimeout(() => setIsMounted(true)); }, []); if (!inMounted) return null; const outerStyle: CSSProperties = { display: 'flex', flexDirection: 'column', ...style, }; return ( <>
{children} {/* footer 站位 */}
{footer !== false ? (
{footer || ( <> )}
) : null} ); }); export default DrawerContent;