|
| 1 | +import * as React from 'react'; |
| 2 | +import { Manager, Target, Popper, Arrow } from 'react-popper'; |
| 3 | + |
| 4 | +import { ErrorInfoIconSvg } from './icons/errorInfoIcon.svg'; |
| 5 | + |
| 6 | +export interface ErrorIconWithPopoverProps { |
| 7 | + errorMessage: string | undefined; |
| 8 | +} |
| 9 | + |
| 10 | +export interface ErrorIconWithPopoverState { |
| 11 | + popoverIsOpen: boolean; |
| 12 | +} |
| 13 | + |
| 14 | +export class ErrorIconWithPopover extends React.Component<ErrorIconWithPopoverProps, ErrorIconWithPopoverState> { |
| 15 | + private wrapperRef: Node; |
| 16 | + |
| 17 | + constructor(props: ErrorIconWithPopoverProps) { |
| 18 | + super(props); |
| 19 | + |
| 20 | + this.state = { |
| 21 | + popoverIsOpen: false |
| 22 | + }; |
| 23 | + |
| 24 | + // Used to handle if user clicks outside error icon, the popover should close |
| 25 | + this.setWrapperRef = this.setWrapperRef.bind(this); |
| 26 | + this.handleClickOutside = this.handleClickOutside.bind(this); |
| 27 | + |
| 28 | + // Used to handle if the user scrolls anywhere on the page, the popover should close |
| 29 | + this.scrollHandler = this.scrollHandler.bind(this); |
| 30 | + |
| 31 | + // Used to handle if the user hits space on the error icon, the popover should toggle visibility |
| 32 | + this.keyUpHandler = this.keyUpHandler.bind(this); |
| 33 | + |
| 34 | + // Used to handle if the user clicks the error icon, the popover should toggle visibility |
| 35 | + this.clickHandler = this.clickHandler.bind(this); |
| 36 | + } |
| 37 | + |
| 38 | + componentDidMount() { |
| 39 | + document.addEventListener('mousedown', this.handleClickOutside); |
| 40 | + window.addEventListener('scroll', this.scrollHandler, true); |
| 41 | + } |
| 42 | + |
| 43 | + componentWillUnmount() { |
| 44 | + document.removeEventListener('mousedown', this.handleClickOutside); |
| 45 | + window.removeEventListener('scroll', this.scrollHandler, true); |
| 46 | + } |
| 47 | + |
| 48 | + componentWillReceiveProps() { |
| 49 | + this.setState({ |
| 50 | + popoverIsOpen: false |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + private handleClickOutside(event) { |
| 55 | + if (this.wrapperRef && !this.wrapperRef.contains(event.target) && this.state.popoverIsOpen) { |
| 56 | + this.setState({ |
| 57 | + popoverIsOpen: false |
| 58 | + }); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private setWrapperRef(node) { |
| 63 | + this.wrapperRef = node as Node; |
| 64 | + } |
| 65 | + |
| 66 | + private scrollHandler() { |
| 67 | + this.setState({ |
| 68 | + popoverIsOpen: false |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + private keyUpHandler(event) { |
| 73 | + // Space |
| 74 | + if (event.keyCode === 32) { |
| 75 | + this.clickHandler(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private clickHandler() { |
| 80 | + this.setState({ |
| 81 | + popoverIsOpen: !this.state.popoverIsOpen |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + render() { |
| 86 | + return ( |
| 87 | + <div ref={this.setWrapperRef} className='error-info-icon'> |
| 88 | + <Manager > |
| 89 | + <Target> |
| 90 | + <div |
| 91 | + tabIndex={0} |
| 92 | + style={this.props.errorMessage ? {} : { visibility: 'hidden' }} |
| 93 | + onClick={this.clickHandler} |
| 94 | + onKeyUp={this.keyUpHandler}> |
| 95 | + <ErrorInfoIconSvg/> |
| 96 | + </div> |
| 97 | + </Target> |
| 98 | + <Popper |
| 99 | + placement={'bottom'} |
| 100 | + modifiers={{ |
| 101 | + hide: { |
| 102 | + enabled: false |
| 103 | + }, |
| 104 | + preventOverflow: { |
| 105 | + enabled: true, |
| 106 | + }, |
| 107 | + arrow: { |
| 108 | + enabled: true |
| 109 | + } |
| 110 | + }} |
| 111 | + className='error-info-popover popper' |
| 112 | + eventsEnabled={true}> |
| 113 | + <div |
| 114 | + className='error-info-popover-content' |
| 115 | + style={this.shouldShowPopover() ? {} : { visibility: 'hidden' }}> |
| 116 | + {this.props.errorMessage} |
| 117 | + </div> |
| 118 | + <Arrow className='popper__arrow' /> |
| 119 | + </Popper> |
| 120 | + </Manager> |
| 121 | + </div> |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + private shouldShowPopover() { |
| 126 | + return this.state.popoverIsOpen; |
| 127 | + } |
| 128 | +} |
0 commit comments