reactjs 如何在Ant设计列表项中添加onClick事件?

vnjpjtjt  于 2022-12-26  发布在  React
关注(0)|答案(2)|浏览(385)

我想在点击列表项时触发函数,但它不在列表项中。在我的代码中。

{suggestions.length > 0 &&
                            <List
                                className={classes['address-suggestions']}
                                size="small"
                                bordered
                                dataSource={suggestions}
                                renderItem={suggestion => <List.Item onClick={handleSelect}>
                                    {suggestion.description}
                                </List.Item>}
                            />

                        }
qacovj5a

qacovj5a1#

文档中的这个扩展示例适合我:

import React from 'react';
import ReactDOM from 'react-dom';
import 'antd/dist/antd.css';
import './index.css';
import { List, Avatar } from 'antd';

const data = [
  {
    title: 'Ant Design Title 1',
  },
  {
    title: 'Ant Design Title 2',
  },
  {
    title: 'Ant Design Title 3',
  },
  {
    title: 'Ant Design Title 4',
  },
];

ReactDOM.render(
  <List
    itemLayout="horizontal"
    dataSource={data}
    renderItem={item => (
      <List.Item onClick={() => console.log(item.title)}> // <-- on click handler
        <List.Item.Meta
          avatar={<Avatar src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png" />}
          title={<span>{item.title}</span>}
          description="Ant Design, a design language for background applications, is refined by Ant UED Team"
        />
      </List.Item>
    )}
  />,
  document.getElementById('container'),
);
nukf8bse

nukf8bse2#

和在Antdv4.x中一样,我没有找到一种方法来将我的h4元素从 metatitle属性绑定到onclick事件处理程序,我只是复制了生成的HTML及其类,并用这个生成的HTML替换了List.Item.Meta。
因此,我在组件的JSX中得到了类似这样的结果:

<List.Item actions={ListItemActions}>
        <Switch checked={isChecked} onChange={onSwitchChange} />

        {/* this was List.Item.Meta before */}
        <div className="ant-list-item-meta">
            <div className="ant-list-item-meta-content">
                <h4 onClick={() => editHandler(todo)} className="ant-list-item-meta-title cursor-pointer">{todo.title}</h4>
            </div>
        </div>
        {/* end of copied HTML */}

        <DescriptionIcon />
    </List.Item>

希望能有所帮助

相关问题