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

Added the ability to support single selection in the selection plugin. #1340

Open
wants to merge 5 commits into
base: master
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
12 changes: 6 additions & 6 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"umd": "./plugins/selection/dist/selection.umd.js",
"import": "./plugins/selection/dist/selection.mjs",
"require": "./plugins/selection/dist/selection.js"
},
},
"./package.json": "./package.json",
"./": "./"
},
Expand Down
13 changes: 11 additions & 2 deletions plugins/selection/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { RowSelection } from './src/rowSelection/rowSelection';
import {
RowSelection,
RowSelectionSingle,
RowSelectionMultiple,
} from './src/rowSelection/rowSelection';
import * as RowSelectionActions from './src/rowSelection/actions';

export { RowSelection, RowSelectionActions };
export {
RowSelection,
RowSelectionSingle,
RowSelectionMultiple,
RowSelectionActions,
};
137 changes: 68 additions & 69 deletions plugins/selection/package-lock.json

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

2 changes: 1 addition & 1 deletion plugins/selection/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "selection",
"version": "4.0.0",
"version": "5.0.0",
"private": true,
"description": "Adds row and cell selection to Grid.js",
"author": "Afshin Mehrabani <afshin.meh@gmail.com>",
Expand Down
30 changes: 20 additions & 10 deletions plugins/selection/src/rowSelection/actions.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
export const CheckRow = (rowId: string) => (state) => {
const rowIds = state.rowSelection?.rowIds || [];
export const CheckRow =
(rowId: string, singleSelection: boolean) => (state) => {
const rowIds = state.rowSelection?.rowIds || [];

// rowId already exists
if (rowIds.indexOf(rowId) > -1) return state;
// rowId already exists
if (rowIds.indexOf(rowId) > -1) return state;

return {
...state,
rowSelection: {
rowIds: [rowId, ...rowIds],
},
if (singleSelection) {
return {
...state,
rowSelection: {
rowIds: [rowId],
},
};
}

return {
...state,
rowSelection: {
rowIds: [rowId, ...rowIds],
},
};
};
};

export const UncheckRow = (rowId: string) => (state) => {
const rowIds = state.rowSelection?.rowIds || [];
Expand Down
28 changes: 23 additions & 5 deletions plugins/selection/src/rowSelection/rowSelection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,22 @@ interface RowSelectionProps {
cell?: Cell;
}

export function RowSelectionSingle(props: RowSelectionProps) {
return RowSelectionCore(props, true);
}

export function RowSelectionMultiple(props: RowSelectionProps) {
return RowSelectionCore(props, false);
}

export function RowSelection(props: RowSelectionProps) {
return RowSelectionCore(props, false);
}

export function RowSelectionCore(
props: RowSelectionProps,
singleSelect: boolean = false,
) {
const { dispatch } = useStore();
const state = useSelector((state) => state.rowSelection);
const [isChecked, setIsChecked] = useState(false);
Expand All @@ -22,6 +37,8 @@ export function RowSelection(props: RowSelectionProps) {
this.base.parentElement &&
(this.base.parentElement.parentElement as Element);

const isSingleSelect: boolean = singleSelect;

useEffect(() => {
// store/dispatcher is required only if we are rendering a TD (not a TH)
if (props.cell?.data && isDataCell(props)) {
Expand All @@ -31,14 +48,15 @@ export function RowSelection(props: RowSelectionProps) {
}, []);

useEffect(() => {
const parent = getParentTR();

if (!parent) return;

const rowIds = state?.rowIds || [];
const isChecked = rowIds.indexOf(props.row.id) > -1;

setIsChecked(isChecked);

const parent = getParentTR();

if (!parent) return;

if (isChecked) {
parent.classList.add(selectedClassName);
} else {
Expand All @@ -47,7 +65,7 @@ export function RowSelection(props: RowSelectionProps) {
}, [state]);

const check = () => {
dispatch(actions.CheckRow(props.row.id));
dispatch(actions.CheckRow(props.row.id, isSingleSelect));
props.cell?.update(true);
};

Expand Down
22 changes: 18 additions & 4 deletions plugins/selection/tests/rowSelection/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as Actions from '../../src/rowSelection/actions';

describe('Actions', () => {
it('should trigger CHECK', () => {
const state = Actions.CheckRow('42')({});
const state = Actions.CheckRow('42', false)({});

expect(state).toStrictEqual({
rowSelection: {
Expand All @@ -12,7 +12,10 @@ describe('Actions', () => {
});

it('should trigger CHECK when rowIds already exists', () => {
const state = Actions.CheckRow('42')({
const state = Actions.CheckRow(
'42',
false,
)({
rowSelection: {
rowIds: ['24'],
},
Expand Down Expand Up @@ -75,8 +78,8 @@ describe('Actions', () => {

it('should CHECK and UNCHECK', () => {
let state = {};
state = Actions.CheckRow('42')(state);
state = Actions.CheckRow('24')(state);
state = Actions.CheckRow('42', false)(state);
state = Actions.CheckRow('24', false)(state);
state = Actions.UncheckRow('42')(state);

expect(state).toStrictEqual({
Expand All @@ -85,4 +88,15 @@ describe('Actions', () => {
},
});
});

it('should CHECK (SINGLE SELECT)', () => {
let state = {};
state = Actions.CheckRow('42', true)(state);
state = Actions.CheckRow('24', true)(state);
expect(state).toStrictEqual({
rowSelection: {
rowIds: ['24'],
},
});
});
});
2 changes: 1 addition & 1 deletion tests/dev-server/package-lock.json

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