11import * as React from 'react' ;
22
3- import { ToolbarDropDownSelectionPlacement } from './ToolbarDropDownSelectionPlacement' ;
43import { ToolbarDropDownSelectionItem } from './ToolbarDropDownSelectionItem' ;
54
65import { DropDown } from '../dropdown/DropDown' ;
@@ -24,6 +23,7 @@ export class ToolbarDropDown extends React.Component<Props, State> {
2423 } ;
2524
2625 private selectedLabelNode : HTMLElement ;
26+ private dropDownNode : HTMLElement ;
2727
2828 render ( ) {
2929 const { dropDown} = this . props ;
@@ -37,6 +37,14 @@ export class ToolbarDropDown extends React.Component<Props, State> {
3737 ) ;
3838 }
3939
40+ componentDidMount ( ) {
41+ document . addEventListener ( 'mousedown' , this . onGlobalClick ) ;
42+ }
43+
44+ componentWillUnmount ( ) {
45+ document . addEventListener ( 'mouseup' , this . onGlobalClick ) ;
46+ }
47+
4048 private saveSelectedLabelNode = ( node : HTMLDivElement ) => {
4149 this . selectedLabelNode = node ;
4250 }
@@ -52,15 +60,32 @@ export class ToolbarDropDown extends React.Component<Props, State> {
5260 const { dropDown} = this . props ;
5361
5462 return (
55- < ToolbarDropDownSelectionPlacement parent = { this . selectedLabelNode } >
63+ < div
64+ className = "rcv-toolbar-drop-down-selection-placement"
65+ style = { this . dropDownSelectionStyle ( ) }
66+ ref = { this . saveDropDownNode }
67+ >
5668 < div className = "rcv-toolbar-drop-down-selection" >
5769 { dropDown . items . map ( item =>
5870 < ToolbarDropDownSelectionItem key = { item . label } item = { item } onItemSelect = { this . onItemSelect } /> ) }
5971 </ div >
60- </ ToolbarDropDownSelectionPlacement >
72+ </ div >
6173 ) ;
6274 }
6375
76+ private saveDropDownNode = ( node : HTMLDivElement ) => {
77+ this . dropDownNode = node ;
78+ }
79+
80+ private dropDownSelectionStyle = ( ) => {
81+ const pos = this . selectedLabelNode . getBoundingClientRect ( ) ;
82+
83+ return {
84+ left : pos . left ,
85+ top : pos . top
86+ } ;
87+ }
88+
6489 private onItemSelect = ( itemLabel : string ) => {
6590 const { dropDown, onItemSelect} = this . props ;
6691
@@ -100,4 +125,17 @@ export class ToolbarDropDown extends React.Component<Props, State> {
100125 private toggleSelection = ( ) => {
101126 this . setState ( prev => ( { selectionVisible : ! prev . selectionVisible } ) ) ;
102127 }
128+
129+ private hideSelection = ( ) => {
130+ this . setState ( { selectionVisible : false } ) ;
131+ }
132+
133+ private onGlobalClick = ( e : any ) => {
134+ if ( this . dropDownNode &&
135+ this . selectedLabelNode &&
136+ ! this . dropDownNode . contains ( e . target ) &&
137+ ! this . selectedLabelNode . contains ( e . target ) ) {
138+ this . hideSelection ( ) ;
139+ }
140+ }
103141}
0 commit comments