ant-design/components/popconfirm/demo/dynamic-trigger.md
章鱼 7fd093bd0a
docs: feat components TS demo (#34742)
* docs: add general components TS demo

* docs: add layout components TS demo

* docs: add navigation components TS demo

* docs: add data entry components TS demo

* chore(deps): add types for qs

* docs: add data display TS demo

* docs: add feedback components TS demo

* docs: add other components TS demo

* chore(deps): add types

* docs: unified demo code style

* docs: fix lint error

* docs: add demo TS type

* docs: fix demo TS type

* test: update snapshot

* docs: fix TS demo

* feat: update Rate character type

* docs: fix lint error

* feat: update Rate character type

* feat: update Rate character type
2022-05-19 09:46:26 +08:00

1.4 KiB
Executable File
Raw Blame History

order title
3
zh-CN en-US
条件触发 Conditional trigger

zh-CN

可以判断是否需要弹出。

en-US

Make it pop up under some conditions.

import React, { useState } from 'react';
import { Popconfirm, Switch, message } from 'antd';

const App: React.FC = () => {
  const [visible, setVisible] = useState(false);
  const [condition, setCondition] = useState(true);

  const changeCondition = (checked: boolean) => {
    setCondition(checked);
  };

  const confirm = () => {
    setVisible(false);
    message.success('Next step.');
  };

  const cancel = () => {
    setVisible(false);
    message.error('Click on cancel.');
  };

  const handleVisibleChange = (newVisible: boolean) => {
    if (!newVisible) {
      setVisible(newVisible);
      return;
    }
    // Determining condition before show the popconfirm.
    console.log(condition);
    if (condition) {
      confirm(); // next step
    } else {
      setVisible(newVisible);
    }
  };

  return (
    <div>
      <Popconfirm
        title="Are you sure delete this task?"
        visible={visible}
        onVisibleChange={handleVisibleChange}
        onConfirm={confirm}
        onCancel={cancel}
        okText="Yes"
        cancelText="No"
      >
        <a href="#">Delete a task</a>
      </Popconfirm>
      <br />
      <br />
      Whether directly execute
      <Switch defaultChecked onChange={changeCondition} />
    </div>
  );
};

export default App;