Skip to content

Commit f8dac7f

Browse files
committed
chore: add changes from PR 668
1 parent c888b08 commit f8dac7f

File tree

1 file changed

+34
-30
lines changed

1 file changed

+34
-30
lines changed

packages/core/src/lib/Todo/Todo.tsx

+34-30
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,21 @@ const Todo: React.FC<TodoProps> = ({
2929
}) => {
3030
const key = useMemo(() => Math.random(), []);
3131
const [inputValue, setInputValue] = useState<string>('');
32+
const [isInputVlaid, setIsInputValid] = useState<boolean>(false);
3233
const [lists, setLists] = useState<string[]>(todos);
33-
3434
useEffect(() => onChange && onChange(lists), [lists]);
3535
useEffect(() => setLists(todos), [todos]);
36+
useEffect(() => {
37+
const expression = new RegExp(pattern || '(.*?)');
38+
setIsInputValid(expression.test(inputValue));
39+
}, [inputValue]);
3640

3741
const removeTodo = (id: number) => {
3842
const updatedList = lists.filter((item) => item !== lists[id]);
3943
setLists([...updatedList]);
4044
};
4145

42-
const addTodo = (e: any) => {
43-
e.preventDefault();
46+
const addTodo = () => {
4447
setLists((prevTodo) => Array.from(new Set([...prevTodo, inputValue])));
4548
setInputValue('');
4649
const input: HTMLInputElement | null = document.querySelector(
@@ -52,40 +55,41 @@ const Todo: React.FC<TodoProps> = ({
5255

5356
return (
5457
<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>
8181

8282
<DivStyledContent
8383
data-testid="test-todo_content"
8484
fullWidth={fullWidth}
8585
>
8686
{lists.map((todo, i) => (
8787
<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+
}
8993
hasCancel
9094
key={i}
9195
onCancelClick={() => removeTodo(i)}

0 commit comments

Comments
 (0)