File tree Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change @@ -8,6 +8,8 @@ const KEY_CODE = {
8
8
DELETE : 46 ,
9
9
} ;
10
10
11
+ const CONTAINER_DATA_ID = 'REACT_VERIFICATION_CODE_CONTAINER' ;
12
+
11
13
export default ( { length = 4 , placeholder = '·' } ) => {
12
14
const [ activeIndex , setActiveIndex ] = React . useState < number > ( - 1 ) ;
13
15
const [ value , setValue ] = React . useState < string [ ] > (
@@ -87,8 +89,29 @@ export default ({ length = 4, placeholder = '·' }) => {
87
89
blurItem ( activeIndex ) ;
88
90
} ;
89
91
92
+ React . useEffect ( ( ) => {
93
+ const onDocumentClick = ( e : MouseEvent ) => {
94
+ const targetIncludesContainer = e
95
+ . composedPath ( )
96
+ . reduce (
97
+ ( bool : boolean , path : HTMLElement ) =>
98
+ bool || path . dataset ?. reactVerificationCodeId === CONTAINER_DATA_ID ,
99
+ false
100
+ ) ;
101
+
102
+ if ( ! targetIncludesContainer ) setActiveIndex ( - 1 ) ;
103
+ } ;
104
+
105
+ document . addEventListener ( 'click' , onDocumentClick ) ;
106
+
107
+ return ( ) => {
108
+ document . removeEventListener ( 'click' , onDocumentClick ) ;
109
+ } ;
110
+ } , [ ] ) ;
111
+
90
112
return (
91
113
< div
114
+ data-react-verification-code-id = { CONTAINER_DATA_ID }
92
115
className = 'ReactVerificationCode__container'
93
116
style = {
94
117
{
You can’t perform that action at this time.
0 commit comments