Skip to content

Commit dc70d88

Browse files
committed
update selected props
1 parent c647e78 commit dc70d88

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

frameworks/keyed/san-store/src/App.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class AppComponent extends Component {
3535
</div>
3636
<table class="table table-hover table-striped test-data" on-click="handleClick($event)">
3737
<tbody>
38-
<tr s-for="item in rows trackBy item.id" class="{{selected === item.id ? 'danger':''}}">
38+
<tr s-for="item in rows trackBy item.id" class="{{item.selected ? 'danger':''}}">
3939
<td class="col-md-1">{{item.id}}</td>
4040
<td class="col-md-4">
4141
<a data-action="select" data-id="{{item.id}}">{{item.label}}</a>
@@ -87,8 +87,7 @@ class AppComponent extends Component {
8787

8888
export default connectSan(
8989
{
90-
rows: 'rows',
91-
selected: 'selected'
90+
rows: 'rows'
9291
},
9392
{
9493
run: 'run',

frameworks/keyed/san-store/src/store.js

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,59 +8,70 @@ const C = ['red', 'yellow', 'blue', 'green', 'pink', 'brown', 'purple', 'brown',
88
const N = ['table', 'chair', 'house', 'bbq', 'desk', 'car', 'pony', 'cookie', 'sandwich', 'burger', 'pizza',
99
'mouse', 'keyboard'];
1010

11-
const random = max => Math.round(Math.random() * 1000) % max;
11+
function random(max) {
12+
return Math.round(Math.random() * 1000) % max;
13+
}
1214

1315
let nextId = 1;
1416
function buildData(count = 1000) {
1517
const data = [];
1618
for (let i = 0; i < count; i++) {
1719
data.push({
1820
id: nextId++,
19-
label: `${A[random(A.length)]} ${C[random(C.length)]} ${N[random(N.length)]}`
21+
label: A[random(A.length)] + " " + C[random(C.length)] + " " + N[random(N.length)],
22+
selected: false
2023
});
2124
}
2225
return data;
2326
}
2427

2528
const store = new Store({
2629
initData: {
27-
rows: [],
28-
selected: 0
30+
rows: []
2931
},
3032

3133
actions: {
32-
setSelected(id) {
33-
return builder().set('selected', id);
34+
setSelected(id, {getState, dispatch}) {
35+
const rows = getState('rows');
36+
const newData = rows.slice(0);
37+
38+
rows.forEach((el, i) => {
39+
if (el.selected) {
40+
newData[i] = {...rows[i], selected: false}
41+
}
42+
if (el.id === id) {
43+
newData[i] = {...rows[i], selected: true}
44+
}
45+
});
46+
return builder().set('rows', newData);
3447
},
35-
run(n, {dispatch}) {
36-
dispatch('setSelected', 0);
48+
run(n) {
3749
return builder().set('rows', buildData(n));
3850
},
3951
add(n, {getState}) {
4052
const rows = getState('rows');
4153
return builder().set('rows', rows.concat(buildData(n)));
4254
},
4355
update(n, {getState}) {
44-
const newData = getState('rows').slice();
56+
const newData = getState('rows').slice(0);
4557
for (let i = 0; i < newData.length; i += 10) {
4658
const r = newData[i];
4759
newData[i] = {id: r.id, label: r.label + ' !!!'};
4860
}
4961
return builder().set('rows', newData);
5062
},
51-
clear(n, {dispatch}) {
52-
dispatch('setSelected', 0);
63+
clear(n) {
5364
return builder().set('rows', []);
5465
},
5566
remove(id, {getState}) {
56-
const newData = getState('rows').slice();
67+
const newData = getState('rows').slice(0);
5768
const idx = newData.findIndex(d => d.id === id);
5869

5970
newData.splice(idx, 1);
6071
return builder().set('rows', newData);
6172
},
6273
swapRows(n, {getState}) {
63-
const newData = getState('rows').slice();
74+
const newData = getState('rows').slice(0);
6475
if (newData.length > 998) {
6576
const tmp = newData[1];
6677
newData[1] = newData[998];

0 commit comments

Comments
 (0)