@@ -29,18 +29,21 @@ const Todo: React.FC<TodoProps> = ({
29
29
} ) => {
30
30
const key = useMemo ( ( ) => Math . random ( ) , [ ] ) ;
31
31
const [ inputValue , setInputValue ] = useState < string > ( '' ) ;
32
+ const [ isInputVlaid , setIsInputValid ] = useState < boolean > ( false ) ;
32
33
const [ lists , setLists ] = useState < string [ ] > ( todos ) ;
33
-
34
34
useEffect ( ( ) => onChange && onChange ( lists ) , [ lists ] ) ;
35
35
useEffect ( ( ) => setLists ( todos ) , [ todos ] ) ;
36
+ useEffect ( ( ) => {
37
+ const expression = new RegExp ( pattern || '(.*?)' ) ;
38
+ setIsInputValid ( expression . test ( inputValue ) ) ;
39
+ } , [ inputValue ] ) ;
36
40
37
41
const removeTodo = ( id : number ) => {
38
42
const updatedList = lists . filter ( ( item ) => item !== lists [ id ] ) ;
39
43
setLists ( [ ...updatedList ] ) ;
40
44
} ;
41
45
42
- const addTodo = ( e : any ) => {
43
- e . preventDefault ( ) ;
46
+ const addTodo = ( ) => {
44
47
setLists ( ( prevTodo ) => Array . from ( new Set ( [ ...prevTodo , inputValue ] ) ) ) ;
45
48
setInputValue ( '' ) ;
46
49
const input : HTMLInputElement | null = document . querySelector (
@@ -52,40 +55,41 @@ const Todo: React.FC<TodoProps> = ({
52
55
53
56
return (
54
57
< SectionStyled data-testid = "test-todo" { ...props } >
55
- < form onSubmit = { addTodo } >
56
- < DivStyled >
57
- < Input
58
- id = "todo-input"
59
- label = { label }
60
- onChange = { ( e ) => setInputValue ( e . target . value ) }
61
- size = "large"
62
- validation = { {
63
- regExp : pattern ,
64
- required : true ,
65
- } }
66
- key = { key }
67
- value = { inputValue }
68
- />
69
- < Button
70
- type = "submit"
71
- disabled = { ! inputValue }
72
- icon = {
73
- < Plus title = "plus icon" titleId = "todo plus icon" />
74
- }
75
- size = "large"
76
- text = { buttonText }
77
- theme = "primary"
78
- />
79
- </ DivStyled >
80
- </ form >
58
+ < DivStyled >
59
+ < Input
60
+ id = "todo-input"
61
+ label = { label }
62
+ onChange = { ( e ) => setInputValue ( e . target . value ) }
63
+ size = "large"
64
+ validation = { {
65
+ regExp : pattern ,
66
+ required : true ,
67
+ } }
68
+ key = { key }
69
+ value = { inputValue }
70
+ />
71
+
72
+ < Button
73
+ disabled = { ! isInputVlaid }
74
+ icon = { < Plus title = "plus icon" titleId = "todo plus icon" /> }
75
+ onClick = { addTodo }
76
+ size = "large"
77
+ text = { buttonText }
78
+ theme = "primary"
79
+ />
80
+ </ DivStyled >
81
81
82
82
< DivStyledContent
83
83
data-testid = "test-todo_content"
84
84
fullWidth = { fullWidth }
85
85
>
86
86
{ lists . map ( ( todo , i ) => (
87
87
< Tag
88
- color = { i < 8 ? colors [ i ] : colors [ i % 8 ] }
88
+ color = {
89
+ i < colors . length
90
+ ? colors [ i ]
91
+ : colors [ i % colors . length ]
92
+ }
89
93
hasCancel
90
94
key = { i }
91
95
onCancelClick = { ( ) => removeTodo ( i ) }
0 commit comments