2023-07-31 23:55:25 +08:00
|
|
|
import CloseOutlined from '@ant-design/icons/CloseOutlined';
|
2023-06-16 15:36:28 +08:00
|
|
|
import type { ReactNode } from 'react';
|
|
|
|
import React from 'react';
|
|
|
|
|
|
|
|
function useInnerClosable(
|
|
|
|
closable?: boolean,
|
|
|
|
closeIcon?: boolean | ReactNode,
|
|
|
|
defaultClosable?: boolean,
|
|
|
|
): boolean {
|
|
|
|
if (typeof closable === 'boolean') {
|
|
|
|
return closable;
|
|
|
|
}
|
|
|
|
if (closeIcon === undefined) {
|
|
|
|
return !!defaultClosable;
|
|
|
|
}
|
|
|
|
return closeIcon !== false && closeIcon !== null;
|
|
|
|
}
|
|
|
|
|
|
|
|
export type UseClosableParams = {
|
|
|
|
closable?: boolean;
|
|
|
|
closeIcon?: boolean | ReactNode;
|
|
|
|
defaultClosable?: boolean;
|
|
|
|
defaultCloseIcon?: ReactNode;
|
|
|
|
customCloseIconRender?: (closeIcon: ReactNode) => ReactNode;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function useClosable(
|
|
|
|
closable?: boolean,
|
|
|
|
closeIcon?: boolean | ReactNode,
|
|
|
|
customCloseIconRender?: (closeIcon: ReactNode) => ReactNode,
|
|
|
|
defaultCloseIcon: ReactNode = <CloseOutlined />,
|
|
|
|
defaultClosable = false,
|
|
|
|
): [closable: boolean, closeIcon: React.ReactNode | null] {
|
|
|
|
const mergedClosable = useInnerClosable(closable, closeIcon, defaultClosable);
|
|
|
|
if (!mergedClosable) {
|
|
|
|
return [false, null];
|
|
|
|
}
|
|
|
|
const mergedCloseIcon =
|
|
|
|
typeof closeIcon === 'boolean' || closeIcon === undefined || closeIcon === null
|
|
|
|
? defaultCloseIcon
|
|
|
|
: closeIcon;
|
|
|
|
return [true, customCloseIconRender ? customCloseIconRender(mergedCloseIcon) : mergedCloseIcon];
|
|
|
|
}
|