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): optimize collapse behavior #3281

Merged
merged 1 commit into from
Dec 17, 2024
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
16 changes: 9 additions & 7 deletions src/alert/Alert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const Alert = forwardRef<HTMLDivElement, AlertProps>((props, ref) => {
} = useDefaultProps(props, alertDefaultProps);

const [closed, setClosed] = React.useState(false);
const [collapsed, setCollapsed] = React.useState(false);
const [collapsed, setCollapsed] = React.useState(true);

const iconMap = {
success: CheckCircleFilledIcon,
Expand All @@ -62,7 +62,7 @@ const Alert = forwardRef<HTMLDivElement, AlertProps>((props, ref) => {
};

const handleCollapse = () => {
setCollapsed(!collapsed);
setCollapsed((collapsed) => !collapsed);
};

const renderIconNode = () => {
Expand All @@ -75,18 +75,20 @@ const Alert = forwardRef<HTMLDivElement, AlertProps>((props, ref) => {
return (
<div className={`${classPrefix}-alert__description`}>
{message.map((item, index) => {
if (!collapsed) {
if (index < maxLine) {
if (collapsed) {
if (index < +maxLine) {
return <div key={index}>{item}</div>;
}
} else {
return <div key={index}>{item}</div>;
}
return true;
})}
<div className={`${classPrefix}-alert__collapse`} onClick={handleCollapse}>
{!collapsed ? t(local.expandText) : t(local.collapseText)}
</div>
{+maxLine < message.length && (
<div className={`${classPrefix}-alert__collapse`} onClick={handleCollapse}>
{collapsed ? t(local.expandText) : t(local.collapseText)}
</div>
)}
</div>
);
}
Expand Down
7 changes: 5 additions & 2 deletions src/alert/__tests__/alert.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,20 @@ describe('Alert 组件测试', () => {
});

test('message not collapsed', () => {
const massage = [
const message = [
<div key={0}>{text}</div>,
<div key={1}>{text}</div>,
<div key={2} data-testid={testId}>
{text}
</div>,
];
const { container } = render(<Alert title="title content" message={massage} />);
const { container } = render(<Alert title="title content" message={message} />);
const { container: container1 } = render(<Alert title="title content" message={message} maxLine={4} />);

expect(container.querySelector('.t-alert__collapse')).toBeNull();
expect(container.querySelector('.t-alert__collapse')).not.toBeInTheDocument();
expect(container1.querySelector('.t-alert__collapse')).toBeNull();
expect(container1.querySelector('.t-alert__collapse')).not.toBeInTheDocument();
});

test('Alert 展开收起操作', async () => {
Expand Down
Loading