@@ -5,6 +5,7 @@ import palette from '../../lib/styles/palette';
5
5
import transitions from '../../lib/styles/transitions' ;
6
6
import { mediaQuery } from '../../lib/styles/media' ;
7
7
import { useTransition , animated } from 'react-spring' ;
8
+ import OutsideClickHandler from 'react-outside-click-handler' ;
8
9
9
10
export interface TagInputProps {
10
11
ref ?: React . RefObject < HTMLDivElement > ;
@@ -24,7 +25,6 @@ const TagInput: React.FC<TagInputProps> = ({ onChange, tags: initialTags }) => {
24
25
const [ value , setValue ] = useState ( '' ) ;
25
26
const [ focus , setFocus ] = useState ( false ) ;
26
27
const ignore = useRef ( false ) ;
27
- const editableDiv = useRef < HTMLDivElement > ( null ) ;
28
28
29
29
useEffect ( ( ) => {
30
30
if ( tags . length === 0 ) return ;
@@ -39,13 +39,10 @@ const TagInput: React.FC<TagInputProps> = ({ onChange, tags: initialTags }) => {
39
39
setValue ( e . target . value ) ;
40
40
} ;
41
41
42
- useEffect ( ( ) => {
43
- if ( editableDiv . current ) {
44
- if ( value === '' ) {
45
- editableDiv . current . innerText = value ;
46
- }
47
- }
48
- } , [ value ] ) ;
42
+ const onOutsideClick = ( ) => {
43
+ if ( value === '' ) return ;
44
+ insertTag ( value ) ;
45
+ } ;
49
46
50
47
const insertTag = useCallback (
51
48
( tag : string ) => {
@@ -96,23 +93,25 @@ const TagInput: React.FC<TagInputProps> = ({ onChange, tags: initialTags }) => {
96
93
} ;
97
94
98
95
return (
99
- < TagInputBlock >
100
- { tags . map ( ( tag ) => (
101
- < TagItem key = { tag } onClick = { ( ) => onRemove ( tag ) } >
102
- { tag }
103
- </ TagItem >
104
- ) ) }
105
- < StyledInput
106
- placeholder = "태그를 입력하세요"
107
- tabIndex = { 2 }
108
- onKeyDown = { onKeyDown }
109
- onChange = { onChangeInput }
110
- value = { value }
111
- onFocus = { ( ) => setFocus ( true ) }
112
- onBlur = { ( ) => setFocus ( false ) }
113
- />
114
- < Help focus = { focus } />
115
- </ TagInputBlock >
96
+ < OutsideClickHandler onOutsideClick = { onOutsideClick } >
97
+ < TagInputBlock >
98
+ { tags . map ( ( tag ) => (
99
+ < TagItem key = { tag } onClick = { ( ) => onRemove ( tag ) } >
100
+ { tag }
101
+ </ TagItem >
102
+ ) ) }
103
+ < StyledInput
104
+ placeholder = "태그를 입력하세요"
105
+ tabIndex = { 2 }
106
+ onKeyDown = { onKeyDown }
107
+ onChange = { onChangeInput }
108
+ value = { value }
109
+ onFocus = { ( ) => setFocus ( true ) }
110
+ onBlur = { ( ) => setFocus ( false ) }
111
+ />
112
+ < Help focus = { focus } />
113
+ </ TagInputBlock >
114
+ </ OutsideClickHandler >
116
115
) ;
117
116
} ;
118
117
0 commit comments