Skip to content

Commit 807d1ad

Browse files
committed
fix(plugins/selection): remove the props.id and other unused props
1 parent 9318faa commit 807d1ad

File tree

3 files changed

+100
-15
lines changed

3 files changed

+100
-15
lines changed

plugins/selection/src/rowSelection/rowSelection.tsx

+8-12
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,19 @@ import { Row } from 'gridjs';
55
import { Cell } from 'gridjs';
66

77
interface RowSelectionProps {
8-
// row identifier
9-
id: (row?: Row) => string;
108
// it's optional because thead doesn't have a row
119
row?: Row;
1210
cell?: Cell;
13-
selectedClassName?: string;
14-
checkboxClassName?: string;
1511
}
1612

1713
export function RowSelection(props: RowSelectionProps) {
1814
const { dispatch } = useStore();
1915
const state = useSelector((state) => state.rowSelection);
2016
const [isChecked, setIsChecked] = useState(false);
21-
const selectedClassName =
22-
props.selectedClassName || className('tr', 'selected');
23-
const checkboxClassName = props.checkboxClassName || className('checkbox');
24-
const isDataCell = (props): boolean => props.row !== undefined;
25-
const getParentTR = (): Element =>
17+
const selectedClassName = className('tr', 'selected');
18+
const checkboxClassName = className('checkbox');
19+
const isDataCell = (props) => props.row !== undefined;
20+
const getParentTR = () =>
2621
this.base &&
2722
this.base.parentElement &&
2823
(this.base.parentElement.parentElement as Element);
@@ -40,7 +35,8 @@ export function RowSelection(props: RowSelectionProps) {
4035

4136
if (!parent) return;
4237

43-
const isChecked = state.rowIds.indexOf(props.id(props.row)) > -1;
38+
const rowIds = state?.rowIds || [];
39+
const isChecked = rowIds.indexOf(props.row.id) > -1;
4440
setIsChecked(isChecked);
4541

4642
if (isChecked) {
@@ -51,12 +47,12 @@ export function RowSelection(props: RowSelectionProps) {
5147
}, [state]);
5248

5349
const check = () => {
54-
dispatch(actions.CheckRow(props.id(props.row)));
50+
dispatch(actions.CheckRow(props.row.id));
5551
props.cell?.update(true);
5652
};
5753

5854
const uncheck = () => {
59-
dispatch(actions.UncheckRow(props.id(props.row)));
55+
dispatch(actions.UncheckRow(props.row.id));
6056
props.cell?.update(false);
6157
};
6258

src/state/store.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export class Store<S = Record<string, unknown>> {
22
private state: S;
3-
private listeners: (() => void)[] = [];
3+
private listeners: ((current?: S, prev?: S) => void)[] = [];
44
private isDispatching = false;
55

66
constructor(initialState: S) {
@@ -18,20 +18,21 @@ export class Store<S = Record<string, unknown>> {
1818

1919
this.isDispatching = true;
2020

21+
const prevState = this.state;
2122
try {
2223
this.state = reducer(this.state);
2324
} finally {
2425
this.isDispatching = false;
2526
}
2627

2728
for (const listener of this.listeners) {
28-
listener();
29+
listener(this.state, prevState);
2930
}
3031

3132
return this.state;
3233
};
3334

34-
subscribe = (listener: () => void): (() => void) => {
35+
subscribe = (listener: (current?: S, prev?: S) => void): (() => void) => {
3536
if (typeof listener !== 'function')
3637
throw new Error('Listener is not a function');
3738

tests/jest/state/store.test.ts

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { Store } from '../../../src/state/store';
2+
3+
describe('Store', () => {
4+
it('should set the initial state', () => {
5+
const stubState = {
6+
a: 45,
7+
};
8+
const store = new Store(stubState);
9+
expect(store.getState()).toEqual(stubState);
10+
});
11+
12+
it('should update the state', () => {
13+
const store = new Store({
14+
hello: 'world',
15+
});
16+
store.dispatch((state) => {
17+
return {
18+
...state,
19+
newKey: 42,
20+
};
21+
});
22+
expect(store.getState()).toEqual({
23+
hello: 'world',
24+
newKey: 42,
25+
});
26+
});
27+
28+
it('should override the state', () => {
29+
const store = new Store({
30+
hello: 'world',
31+
});
32+
store.dispatch((state) => {
33+
return {
34+
...state,
35+
hello: 'updated',
36+
};
37+
});
38+
expect(store.getState()).toEqual({
39+
hello: 'updated',
40+
});
41+
});
42+
43+
it('should call the subscribers', () => {
44+
const store = new Store({
45+
hello: 'world',
46+
});
47+
48+
const mockSubscriber = jest.fn();
49+
50+
store.subscribe(mockSubscriber);
51+
52+
store.dispatch((state) => {
53+
return {
54+
...state,
55+
hello: 'updated',
56+
newKey: 42,
57+
};
58+
});
59+
60+
expect(mockSubscriber).toBeCalledTimes(1);
61+
expect(mockSubscriber).toBeCalledWith(
62+
{
63+
hello: 'updated',
64+
newKey: 42,
65+
},
66+
{
67+
hello: 'world',
68+
},
69+
);
70+
});
71+
72+
it('should return a list of subscribers', () => {
73+
const store = new Store({
74+
hello: 'world',
75+
});
76+
77+
const mockSubscriber1 = jest.fn();
78+
const mockSubscriber2 = jest.fn();
79+
80+
store.subscribe(mockSubscriber1);
81+
store.subscribe(mockSubscriber2);
82+
83+
expect(store.getListeners()).toHaveLength(2);
84+
expect(store.getListeners()).toEqual(
85+
expect.arrayContaining([mockSubscriber2, mockSubscriber1]),
86+
);
87+
});
88+
});

0 commit comments

Comments
 (0)