Skip to content

Commit

Permalink
Merge pull request #11 from GSG-G9/components
Browse files Browse the repository at this point in the history
add checkbox to input
  • Loading branch information
Israa91 authored Apr 28, 2021
2 parents fe3c4fb + fc1392f commit f4b064a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
10 changes: 8 additions & 2 deletions src/components/AddTodo/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
import React, { useState } from 'react';
import { useDispatch } from 'react-redux';
import { saveTodo } from '../../features/todoSlice';
import Checkbox from '../Checkbox';

const AddTodo = () => {
const [todoText, setTodoText] = useState('');
const [check, setCheck] = useState(false);
const dispatch = useDispatch();
const handleSubmit = (e) => {
e.preventDefault();
console.log(todoText);
dispatch(
saveTodo({
text: todoText,
done: false,
done: check,
id: Date.now(),
})
);
};
return (
<form className="input" onSubmit={handleSubmit}>
<input type="text" onChange={(e) => setTodoText(e.target.value)} />
<span>
<input type="checkbox" onChange={(e) => setCheck(e.target.checked)} />

<input type="text" onChange={(e) => setTodoText(e.target.value)} />
</span>
</form>
);
};
Expand Down
13 changes: 13 additions & 0 deletions src/components/Checkbox/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from 'react';
import { useDispatch } from 'react-redux';
import { setCheck } from '../../features/todoSlice';

const Checkbox = ({ checked, id }) => {
const dispatch = useDispatch();
const handleCheck = () => {
dispatch(setCheck(id));
};
return <input type="checkbox" checked={checked} onChange={handleCheck} />;
};

export default Checkbox;
Empty file.
12 changes: 9 additions & 3 deletions src/components/TodoItem/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React from 'react';
import { useDispatch } from 'react-redux';
import PropTypes from 'prop-types';
import { deleteTodo, setCheck } from '../../features/todoSlice';
import Checkbox from '../Checkbox';

const TodoItem = ({ text, done, id }) => {
const dispatch = useDispatch();
Expand All @@ -9,19 +11,23 @@ const TodoItem = ({ text, done, id }) => {
};

const handleClick = () => {
console.log(dispatch(deleteTodo(id)));
dispatch(deleteTodo(id));
};
return (
<li className="todoItem">
<input type="checkbox" checked={done} onChange={handleCheck} />

{/* <input type="checkbox" checked={done} onChange={handleCheck} /> */}
<Checkbox checked={done} id={id} />
<span className={done ? 'todoItem--done' : null}>{text}</span>
<button type="button" onClick={handleClick}>
delete
</button>
</li>
);
};
TodoItem.propTypes = {
text: PropTypes.string.isRequired,
done: PropTypes.bool.isRequired,
id: PropTypes.string.isRequired,
};

export default TodoItem;
1 change: 1 addition & 0 deletions src/components/TodoList/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import { useSelector, useDispatch } from 'react-redux';
import PropTypes from 'prop-types';
import TodoItem from '../TodoItem';

import { VisibilityFilter } from '../../features/filtersSlice';
Expand Down

0 comments on commit f4b064a

Please sign in to comment.