请问如何进行使用添加Button在antd/Collapse 的label上,点击Button,不触发折叠的事件?
基于antd 的 折叠块 我使用此代码做到:
折叠块的使用(添加了一个按钮)
import type { CollapseProps } from 'antd';
import { Collapse, Button } from 'antd';
const text = `
A dog is a type of domesticated animal.
Known for its loyalty and faithfulness,
it can be found as a welcome guest in many households across the world.
`;
const items: CollapseProps['items'] = [
{
key: '1',
label: <div><span>This is panel header 1</span><Button
onClick={(e) => {
if(e.preventDefault) {
e.preventDefault()
}
console.log(e)
console.log('123')
}}
>您好</Button></div>,
children: <p>{text}</p>,
},
{
key: '2',
label: 'This is panel header 2',
children: <p>{text}</p>,
},
{
key: '3',
label: 'This is panel header 3',
children: <p>{text}</p>,
},
];
const TestComp: React.FC = () => {
const onChange = (key: string | string[]) => {
console.log(key);
};
return <Collapse items={items} defaultActiveKey={['1']} onChange={onChange} />;
};
export default TestComp;
基于此折叠款,我加了一个按钮,但是现在有一个需求就是点击折叠块上面的header可以折叠,但是我不想点击button它也折叠。我尝试了用:
if(e.preventDefault) {
e.preventDefault()
}
但是并不生效。
回复
1个回答

test
2024-06-27
<Button
onClick={(e) => {
e.stopPropagation(); // 阻止事件冒泡
console.log('Button clicked');
}}
>
您好
</Button>
回复

适合作为回答的
- 经过验证的有效解决办法
- 自己的经验指引,对解决问题有帮助
- 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
- 询问内容细节或回复楼层
- 与题目无关的内容
- “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容