Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(alert): support className and ref #1713

Merged
merged 1 commit into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 53 additions & 53 deletions src/alert/Alert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,62 +12,62 @@ import {
import IconButton from '../button/IconButton';
import { AlertProps } from './interfaces';

export const Alert: React.FC<AlertProps> = ({
message,
description,
closeable,
showIcon = false,
onClose,
icon,
type = 'info',
style,
...restProps
}: AlertProps) => {
const prefixCls = usePrefixCls('alert');
const [alertStatus, setAlertStatus] = useState(true);
export const Alert = React.forwardRef<HTMLDivElement, AlertProps>(
(
{ message, description, closeable, showIcon = false, onClose, icon, type = 'info', className, ...restProps },
ref
) => {
const prefixCls = usePrefixCls('alert');
const [alertStatus, setAlertStatus] = useState(true);

const getIcon = () => {
switch (type) {
case 'success':
return <CheckCircleFilled />;
case 'warning':
return <WarningCircleFilled />;
case 'error':
return <CloseCircleFilled />;
case 'info':
return <InfoCircleFilled />;
default:
return icon || <InfoCircleFilled />;
}
};
const getIcon = () => {
switch (type) {
case 'success':
return <CheckCircleFilled />;
case 'warning':
return <WarningCircleFilled />;
case 'error':
return <CloseCircleFilled />;
case 'info':
return <InfoCircleFilled />;
default:
return icon || <InfoCircleFilled />;
}
};

const closeAlert = () => {
setAlertStatus(false);
onClose?.();
};
const closeAlert = () => {
setAlertStatus(false);
onClose?.();
};

return alertStatus ? (
<div style={style} className={classnames(prefixCls, `${prefixCls}-${type}`)} data-testid="alert" {...restProps}>
{showIcon && <div className={classnames(`${prefixCls}-icon`)}>{getIcon()}</div>}
<div className={classnames(showIcon ? null : `${prefixCls}-content-no-icon`, `${prefixCls}-content`)}>
{message && <div className={classnames(`${prefixCls}-content-title`)}>{message}</div>}
{message && description && <div className={classnames(`${prefixCls}-content-gap`)} />}
{description && <div className={classnames(`${prefixCls}-content-description`)}>{description}</div>}
return alertStatus ? (
<div
className={classnames(prefixCls, `${prefixCls}-${type}`, className)}
data-testid="alert"
ref={ref}
{...restProps}
>
{showIcon && <div className={classnames(`${prefixCls}-icon`)}>{getIcon()}</div>}
<div className={classnames(`${prefixCls}-content`, { [`${prefixCls}-content-no-icon`]: !showIcon })}>
{message && <div className={classnames(`${prefixCls}-content-title`)}>{message}</div>}
{message && description && <div className={classnames(`${prefixCls}-content-gap`)} />}
{description && <div className={classnames(`${prefixCls}-content-description`)}>{description}</div>}
</div>
{closeable && (
<IconButton
className={classnames(`${prefixCls}-closeButton`)}
onClick={closeAlert}
tabIndex={0}
onKeyPress={_.noop}
type="text"
size="small"
>
<CloseOutlined />
</IconButton>
)}
</div>
{closeable && (
<IconButton
className={classnames(`${prefixCls}-closeButton`)}
onClick={closeAlert}
tabIndex={0}
onKeyPress={_.noop}
type="text"
size="small"
>
<CloseOutlined />
</IconButton>
)}
</div>
) : null;
};
) : null;
}
);

export default Alert;
6 changes: 1 addition & 5 deletions src/alert/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface AlertProps {
export interface AlertProps extends React.HTMLAttributes<HTMLDivElement> {
/**
指定警告提示的样式
*/
Expand Down Expand Up @@ -27,8 +27,4 @@ export interface AlertProps {
关闭时触发的回调函数
*/
onClose?: () => void;
/**
外层div样式
*/
style?: React.CSSProperties;
}
2 changes: 1 addition & 1 deletion src/alert/style/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
width: 30px;
margin: 0;
padding: 0;
font-weight: @weight-font-semi-bold;
font-weight: 600;
border: none;
cursor: pointer;
}
Expand Down