From ee276c65c8e81ede746d54f6ddd27f566fa29b45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BFCai?= Date: Mon, 11 Nov 2024 16:45:25 +0800 Subject: [PATCH] fix(Drawer): use `FooterButton` parm define `confirmBtn` and `closeBtn` are invalid --- src/drawer/Drawer.tsx | 110 +++++++++++++++++---------- src/drawer/__tests__/drawer.test.tsx | 46 ++++++++++- 2 files changed, 110 insertions(+), 46 deletions(-) diff --git a/src/drawer/Drawer.tsx b/src/drawer/Drawer.tsx index 559574b54b..799ce4505b 100644 --- a/src/drawer/Drawer.tsx +++ b/src/drawer/Drawer.tsx @@ -1,12 +1,16 @@ -import React, { forwardRef, useState, useEffect, useImperativeHandle, useRef, useMemo } from 'react'; +import React, { forwardRef, useState, useEffect, useImperativeHandle, useRef, useMemo, isValidElement } from 'react'; import classnames from 'classnames'; +import isString from 'lodash/isString'; +import isObject from 'lodash/isObject'; +import isFunction from 'lodash/isFunction'; + import { CSSTransition } from 'react-transition-group'; import { CloseIcon as TdCloseIcon } from 'tdesign-icons-react'; import { useLocaleReceiver } from '../locale/LocalReceiver'; import { TdDrawerProps, DrawerEventSource } from './type'; import { StyledProps } from '../common'; -import Button from '../button'; +import Button, { ButtonProps } from '../button'; import useConfig from '../hooks/useConfig'; import useGlobalIcon from '../hooks/useGlobalIcon'; import { drawerDefaultProps } from './defaultProps'; @@ -14,6 +18,7 @@ import useDrag from './hooks/useDrag'; import Portal from '../common/Portal'; import useLockStyle from './hooks/useLockStyle'; import useDefaultProps from '../hooks/useDefaultProps'; +import parseTNode from '../_util/parseTNode'; export const CloseTriggerType: { [key: string]: DrawerEventSource } = { CLICK_OVERLAY: 'overlay', @@ -25,6 +30,12 @@ export const CloseTriggerType: { [key: string]: DrawerEventSource } = { export interface DrawerProps extends TdDrawerProps, StyledProps {} const Drawer = forwardRef((originalProps, ref) => { + // 国际化文本初始化 + const [local, t] = useLocaleReceiver('drawer'); + const { CloseIcon } = useGlobalIcon({ CloseIcon: TdCloseIcon }); + const confirmText = t(local.confirm); + const cancelText = t(local.cancel); + const props = useDefaultProps(originalProps, drawerDefaultProps); const { className, @@ -49,28 +60,22 @@ const Drawer = forwardRef((originalProps, ref) => { body, footer, closeBtn, - cancelBtn, - confirmBtn, + cancelBtn = cancelText, + confirmBtn = confirmText, zIndex, destroyOnClose, sizeDraggable, forceRender, } = props; - // 国际化文本初始化 - const [local, t] = useLocaleReceiver('drawer'); - const { CloseIcon } = useGlobalIcon({ CloseIcon: TdCloseIcon }); const size = propsSize ?? local.size; - const confirmText = t(local.confirm); - const cancelText = t(local.cancel); - const { classPrefix } = useConfig(); const maskRef = useRef(); const containerRef = useRef(); const drawerWrapperRef = useRef(); // 即最终的 attach dom,默认为 document.body const prefixCls = `${classPrefix}-drawer`; - const closeIcon = React.isValidElement(closeBtn) ? closeBtn : ; + const closeIcon = isValidElement(closeBtn) ? closeBtn : ; const { dragSizeValue, enableDrag, draggableLineStyles } = useDrag(placement, sizeDraggable, onSizeDragEnd); const [animationStart, setAnimationStart] = useState(visible); @@ -119,37 +124,59 @@ const Drawer = forwardRef((originalProps, ref) => { [visible, placement, sizeValue, animationStart], ); - function getFooter(): React.ReactNode { - if (footer !== true) return footer; - - const defaultCancelBtn = ( - - ); - - const defaultConfirmBtn = ( - - ); - - const renderCancelBtn = cancelBtn && React.isValidElement(cancelBtn) ? cancelBtn : defaultCancelBtn; - const renderConfirmBtn = confirmBtn && React.isValidElement(confirmBtn) ? confirmBtn : defaultConfirmBtn; - - const footerStyle = { - display: 'flex', - justifyContent: placement === 'right' ? 'flex-start' : 'flex-end', + const renderDrawerButton = (btn: DrawerProps['cancelBtn'], defaultProps: ButtonProps) => { + let result = null; + + if (isString(btn)) { + result = ; + } else if (isValidElement(btn)) { + result = btn; + } else if (isObject(btn)) { + result =