最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Semantic react ui Popup close button - Stack Overflow

programmeradmin0浏览0评论

I'm using semantic-react-ui's Popup component and I was wondering how to trigger close popup event by clicking a button inside the popup without using jquery.

Thank you

I'm using semantic-react-ui's Popup component and I was wondering how to trigger close popup event by clicking a button inside the popup without using jquery.

Thank you

Share Improve this question edited Jun 22, 2018 at 12:14 Oleksandr Fediashov 4,3351 gold badge25 silver badges43 bronze badges asked Feb 15, 2018 at 23:25 riyozriyoz 4311 gold badge6 silver badges15 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 15

According to the docs, you have to create a controlled Popup.
Create a component which nests the Popup component, and maintain a state in it:

class ControlledPopup extends React.Component {
  constructor() {
    super();
    this.state = {
      isOpen: false
    };                  // state to control the state of popup
  }
  
  handleOpen = () => {
    this.setState({ isOpen: true });
  }
  
  handleClose = () => {
    this.setState({ isOpen: false });
  }
  
  render() {
    return (
      <div>
        <Popup
          trigger={<button>click to open</button>}
          content={<button onClick={this.handleClose}>click to close</button>}
          on='click'
          open={this.state.isOpen}
          onOpen={this.handleOpen}
        />
      </div>
    );
  }
}

Here is the TS + React Hooks version of the above:

import { FC, useState } from 'react';

import { Popup } from 'semantic-ui-react';

const ControlledPopup: FC = () => {
    const [isOpen, setIsOpen] = useState(false);
    return (
        <Popup
            trigger={<button>click to open</button>}
            content={<button onClick={() => setIsOpen(!isOpen)}>click to close</button>}
            on="click"
            open={isOpen}
            onOpen={() => setIsOpen(!isOpen)}
        />
    );
};

export default ControlledPopup;
发布评论

评论列表(0)

  1. 暂无评论