Search code examples
reactjsreact-hooksantd

Trigger modal from dropdown menu click event antd


So i have a <Modal/> which is controlled by a state Visible, i want to be able to change the state of this modal from a dropdown menu inside a table row from Antd using SetVisible but i cannot find a way to do it, any suggestions?

    <Table // antd table 
        loading={Boolean(!data)} 
        rowKey={'id'} expandable={expandable}  
        pagination={{pageSize: 5}} 
        columns={columns} 
        dataSource={data}
    />

    render: (text, record) => ( // fragment from column object, which contain in column that'll be rendered
    <Space size="middle">
        <Dropdown overlay={() => DropMenu()}> //Where the dropdown compoennt is being called
            <a className="ant-dropdown-link" onClick={e => e.preventDefault()}>
                Mais Opções <DownOutlined />
            </a>
        </Dropdown>
    </Space>

  const DropMenu = () => { //Dropdown Component

  return (
    <Menu>
      <Menu.Item>
        <a target="_blank" rel="noopener noreferrer" href="http://www.taobao.com/"> // Here I'll trigger the modal
          trigger modal
        </a>
      </Menu.Item>
    </Menu>
  )
};

Solution

  • I actually realized that antd has a Modal.confirm() which is a function that renders a confirmation modal with an ok and cancel button which you handle your own way.

    <Menu.Item danger>
      <span onClick={() => confirm({id: record.id})}>Delete</span>
    </Menu.Item>
    
    function confirm({name, id}) {
       Modal.confirm({
          title: 'Confirm',
          icon: <ExclamationCircleOutlined />,
          content: 'Are you sure you want to delete this user??',
          okText: 'Confirmar',
          cancelText: 'Cancelar',
          onOk: (close) => { ...handling deletion }
       )}
    }