@@ -11,13 +11,12 @@ import Popover from '../popover';
11
11
class Dropdown extends Component {
12
12
constructor ( ) {
13
13
super ( ...arguments ) ;
14
-
15
14
this . toggle = this . toggle . bind ( this ) ;
16
15
this . close = this . close . bind ( this ) ;
16
+ this . clickOutside = this . clickOutside . bind ( this ) ;
17
+ this . bindContainer = this . bindContainer . bind ( this ) ;
17
18
this . refresh = this . refresh . bind ( this ) ;
18
-
19
19
this . popoverRef = createRef ( ) ;
20
-
21
20
this . state = {
22
21
isOpen : false ,
23
22
} ;
@@ -39,6 +38,10 @@ class Dropdown extends Component {
39
38
}
40
39
}
41
40
41
+ bindContainer ( ref ) {
42
+ this . container = ref ;
43
+ }
44
+
42
45
/**
43
46
* When contents change height due to user interaction,
44
47
* `refresh` can be called to re-render Popover with correct
@@ -56,6 +59,12 @@ class Dropdown extends Component {
56
59
} ) ) ;
57
60
}
58
61
62
+ clickOutside ( event ) {
63
+ if ( ! this . container . contains ( event . target ) ) {
64
+ this . close ( ) ;
65
+ }
66
+ }
67
+
59
68
close ( ) {
60
69
this . setState ( { isOpen : false } ) ;
61
70
}
@@ -75,14 +84,15 @@ class Dropdown extends Component {
75
84
const args = { isOpen, onToggle : this . toggle , onClose : this . close } ;
76
85
77
86
return (
78
- < div className = { className } >
87
+ < div className = { className } ref = { this . bindContainer } >
79
88
{ renderToggle ( args ) }
80
89
{ isOpen && (
81
90
< Popover
82
91
className = { contentClassName }
83
92
ref = { this . popoverRef }
84
93
position = { position }
85
94
onClose = { this . close }
95
+ onClickOutside = { this . clickOutside }
86
96
expandOnMobile = { expandOnMobile }
87
97
headerTitle = { headerTitle }
88
98
>
0 commit comments