在ts-react使用Modal

在ts-react使用Modal

创建一个 Modal

import React, { useState, forwardRef, useImperativeHandle } from 'react';
import { Modal } from 'antd';

type TableItem = {
  key: React.Key;
  name: string;
  diff: number;
  temp: number;
};

interface RefProps {
  handleOpen: (resData: any) => void;
  handleClose: () => void;
}

interface DefaultProps {
  title:string;
}

export interface TestModalRefProps extends RefProps {}

/**
 * @component TestModal 弹框
 */
const TestModal = forwardRef<RefProps, DefaultProps>(({ title }, ref) => {
  const [visible, setVisible] = useState<boolean>(false);
  const [dataSource, setDataSource] = useState<TableItem[]>();

  useImperativeHandle(
    ref,
    (): RefProps => ({
      handleOpen,
      handleClose,
    }),
  );

  /**
   * @title 打开弹框 
   *  如不需要在打开弹框时就取得参数,可删除
   * @param resData
   * @param {String} title
   */
  function handleOpen(resData: any): void {
    setVisible(true);
    setDataSource(resData);
  }

  function handleClose(): void {
    setVisible(false);
  }

  return (
    <Modal
      title={title}
      width={1200}
      destroyOnClose={true}
      visible={visible}
      onOk={handleClose}
      onCancel={handleClose}
      footer={false}
    >
     content
    </Modal>
  );
});

export default TestModal;


父组件中使用

import React, {useRef}
import TestModal, { TestModalRefProps} from "TestModal";

const Parent: React.FC = () => {
  const TestMOdalRef = useRef<TestModalRefProps>(null);
  
  return (
    <div>
    <Button onCLick={ ()=> TestMOdalRef.current?.handleOpen([]) }></Buttom>
    <TestModal ref={TestMOdalRef}></TestModal>
    </div> 
  );
};
export default Parent;
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值