Dropdowns

The Dropdown component is used to pass the isOpen & toggle props via context to the following components: DropdownToggle, DropdownMenu. The DropdownToggle uses the Button component internally, meaning it also accepts all the props the Button component accepts.

Dropdowns - 图1

  1. import React from 'react';
  2. import { Dropdown, DropdownToggle, DropdownMenu, DropdownItem } from 'reactstrap';
  3. export default class Example extends React.Component {
  4. constructor(props) {
  5. super(props);
  6. this.toggle = this.toggle.bind(this);
  7. this.state = {
  8. dropdownOpen: false
  9. };
  10. }
  11. toggle() {
  12. this.setState(prevState => ({
  13. dropdownOpen: !prevState.dropdownOpen
  14. }));
  15. }
  16. render() {
  17. return (
  18. <Dropdown isOpen={this.state.dropdownOpen} toggle={this.toggle}>
  19. <DropdownToggle caret>
  20. Dropdown
  21. </DropdownToggle>
  22. <DropdownMenu>
  23. <DropdownItem header>Header</DropdownItem>
  24. <DropdownItem>Some Action</DropdownItem>
  25. <DropdownItem disabled>Action (disabled)</DropdownItem>
  26. <DropdownItem divider />
  27. <DropdownItem>Foo Action</DropdownItem>
  28. <DropdownItem>Bar Action</DropdownItem>
  29. <DropdownItem>Quo Action</DropdownItem>
  30. </DropdownMenu>
  31. </Dropdown>
  32. );
  33. }
  34. }

Properties

  1. Dropdown.propTypes = {
  2. disabled: PropTypes.bool,
  3. direction: PropTypes.oneOf(['up', 'down', 'left', 'right']),
  4. group: PropTypes.bool,
  5. isOpen: PropTypes.bool,
  6. // For Dropdown usage inside a Nav
  7. nav: PropTypes.bool,
  8. active: PropTypes.bool,
  9. // For Dropdown usage inside a Navbar (disables popper)
  10. inNavbar: PropTypes.bool,
  11. tag: PropTypes.string, // default: 'div' unless nav=true, then 'li'
  12. toggle: PropTypes.func,
  13. setActiveFromChild: PropTypes.bool
  14. };
  15. DropdownToggle.propTypes = {
  16. caret: PropTypes.bool,
  17. color: PropTypes.string,
  18. className: PropTypes.string,
  19. disabled: PropTypes.bool,
  20. onClick: PropTypes.func,
  21. 'data-toggle': PropTypes.string,
  22. 'aria-haspopup': PropTypes.bool,
  23. // For DropdownToggle usage inside a Nav
  24. nav: PropTypes.bool,
  25. // Defaults to Button component
  26. tag: PropTypes.any
  27. };
  28. DropdownMenu.propTypes = {
  29. tag: PropTypes.string,
  30. children: PropTypes.node.isRequired,
  31. right: PropTypes.bool,
  32. flip: PropTypes.bool, // default: true,
  33. className: PropTypes.string,
  34. cssModule: PropTypes.object,
  35. // Custom modifiers that are passed to DropdownMenu.js, see https://popper.js.org/popper-documentation.html#modifiers
  36. modifiers: PropTypes.object,
  37. persist: PropTypes.bool, // presist the popper, even when closed. See #779 for reasoning
  38. // passed to popper, see https://popper.js.org/popper-documentation.html#Popper.Defaults.positionFixed
  39. positionFixed: PropTypes.bool
  40. };
  41. DropdownItem.propTypes = {
  42. children: PropTypes.node,
  43. active: PropTypes.bool,
  44. disabled: PropTypes.bool,
  45. divider: PropTypes.bool,
  46. tag: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
  47. header: PropTypes.bool,
  48. onClick: PropTypes.func,
  49. className: PropTypes.string,
  50. cssModule: PropTypes.object,
  51. toggle: PropTypes.bool // default: true
  52. };

Alignment

To align the DropdownMenu to the right, add a right prop to Dropdown.

Dropdowns - 图2

  1. <Dropdown isOpen={this.state.dropdownOpen} toggle={this.toggle}>
  2. <DropdownToggle caret>
  3. This dropdown's menu is right-aligned
  4. </DropdownToggle>
  5. <DropdownMenu right>
  6. <DropdownItem header>Header</DropdownItem>
  7. <DropdownItem disabled>Action</DropdownItem>
  8. <DropdownItem>Another Action</DropdownItem>
  9. <DropdownItem divider/>
  10. <DropdownItem>Another Action</DropdownItem>
  11. </DropdownMenu>
  12. </Dropdown>

Menu Headers

  1. <DropdownItem header>Header</DropdownItem>

Menu Dividers

  1. <DropdownItem divider/>

Menu Items

  1. <DropdownItem>Action</DropdownItem>

Disabled Menu Items

  1. <DropdownItem disabled>Action</DropdownItem>

Sizing

Dropdowns - 图3

  1. <Dropdown group isOpen={this.state.dropdownOpen} size="lg" toggle={this.toggle}>
  2. <DropdownToggle caret>
  3. Dropdown
  4. </DropdownToggle>
  5. <DropdownMenu>
  6. ...
  7. </DropdownMenu>
  8. </Dropdown>
  9. <Dropdown isOpen={this.state.dropdownOpen} toggle={this.toggle}>
  10. <DropdownToggle caret>
  11. Dropdown
  12. </DropdownToggle>
  13. <DropdownMenu>
  14. ...
  15. </DropdownMenu>
  16. </Dropdown>
  17. <Dropdown group isOpen={this.state.dropdownOpen} size="sm" toggle={this.toggle}>
  18. <DropdownToggle caret>
  19. Dropdown
  20. </DropdownToggle>
  21. <DropdownMenu>
  22. ...
  23. </DropdownMenu>
  24. </Dropdown>

Custom Dropdown

Dropdowns - 图4

  1. import React from 'react';
  2. import { Dropdown, DropdownMenu, DropdownToggle } from 'reactstrap';
  3. export default class Example extends React.Component {
  4. constructor(props) {
  5. super(props);
  6. this.toggle = this.toggle.bind(this);
  7. this.state = {
  8. dropdownOpen: false
  9. };
  10. }
  11. toggle() {
  12. this.setState({
  13. dropdownOpen: !this.state.dropdownOpen
  14. });
  15. }
  16. render() {
  17. return (
  18. <Dropdown isOpen={this.state.dropdownOpen} toggle={this.toggle}>
  19. <DropdownToggle
  20. tag="span"
  21. onClick={this.toggle}
  22. data-toggle="dropdown"
  23. aria-expanded={this.state.dropdownOpen}
  24. >
  25. Custom Dropdown Content
  26. </DropdownToggle>
  27. <DropdownMenu>
  28. <div onClick={this.toggle}>Custom dropdown item</div>
  29. <div onClick={this.toggle}>Custom dropdown item</div>
  30. <div onClick={this.toggle}>Custom dropdown item</div>
  31. <div onClick={this.toggle}>Custom dropdown item</div>
  32. </DropdownMenu>
  33. </Dropdown>
  34. );
  35. }
  36. }

Uncontrolled Dropdown

For the most basic use-case an uncontrolled component can provide the functionality wanted without the need to manage/control the state of the component. UncontrolledDropdown does not require isOpen nor toggle props to work. For the ButtonDropdown flavor, an uncontrolled component have been made as well; UncontrolledButtonDropdown.

Dropdowns - 图5

  1. import React from 'react';
  2. import { UncontrolledDropdown, DropdownToggle, DropdownMenu, DropdownItem } from 'reactstrap';
  3. export default function Example () {
  4. return (
  5. <UncontrolledDropdown>
  6. <DropdownToggle caret>
  7. Dropdown
  8. </DropdownToggle>
  9. <DropdownMenu>
  10. <DropdownItem header>Header</DropdownItem>
  11. <DropdownItem disabled>Action</DropdownItem>
  12. <DropdownItem>Another Action</DropdownItem>
  13. <DropdownItem divider />
  14. <DropdownItem>Another Action</DropdownItem>
  15. </DropdownMenu>
  16. </UncontrolledDropdown>
  17. );
  18. }

Drop direction variations

Dropdowns - 图6

  1. <Dropdown direction="up" isOpen={isOpen} toggle={toggle}>
  2. <DropdownToggle caret>
  3. Dropup
  4. </DropdownToggle>
  5. <DropdownMenu>
  6. <DropdownItem>Another Action</DropdownItem>
  7. <DropdownItem>Another Action</DropdownItem>
  8. </DropdownMenu>
  9. </Dropdown>
  10. <Dropdown direction="left" isOpen={this.state.btnDropleft} toggle={() => { this.setState({ btnDropleft: !this.state.btnDropleft }); }}>
  11. <DropdownToggle caret>
  12. Dropleft
  13. </DropdownToggle>
  14. <DropdownMenu>
  15. <DropdownItem>Another Action</DropdownItem>
  16. <DropdownItem>Another Action</DropdownItem>
  17. </DropdownMenu>
  18. </Dropdown>
  19. <Dropdown direction="right" isOpen={this.state.btnDropright} toggle={() => { this.setState({ btnDropright: !this.state.btnDropright }); }}>
  20. <DropdownToggle caret>
  21. Dropright
  22. </DropdownToggle>
  23. <DropdownMenu>
  24. <DropdownItem>Another Action</DropdownItem>
  25. <DropdownItem>Another Action</DropdownItem>
  26. </DropdownMenu>
  27. </Dropdown>

Modifiers

Dropdowns - 图7

  1. <Dropdown isOpen={isOpen} toggle={toggle}>
  2. <DropdownToggle>
  3. Dropdown
  4. </DropdownToggle>
  5. <DropdownMenu
  6. modifiers={{
  7. setMaxHeight: {
  8. enabled: true,
  9. order: 890,
  10. fn: (data) => {
  11. return {
  12. ...data,
  13. styles: {
  14. ...data.styles,
  15. overflow: 'auto',
  16. maxHeight: 100,
  17. },
  18. };
  19. },
  20. },
  21. }}
  22. >
  23. <DropdownItem>Another Action</DropdownItem>
  24. <DropdownItem>Another Action</DropdownItem>
  25. <DropdownItem>Another Action</DropdownItem>
  26. <DropdownItem>Another Action</DropdownItem>
  27. <DropdownItem>Another Action</DropdownItem>
  28. <DropdownItem>Another Action</DropdownItem>
  29. <DropdownItem>Another Action</DropdownItem>
  30. <DropdownItem>Another Action</DropdownItem>
  31. <DropdownItem>Another Action</DropdownItem>
  32. <DropdownItem>Another Action</DropdownItem>
  33. </DropdownMenu>
  34. </Dropdown>

setActiveFromChild

Use setActiveFromChild prop to automatically set the dropdown to active when any of the dropdown menu items are active. Note: this prop will not work in conjunction with react-router Link and activeClassName.

  1. import React from 'react';
  2. import { Navbar, Nav, NavItem, NavLink, UncontrolledDropdown, DropdownToggle, DropdownMenu, DropdownItem } from 'reactstrap';
  3. export default function Example () {
  4. return (
  5. <Navbar color="light" light expand="md">
  6. <Nav className="ml-auto" navbar>
  7. <NavItem>
  8. <NavLink href="/components/">Inactive Link</NavLink>
  9. </NavItem>
  10. <UncontrolledDropdown setActiveFromChild>
  11. <DropdownToggle tag="a" className="nav-link" caret>
  12. Dropdown
  13. </DropdownToggle>
  14. <DropdownMenu>
  15. <DropdownItem tag="a" href="/blah" active>Link</DropdownItem>
  16. </DropdownMenu>
  17. </UncontrolledDropdown>
  18. </Nav>
  19. </Navbar>
  20. );
  21. }