File tree Expand file tree Collapse file tree 2 files changed +47
-1
lines changed Expand file tree Collapse file tree 2 files changed +47
-1
lines changed Original file line number Diff line number Diff line change 1
1
{
2
2
"name" : " libreact" ,
3
- "version" : " 0.17 .0" ,
3
+ "version" : " 1.0 .0" ,
4
4
"description" : " React standard library" ,
5
5
"main" : " lib/index.js" ,
6
6
"repository" : {
Original file line number Diff line number Diff line change
1
+ import { Component , cloneElement , Children } from 'react' ;
2
+ import { on , off , noop } from '../util' ;
3
+
4
+ export interface IOutsideClickProps {
5
+ onClick ?: ( event ?) => void ;
6
+ }
7
+
8
+ export interface IOutsideClickState {
9
+ }
10
+
11
+ class OutsideClick extends Component < IOutsideClickProps , IOutsideClickState > {
12
+ el : HTMLElement ;
13
+
14
+ ref = ( originalRef ) => ( el ) => {
15
+ this . el = el ;
16
+ ( originalRef || noop ) ( el ) ;
17
+ } ;
18
+
19
+ componentDidMount ( ) {
20
+ on ( document , 'mousedown' , this . onClickOutside ) ;
21
+ }
22
+
23
+ componentWillUnmount ( ) {
24
+ off ( document , 'mousedown' , this . onClickOutside ) ;
25
+ }
26
+
27
+ onClickOutside = ( event ) => {
28
+ if ( this . el && ! this . el . contains ( event . target ) ) {
29
+ ( this . props . onClick || noop ) ( event ) ;
30
+ }
31
+ } ;
32
+
33
+ render ( ) {
34
+ const { children} = this . props ;
35
+ const element = Children . only ( children ) ;
36
+
37
+ if ( ! element ) {
38
+ return null ;
39
+ }
40
+
41
+ return cloneElement ( element , {
42
+ ...element . props ,
43
+ ref : this . ref ( ( element as any ) . ref )
44
+ } ) ;
45
+ }
46
+ }
You can’t perform that action at this time.
0 commit comments