Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

task 1 #43

Open
wants to merge 3 commits into
base: lecture-1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 13 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { createElement } from './utils.js';
import { createElement, getPluralForm } from './utils.js';
import './styles.css';

/**
Expand Down Expand Up @@ -27,7 +27,13 @@ function App({ store }) {
onClick={() => store.selectItem(item.code)}
>
<div className="Item-code">{item.code}</div>
<div className="Item-title">{item.title}</div>
<div className="Item-title">{item.title}
{item.selectCount > 0 && (
<div className="Item-selectCount">
&nbsp;| Выделяли {item.selectCount} {getPluralForm(item.selectCount)}
</div>
)}
</div>
<div className="Item-actions">
<button onClick={() => store.deleteItem(item.code)}>Удалить</button>
</div>
Expand Down
14 changes: 7 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import Store from './store.js';

const store = new Store({
list: [
{ code: 1, title: 'Название элемента' },
{ code: 2, title: 'Некий объект' },
{ code: 3, title: 'Заголовок' },
{ code: 4, title: 'Очень длинное название элемента из семи слов' },
{ code: 5, title: 'Запись' },
{ code: 6, title: 'Шестая запись' },
{ code: 7, title: 'Седьмая запись' },
{ code: 1, title: 'Название элемента', selectCount: 0},
{ code: 2, title: 'Некий объект', selectCount: 0},
{ code: 3, title: 'Заголовок', selectCount: 0},
{ code: 4, title: 'Очень длинное название элемента из семи слов', selectCount: 0},
{ code: 5, title: 'Запись', selectCount: 0},
{ code: 6, title: 'Шестая запись', selectCount: 0},
{ code: 7, title: 'Седьмая запись', selectCount: 0},
],
});

Expand Down
30 changes: 25 additions & 5 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
*/
class Store {
constructor(initState = {}) {
this.state = initState;
this.state = {
...initState,
nextCode: initState.list.length ? initState.list.length + 1 : 1,
};
this.listeners = []; // Слушатели изменений состояния
}

Expand Down Expand Up @@ -42,9 +45,17 @@ class Store {
* Добавление новой записи
*/
addItem() {
const newItem = {
code: this.state.nextCode,
title: 'Новая запись',
selected: false,
selectCount: 0
};

this.setState({
...this.state,
list: [...this.state.list, { code: this.state.list.length + 1, title: 'Новая запись' }],
list: [...this.state.list, newItem],
nextCode: this.state.nextCode + 1
});
}

Expand All @@ -68,9 +79,18 @@ class Store {
...this.state,
list: this.state.list.map(item => {
if (item.code === code) {
item.selected = !item.selected;
}
return item;
const isSelected = !item.selected;
return {
...item,
selected: isSelected,
selectCount: isSelected ? item.selectCount + 1 : item.selectCount
};
} else {
return {
...item,
selected: false
};
}
}),
});
}
Expand Down
1 change: 1 addition & 0 deletions src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ body {
.Item-title {
width: 100%;
padding: 20px 10px;
display: flex;
}

.Item-actions {
Expand Down
6 changes: 6 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@ export function createElement(name, props = {}, ...children) {

return element;
}

export function getPluralForm(number) {
if (number % 10 === 1 && number % 100 !== 11) return 'раз';
if ([2, 3, 4].includes(number % 10) && ![12, 13, 14].includes(number % 100)) return 'раза';
return 'раз';
}
Loading