1
- import React from ' react'
2
- import PropTypes from ' prop-types'
1
+ import React from " react" ;
2
+ import PropTypes from " prop-types" ;
3
3
4
4
const noOutlineStyles = {
5
- outline : ' none'
6
- }
5
+ outline : " none"
6
+ } ;
7
7
8
8
class FocusWithin extends React . Component {
9
9
state = {
10
10
focused : false
11
- }
11
+ } ;
12
12
13
- ref = React . createRef ( )
13
+ ref = React . createRef ( ) ;
14
14
15
- /** @public
16
- * @function focus - calls `focus` method on the container node
17
- * @return void
15
+ /**
16
+ * Calls `focus` method on the container node
17
+ *
18
+ * @public
19
+ * @method focus
18
20
* */
19
21
focus ( ) {
20
- const node = this . ref . current
21
- if ( node != null && typeof node . focus === ' function' ) {
22
- node . focus ( )
22
+ const node = this . ref . current ;
23
+ if ( node != null && typeof node . focus === " function" ) {
24
+ node . focus ( ) ;
23
25
}
24
26
}
25
27
26
28
onFocus = evt => {
27
- const { onFocus } = this . props
28
- const { focused } = this . state
29
+ const { onFocus } = this . props ;
30
+ const { focused } = this . state ;
29
31
30
32
// TODO: Figure out if this check is "safe" or we should rely on SCU instead
31
33
if ( ! focused ) {
@@ -34,61 +36,61 @@ class FocusWithin extends React.Component {
34
36
focused : true
35
37
} ,
36
38
( ) => {
37
- onFocus ( evt )
39
+ onFocus ( evt ) ;
38
40
}
39
- )
41
+ ) ;
40
42
}
41
- }
43
+ } ;
42
44
43
45
onBlur = evt => {
44
- const { onBlur } = this . props
45
- const { focused } = this . state
46
+ const { onBlur } = this . props ;
47
+ const { focused } = this . state ;
46
48
47
49
// Do not blur if focus within the container or we're editing
48
50
if ( this . isFocusWithin ( this . ref . current ) ) {
49
- evt . preventDefault ( )
50
- evt . stopPropagation ( )
51
- return
51
+ evt . preventDefault ( ) ;
52
+ evt . stopPropagation ( ) ;
53
+ return ;
52
54
}
53
55
54
56
// Persist event object
55
- evt . persist ( )
57
+ evt . persist ( ) ;
56
58
57
59
if ( focused ) {
58
60
this . setState (
59
61
{
60
62
focused : false
61
63
} ,
62
64
( ) => {
63
- onBlur ( evt )
65
+ onBlur ( evt ) ;
64
66
}
65
- )
67
+ ) ;
66
68
}
67
- }
69
+ } ;
68
70
69
71
isFocusWithin = node => {
70
72
// We need to check `:focus-within` on the the parent element in order to work
71
- if ( process . env . NODE_ENV === ' development' ) {
73
+ if ( process . env . NODE_ENV === " development" ) {
72
74
if (
73
75
node == null ||
74
76
node . parentNode == null ||
75
- typeof node . parentNode . querySelector !== ' function'
77
+ typeof node . parentNode . querySelector !== " function"
76
78
) {
77
79
throw new Error (
78
- ' A ref to a DOM Node with a valid parent Node must be supplied to' +
79
- ' FocusWithin.\n' +
80
- ' You have probably provided a ref to a React Element.\n See https://reactjs.org/docs/react-api.html#refs'
81
- )
80
+ " A ref to a DOM Node with a valid parent Node must be supplied to" +
81
+ " FocusWithin.\n" +
82
+ " You have probably provided a ref to a React Element.\n See https://reactjs.org/docs/react-api.html#refs"
83
+ ) ;
82
84
}
83
85
}
84
- return ! ! node . parentNode . querySelector ( ' :focus-within' )
85
- }
86
+ return ! ! node . parentNode . querySelector ( " :focus-within" ) ;
87
+ } ;
86
88
87
89
render ( ) {
88
- const { children } = this . props
89
- const { focused } = this . state
90
+ const { children } = this . props ;
91
+ const { focused } = this . state ;
90
92
91
- if ( typeof children === ' function' ) {
93
+ if ( typeof children === " function" ) {
92
94
return React . cloneElement (
93
95
children ( {
94
96
focused,
@@ -98,14 +100,19 @@ class FocusWithin extends React.Component {
98
100
onFocus : this . onFocus ,
99
101
onBlur : this . onBlur
100
102
}
101
- )
103
+ ) ;
102
104
}
103
105
104
106
return (
105
- < div ref = { this . ref } onFocus = { this . onFocus } onBlur = { this . onBlur } style = { noOutlineStyles } >
107
+ < div
108
+ ref = { this . ref }
109
+ onFocus = { this . onFocus }
110
+ onBlur = { this . onBlur }
111
+ style = { noOutlineStyles }
112
+ >
106
113
{ children }
107
114
</ div >
108
- )
115
+ ) ;
109
116
}
110
117
}
111
118
@@ -117,11 +124,11 @@ FocusWithin.propTypes = {
117
124
children : PropTypes . oneOfType ( [ PropTypes . node , PropTypes . func ] ) . isRequired ,
118
125
onBlur : PropTypes . func ,
119
126
onFocus : PropTypes . func
120
- }
127
+ } ;
121
128
122
129
FocusWithin . defaultProps = {
123
130
onBlur : ( ) => { } ,
124
131
onFocus : ( ) => { }
125
- }
132
+ } ;
126
133
127
- export default FocusWithin
134
+ export default FocusWithin ;
0 commit comments