@@ -5,6 +5,7 @@ import palette from '../../lib/styles/palette';
55import transitions from '../../lib/styles/transitions' ;
66import { mediaQuery } from '../../lib/styles/media' ;
77import { useTransition , animated } from 'react-spring' ;
8+ import OutsideClickHandler from 'react-outside-click-handler' ;
89
910export interface TagInputProps {
1011 ref ?: React . RefObject < HTMLDivElement > ;
@@ -24,7 +25,6 @@ const TagInput: React.FC<TagInputProps> = ({ onChange, tags: initialTags }) => {
2425 const [ value , setValue ] = useState ( '' ) ;
2526 const [ focus , setFocus ] = useState ( false ) ;
2627 const ignore = useRef ( false ) ;
27- const editableDiv = useRef < HTMLDivElement > ( null ) ;
2828
2929 useEffect ( ( ) => {
3030 if ( tags . length === 0 ) return ;
@@ -39,13 +39,10 @@ const TagInput: React.FC<TagInputProps> = ({ onChange, tags: initialTags }) => {
3939 setValue ( e . target . value ) ;
4040 } ;
4141
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+ } ;
4946
5047 const insertTag = useCallback (
5148 ( tag : string ) => {
@@ -96,23 +93,25 @@ const TagInput: React.FC<TagInputProps> = ({ onChange, tags: initialTags }) => {
9693 } ;
9794
9895 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 >
116115 ) ;
117116} ;
118117
0 commit comments