Skip to content

Commit

Permalink
Improve item selection, unique ID generation, and selection count dis…
Browse files Browse the repository at this point in the history
…play.
  • Loading branch information
zart227 committed Sep 10, 2024
1 parent 1a3af94 commit ab05e2a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
12 changes: 10 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 } from './utils.js';
import './styles.css';

/**
Expand Down Expand Up @@ -28,8 +28,16 @@ function App({ store }) {
>
<div className="Item-code">{item.code}</div>
<div className="Item-title">{item.title}</div>
{item.selectionCount > 0 && (
<div className="Item-selectionCount">
Выделяли {item.selectionCount} раз
</div>
)}
<div className="Item-actions">
<button onClick={() => store.deleteItem(item.code)}>Удалить</button>
<button onClick={(e) => {
e.stopPropagation();
store.deleteItem(item.code)
}}>Удалить</button>
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { createRoot } from 'react-dom/client';
import { createElement } from './utils.js';
// import { createElement } from './utils.js';
import App from './app.js';
import Store from './store.js';

Expand Down
13 changes: 11 additions & 2 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 + 1, // Начальное значение для уникальных кодов
}
this.listeners = []; // Слушатели изменений состояния
}

Expand Down Expand Up @@ -44,7 +47,8 @@ class Store {
addItem() {
this.setState({
...this.state,
list: [...this.state.list, { code: this.state.list.length + 1, title: 'Новая запись' }],
list: [...this.state.list, { code: this.state.nextCode, title: 'Новая запись' }],
nextCode: this.state.nextCode + 1,
});
}

Expand All @@ -69,6 +73,11 @@ class Store {
list: this.state.list.map(item => {
if (item.code === code) {
item.selected = !item.selected;
if (item.selected) {
item.selectionCount = (item.selectionCount || 0) + 1;
}
} else {
item.selected = false;
}
return item;
}),
Expand Down
6 changes: 6 additions & 0 deletions src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,9 @@ body {
justify-content: flex-end;
padding: 20px 20px 20px 10px;
}

.Item-selectionCount {
font-size: 12px;
color: #888;
padding: 5px 0;
}

0 comments on commit ab05e2a

Please sign in to comment.