Guest User

Untitled

a guest
Feb 16th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import './uk-product-context-menu.scss';
  3. import Icon from '../Icon';
  4.  
  5. const ContextMenuButton = (props) => (
  6. <div className="uk-product-context-menu__toggle-trigger" role="button" {...props}>
  7. <Icon type="expand"/>
  8. </div>
  9. );
  10.  
  11. const ContextMenuOption = ({ onActionClick, icon, label }) => (
  12. <li
  13. onClick={(event) => {
  14. event.preventDefault();
  15. onActionClick();
  16. }}
  17. className="uk-product-context-menu__action">
  18. <Icon type={icon}/>
  19. <span>{label}</span>
  20. </li>
  21. );
  22.  
  23. class ContextMenu extends Component {
  24.  
  25. constructor(props) {
  26. super(props);
  27.  
  28. this.state = {
  29. showActions: false,
  30. }
  31.  
  32. this.show = this.show.bind(this);
  33. this.hide = this.hide.bind(this);
  34. }
  35.  
  36. show(event) {
  37.  
  38. if (event) {
  39. event.preventDefault();
  40. }
  41.  
  42. this.setState({
  43. showActions: true
  44. }, () => {
  45. document.addEventListener('click', this.hide);
  46. });
  47.  
  48. }
  49.  
  50. hide(event) {
  51.  
  52. if (event) {
  53. event.preventDefault();
  54. }
  55.  
  56. this.setState({
  57. showActions: false
  58. }, () => {
  59. document.removeEventListener('click', this.hide);
  60. });
  61.  
  62. }
  63.  
  64. render() {
  65.  
  66. const {
  67. actions,
  68. } = this.props;
  69.  
  70. const {
  71. showActions,
  72. } = this.state;
  73.  
  74. return (
  75. <div className="uk-product-context-menu">
  76. <ContextMenuButton onClick={showActions ? this.hide : this.show} />
  77.  
  78. {
  79. showActions && (
  80.  
  81. <div className="uk-product-context-menu__actions" >
  82. {
  83. actions.map((action, i) => (
  84. <ContextMenuOption
  85. key={i}
  86. {...action} />
  87. ))
  88. }
  89. </div>
  90.  
  91. )
  92. }
  93. </div>
  94. );
  95.  
  96. }
  97. }
  98.  
  99. export default ContextMenu;
Add Comment
Please, Sign In to add comment