1- import { useEffect , useRef } from 'react' ;
1+ import { useEffect , useRef , useState } from 'react' ;
22
3+ import { postBadWords } from '@/apis/admin' ;
34import { AddIcon } from '@/assets/icons' ;
45
56export default function AddInputButton ( {
67 setAddInputShow,
8+ setBadWords,
79} : {
810 setAddInputShow : React . Dispatch < React . SetStateAction < boolean > > ;
11+ setBadWords : React . Dispatch < React . SetStateAction < BadWords [ ] > > ;
912} ) {
13+ const [ inputText , setInputText ] = useState < BadWords > ( { word : '' } ) ;
1014 const inputRef = useRef < HTMLInputElement > ( null ) ;
11- const handleInputWidth = ( event : Event ) => {
15+
16+ const handleInputWidth = ( event : React . FormEvent < HTMLInputElement > ) => {
1217 const target = event . target as HTMLInputElement ;
1318 target . style . width = '50px' ;
1419 target . style . width = `${ target . scrollWidth } px` ;
1520 } ;
1621
22+ const handlePostBadWords = ( ) => {
23+ if ( inputText . word === '' ) return setAddInputShow ( false ) ;
24+ postBadWords ( inputText , ( ) => {
25+ setBadWords ( ( cur ) => [ ...cur , inputText ] ) ;
26+ setAddInputShow ( false ) ;
27+ } ) ;
28+ } ;
29+
1730 useEffect ( ( ) => {
1831 const inputElement = inputRef . current ;
1932 if ( inputElement ) {
20- inputElement . addEventListener ( 'input' , handleInputWidth ) ;
2133 inputElement . focus ( ) ;
2234 }
23-
24- return ( ) => {
25- if ( inputElement ) {
26- inputElement . removeEventListener ( 'input' , handleInputWidth ) ;
27- }
28- } ;
2935 } , [ ] ) ;
3036
3137 return (
@@ -34,15 +40,21 @@ export default function AddInputButton({
3440 ref = { inputRef }
3541 type = "text"
3642 className = "w-10 min-w-10"
43+ onInput = { ( e ) => {
44+ handleInputWidth ( e ) ;
45+ } }
46+ onChange = { ( e ) => {
47+ setInputText ( ( ) => ( { word : e . target . value } ) ) ;
48+ } }
3749 onKeyDown = { ( e ) => {
3850 if ( e . key === 'Enter' ) {
39- setAddInputShow ( false ) ;
51+ handlePostBadWords ( ) ;
4052 }
4153 } }
4254 />
4355 < button
4456 onClick = { ( ) => {
45- setAddInputShow ( false ) ;
57+ handlePostBadWords ( ) ;
4658 } }
4759 >
4860 < AddIcon className = "h-4 w-4" />
0 commit comments