diff --git a/src/components/add-item/add-item.css b/src/components/add-item/add-item.css new file mode 100644 index 0000000..1162240 --- /dev/null +++ b/src/components/add-item/add-item.css @@ -0,0 +1,7 @@ +.add-item { + margin-top: 10px; +} + +.add-item input { + margin-right: 3px; +} \ No newline at end of file diff --git a/src/components/add-item/add-item.js b/src/components/add-item/add-item.js new file mode 100644 index 0000000..e222160 --- /dev/null +++ b/src/components/add-item/add-item.js @@ -0,0 +1,61 @@ +import React from 'react'; + +import './add-item.css' + +export default class AddItem extends React.Component { + + state = { + label: '' + }; + + onLabelChange = (e) => { + this.setState( { + label: e.target.value + }); + }; + + onClick = (e) => { + //e.preventDefault(); //Чтобы страница не перезагружалась + this.props.onItemAdded(this.state.label); + this.setState({ + label: '' + }); + }; + + + render() { + return ( +
+ + +
+ ); + }; + + /*render() { + return ( +
+ + + +
+ ); + };*/ +}; \ No newline at end of file diff --git a/src/components/app-header.css b/src/components/app-header/app-header.css similarity index 100% rename from src/components/app-header.css rename to src/components/app-header/app-header.css diff --git a/src/components/app-header.js b/src/components/app-header/app-header.js similarity index 100% rename from src/components/app-header.js rename to src/components/app-header/app-header.js diff --git a/src/components/app.js b/src/components/app.js deleted file mode 100644 index 0e9593c..0000000 --- a/src/components/app.js +++ /dev/null @@ -1,24 +0,0 @@ -import React from 'react'; - -import AppHeader from './app-header'; -import SearchPanel from './search-panel'; -import TodoList from './todo-list'; - -const App = () => { - - const todoData = [ - {label: "Drink Coffe", important: false, id:1}, - {label: "Study React", important: true, id:2}, - {label: "Study Css", important: false, id:3}, - ]; - - return ( -
- - - -
- ); - }; - -export default App; \ No newline at end of file diff --git a/src/components/app.css b/src/components/app/app.css similarity index 100% rename from src/components/app.css rename to src/components/app/app.css diff --git a/src/components/app/app.js b/src/components/app/app.js new file mode 100644 index 0000000..a943c46 --- /dev/null +++ b/src/components/app/app.js @@ -0,0 +1,67 @@ +import React from 'react'; + +import AppHeader from '../app-header/app-header'; +import SearchPanel from '../search-panel/search-panel'; +import TodoList from '../todo-list/todo-list'; +import AddItem from '../add-item/add-item'; + +export default class App extends React.Component { + + maxId =100 + + state = { + todoData: [ + {label: "Drink Coffe", important: false, id:1}, + {label: "Study React", important: true, id:2}, + {label: "Study Css", important: false, id:3}, + ] + }; + + deleteItem = (id) => { + this.setState (( { todoData }) => { + const newArray = todoData.filter((el) => el.id !== id ); + + /*const idx = todoData.findIndex ((el) => el.id === id ); + const newArray = [ + ... todoData.slice(0, idx), //before + ... todoData.slice(idx +1) //after + ];*/ + + return { + todoData: newArray + }; + }); + }; + + addItem = (text) => { + //console.log ('added',text) Для проверки работы кнопки + const newItem = { + label: text, + important: false, + id: this.maxId++ + }; + + this.setState (({ todoData }) => { + const newArr = [ + ...todoData, + newItem + ]; + + return { + todoData: newArr + }; + }); + }; + + render () { + return ( +
+ + + +
+ ); + }; +}; \ No newline at end of file diff --git a/src/components/search-panel.css b/src/components/search-panel/search-panel.css similarity index 100% rename from src/components/search-panel.css rename to src/components/search-panel/search-panel.css diff --git a/src/components/search-panel.js b/src/components/search-panel/search-panel.js similarity index 100% rename from src/components/search-panel.js rename to src/components/search-panel/search-panel.js diff --git a/src/components/todo-list-item.css b/src/components/todo-list-item.css deleted file mode 100644 index 16945f6..0000000 --- a/src/components/todo-list-item.css +++ /dev/null @@ -1,13 +0,0 @@ -.done { - text-decoration: line-through -} - -.todo-list-item.important .todo-list-item-label { - font-weight: bold; - color: rgb(124, 44, 44); -} - -.important { - font-weight: bold; - color: rgb(124, 44, 44); -} \ No newline at end of file diff --git a/src/components/todo-list-item/todo-list-item.css b/src/components/todo-list-item/todo-list-item.css new file mode 100644 index 0000000..e91ff33 --- /dev/null +++ b/src/components/todo-list-item/todo-list-item.css @@ -0,0 +1,16 @@ +.todo-list-item-label { + margin-left: 1.25rem; + line-height: 35px; + cursor: pointer; + user-select: none; +} + + +.todo-list-item.done .todo-list-item-label { + text-decoration: line-through; +} + +.todo-list-item.important .todo-list-item-label { + font-weight: bold; + color: rgb(124, 44, 44); +} \ No newline at end of file diff --git a/src/components/todo-list-item.js b/src/components/todo-list-item/todo-list-item.js similarity index 65% rename from src/components/todo-list-item.js rename to src/components/todo-list-item/todo-list-item.js index b5ad221..89636e5 100644 --- a/src/components/todo-list-item.js +++ b/src/components/todo-list-item/todo-list-item.js @@ -1,54 +1,69 @@ import React from 'react'; -import TodoList from './todo-list'; import './todo-list-item.css'; - //компонент -класс export default class TodoListItem extends React.Component { state = { done: false, - important: true + important: false }; + //Вариант без возврата предыдущего состояния state. + /*onLabelClick = () => { + this.setState({ + done:true + });*/ + //Вариант с возвратом предыдущего состояния. onLabelClick = () => { - this.setState ({ - done: true + this.setState (({done}) => { + return { + done: !done + }; + }); + }; + + onImportantClick = () => { + this.setState (({important}) => { + return { + important: !important + }; }); }; render() { - const { label } = this.props //используем деструктуризацию объекта - const { done, important } = this.state; //используем деструктуризацию: получить значение done из state + const { label, onDeleted } = this.props //используем деструктуризацию объекта + const { done, important } = this.state; //используем деструктуризацию: получить значение done и important из state let classNames = 'todo-list-item'; - if (done) { classNames += ' done'; }; if (important) { - classNames += 'important'; + classNames += ' important'; }; return ( { label } diff --git a/src/components/todo-list.css b/src/components/todo-list/todo-list.css similarity index 100% rename from src/components/todo-list.css rename to src/components/todo-list/todo-list.css diff --git a/src/components/todo-list.js b/src/components/todo-list/todo-list.js similarity index 68% rename from src/components/todo-list.js rename to src/components/todo-list/todo-list.js index 7b60e71..6ecc9a2 100644 --- a/src/components/todo-list.js +++ b/src/components/todo-list/todo-list.js @@ -1,9 +1,9 @@ import React from 'react'; -import TodoListItem from './todo-list-item'; +import TodoListItem from '../todo-list-item/todo-list-item'; import './todo-list.css' -const TodoList = ({ todos }) => { +const TodoList = ({ todos, onDeleted }) => { /*const elements = todos.map((item) => { return ( @@ -17,8 +17,13 @@ const TodoList = ({ todos }) => { //Взять каждое значение из объекта item и передать его в качестве аттрибута const elements = todos.map((item) => { + const {id, ... itemProps } = item; return ( -
  • +
  • + onDeleted(id)}/> +
  • ) }); diff --git a/src/index.js b/src/index.js index db0c9be..9c04e9e 100644 --- a/src/index.js +++ b/src/index.js @@ -1,25 +1,6 @@ import React from 'react'; import ReactDom from 'react-dom'; -import AppHeader from './components/app-header'; -import SearchPanel from './components/search-panel'; -import TodoList from './components/todo-list'; - -const App = () => { - - const todoData = [ - {label: "Drink Coffe", important: false, id:1}, - {label: "Study React", important: true, id:2}, - {label: "Study Css", important: false, id:3}, - ]; - - return ( -
    - - - -
    - ); - }; +import App from './components/app/app'; ReactDom.render(, document.getElementById('root') ) \ No newline at end of file