ModalProvider模态框提供者

模态框提供者。

何时使用

代码演示

ModalProvider 模态框提供者 - 图1

基本使用

基本使用。

  1. import { ModalProvider, Modal, Button } from 'choerodon-ui/pro';
  2. const Context = React.createContext('');
  3. const { injectModal, useModal } = ModalProvider;
  4. const ModalContent = () => {
  5. const context = React.useContext(Context);
  6. return context ? `Modal with context<${context}>` : 'Modal without context';
  7. };
  8. const openModal = (modal, title, context) => {
  9. modal.open({
  10. title: 'Inner',
  11. children: <ModalContent />,
  12. onOk: () => Modal.confirm('This is normal Modal confirm'),
  13. });
  14. };
  15. const InnterModal = () => {
  16. const modal = useModal();
  17. const handleClick = React.useCallback(() => openModal(modal, 'Inner'), []);
  18. return <Button onClick={handleClick}>Open inner modal</Button>;
  19. };
  20. @injectModal
  21. class OuterModal extends React.Component {
  22. handleClick = () => {
  23. const { Modal: modal } = this.props;
  24. openModal(modal, 'Outer');
  25. };
  26. render() {
  27. return (
  28. <>
  29. <Button onClick={this.handleClick}>Open outer modal</Button>
  30. <Context.Provider value="provider">
  31. <ModalProvider>
  32. <InnterModal />
  33. </ModalProvider>
  34. </Context.Provider>
  35. </>
  36. );
  37. }
  38. }
  39. ReactDOM.render(<OuterModal />, mountNode);

API

ModalProvider

参数说明类型默认值
location路由的 location 对象。当设定了 location,路由变更时会自动清空所有已打开的 Modal{ pathname: string }