Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger a modal when clicking on a dropdown item in Semantic UI React?

I have a dropdown and a modal, and I would like to show the modal when one of the dropdown item is clicked. Is it possible? I couldn't find a way to do so because I cannot obtain the target DropdownItem, which is required by Modal's trigger props.

export class Demo extends React.Component<{}, {}> {
  private options = [
    { text: 'doNothing', value: 'doNothing' },
    { text: 'openModal', value: 'openModal' },
  ]
  render() {
    return (
      <div>
        <Dropdown
          fluid
          selection
          options={this.options}
          defaultValue={this.options[0].value} />

        <Modal trigger={<Button>trigger</Button>}>
          <Modal.Header>Select a Photo</Modal.Header>
          <Modal.Content image>
            <Modal.Description>
              <p>Some contents.</p>
            </Modal.Description>
          </Modal.Content>
        </Modal>
      </div>
    )
  }
}
like image 753
Searene Avatar asked Jan 29 '23 22:01

Searene


1 Answers

You could use prop open of Modal to programmatically control it. When you detect that the desired Dropdown item was clicked setState appropriately.

Something among those lines.

import * as React from 'react';

export class Demo extends React.Component<{}, {}> {
  state = {
    options: [
      { text: 'doNothing', value: 'doNothing' },
      { text: 'openModal', value: 'openModal' }
    ],
    open: false
  };

  onClose = () => this.setState({open: false});
  onChange = (selected) => {
    // if the correct one is selected then...
    // this.setState({open: true});
  }

  render() {
    return (
      <div>
        <Dropdown
          fluid
          selection
          options={this.options}
          onChange={this.onChange}
          defaultValue={this.options[0].value} />

        <Modal open={this.state.open} onClose={this.onClose}>
          <Modal.Header>Select a Photo</Modal.Header>
          <Modal.Content image>
            <Modal.Description>
              <p>Some contents.</p>
            </Modal.Description>
          </Modal.Content>
        </Modal>
      </div>
    )
  }
}
like image 194
arpl Avatar answered Jan 31 '23 13:01

arpl