Manual Reference Source

basic/PopupMenu.js

  1. /**
  2. * Created by pposel on 22/02/2017.
  3. */
  4.  
  5. import PropTypes from 'prop-types';
  6.  
  7. import React, { Component } from 'react';
  8. import { Popup, Icon } from './index';
  9.  
  10. /**
  11. * PopupMenu is a component which uses [Popup](https://react.semantic-ui.com/modules/popup) component to create
  12. * dropdown menu triggered by [Icon](https://react.semantic-ui.com/elements/icon) button.
  13. *
  14. * ## Access
  15. * `Stage.Basic.PopupMenu`
  16. *
  17. * ## Usage
  18. * ![PopupMenu](manual/asset/PopupMenu.png)
  19. * ```
  20. * <PopupMenu>
  21. * <Menu pointing vertical>
  22. * <Menu.Item icon='users' content="Edit group's users" name='users' />
  23. * <Menu.Item icon='user' content="Edit group's tenants" name='tenants' />
  24. * <Menu.Item icon='trash' content='Delete' name='delete' />
  25. * </Menu>
  26. * </PopupMenu>
  27. * ```
  28. */
  29. export default class PopupMenu extends Component {
  30. constructor(props, context) {
  31. super(props, context);
  32.  
  33. this.state = {
  34. opened: false
  35. };
  36. }
  37.  
  38. /**
  39. * propTypes
  40. *
  41. * @property {object[]} children primary content
  42. * @property {string} [className=''] additional CSS classes to be applied to popup trigger
  43. * @property {string} [position='bottom right'] position for the popover.
  44. * @property {number} [offset=12] horizontal offset in pixels to be applied to popup
  45. * @property {string} [icon='content'] popup trigger icon name (see [Icon](https://react.semantic-ui.com/elements/icon))
  46. * @property {boolean} [disabled=false] specifies if trigger shall be disabled
  47. * @property {boolean} [bordered=false] specifies if icon shall be bordered
  48. * @property {string} [help=''] additional popup help message shown on trigger hover
  49. */
  50. static propTypes = {
  51. className: PropTypes.string,
  52. children: PropTypes.any.isRequired,
  53. position: PropTypes.string,
  54. offset: PropTypes.number,
  55. icon: PropTypes.string,
  56. disabled: PropTypes.bool,
  57. bordered: PropTypes.bool,
  58. help: PropTypes.string
  59. };
  60.  
  61. static defaultProps = {
  62. className: '',
  63. position: 'bottom right',
  64. offset: 12,
  65. icon: 'content',
  66. disabled: false,
  67. bordered: false,
  68. help: ''
  69. };
  70.  
  71. render() {
  72. const trigger = _.isEmpty(this.props.help) ? (
  73. <Icon
  74. link={!this.props.disabled}
  75. disabled={this.props.disabled}
  76. name={this.props.icon}
  77. bordered={this.props.bordered}
  78. className={this.props.className}
  79. onClick={e => {
  80. e.stopPropagation();
  81. }}
  82. />
  83. ) : (
  84. <span
  85. onClick={e => {
  86. e.stopPropagation();
  87. }}
  88. >
  89. <Popup
  90. trigger={
  91. <Icon
  92. name={this.props.icon}
  93. bordered={this.props.bordered}
  94. link={!this.props.disabled}
  95. className={this.props.className}
  96. />
  97. }
  98. content={this.props.help}
  99. />
  100. </span>
  101. );
  102.  
  103. return (
  104. <Popup
  105. trigger={trigger}
  106. on="click"
  107. position={this.props.position}
  108. className="popupMenu"
  109. offset={this.props.offset}
  110. open={this.props.disabled ? false : this.state.opened}
  111. onClose={() => this.setState({ opened: false })}
  112. onOpen={() => this.setState({ opened: true })}
  113. onClick={e => {
  114. e.stopPropagation();
  115. this.setState({ opened: false });
  116. }}
  117. >
  118. {this.props.children}
  119. </Popup>
  120. );
  121. }
  122. }