Skip to content

Commit

Permalink
allow deselect when selectableRowsSingle (#822)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbetancur committed Jun 13, 2021
1 parent 618f885 commit 614a1b7
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-data-table-component",
"version": "7.0.0-alpha-14",
"version": "7.0.0-alpha-15",
"description": "A declarative react based data table",
"main": "dist/index.cjs.js",
"module": "dist/index.es.js",
Expand Down
16 changes: 16 additions & 0 deletions src/DataTable/__tests__/DataTable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,22 @@ describe('DataTable::selectableRows', () => {
expect(container.firstChild).toMatchSnapshot();
});

test('should de-select a single row when clicked again', () => {
const mock = dataMock();
const { container } = render(
<DataTable data={mock.data} columns={mock.columns} selectableRows selectableRowsSingle />,
);

const rowCheck = container.querySelector('input[name="select-row-1"]') as HTMLInputElement;

fireEvent.click(rowCheck);
expect(rowCheck.checked).toBe(true);

fireEvent.click(rowCheck);

expect(rowCheck.checked).toBe(false);
});

test('should clear all rows selectableRowsSingle is changed', () => {
const mock = dataMock();
const { container, rerender } = render(<DataTable data={mock.data} columns={mock.columns} selectableRows />);
Expand Down
14 changes: 12 additions & 2 deletions src/DataTable/tableReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,26 @@ export function tableReducer<T extends RowRecord>(state: TableState<T>, action:
case 'SELECT_SINGLE_ROW': {
const { keyField, row, isSelected, rowCount, singleSelect } = action;

// single select mode allows only 1 row to be selected at a time
// handle single select mode
if (singleSelect) {
if (isSelected) {
return {
...state,
selectedCount: 0,
allSelected: false,
selectedRows: [],
};
}

return {
...state,
selectedCount: 0, // this also has the effect of disabling the context menu
selectedCount: 1,
allSelected: false,
selectedRows: insertItem([], row),
};
}

// handlesmulti select mode
if (isSelected) {
return {
...state,
Expand Down
40 changes: 31 additions & 9 deletions stories/DataTable/Basic/1BasicKitchenSink.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ import TextField from '@material-ui/core/TextField';
import data from '../constants/sampleMovieData';
import DataTable from '../../../src/index';

const subHeaderComponent = (
<div style={{ display: 'flex', alignItems: 'center' }}>
<TextField id="outlined-basic" label="Search" variant="outlined" size="small" style={{ margin: '5px' }} />
<Icon1 style={{ margin: '5px' }} color="action" />
<Icon2 style={{ margin: '5px' }} color="action" />
<Icon3 style={{ margin: '5px' }} color="action" />
</div>
);

const columns = [
{
name: 'Title',
Expand All @@ -37,6 +46,7 @@ const KitchenSink = () => {
const [selectableRowsVisibleOnly, setSelectableRowsVisibleOnly] = React.useState(false);
const [selectableRowsHighlight, setSelectableRowsHighlight] = React.useState(false);
const [selectableRowsSingle, setSelectableRowsSingle] = React.useState(false);
const [selectableRowsRadio, setSelectableRowsRadio] = React.useState(false);
const [expandableRows, setExpandableRows] = React.useState(false);
const [expandOnRowClick, setExpandOnRowClick] = React.useState(false);
const [pagination, setPagination] = React.useState(true);
Expand All @@ -55,6 +65,13 @@ const KitchenSink = () => {
const [direction, setDirection] = React.useState(false);
const [directionValue, setDirectionValue] = React.useState('auto');

const selectableRowsComponentProps = React.useMemo(
() => ({
type: selectableRowsRadio ? 'radio' : 'checkbox',
}),
[selectableRowsRadio],
);

return (
<div>
<FormControlLabel
Expand Down Expand Up @@ -108,6 +125,17 @@ const KitchenSink = () => {
label="Single Select Mode"
/>

<FormControlLabel
control={
<Checkbox
size="small"
checked={selectableRowsRadio}
onChange={() => setSelectableRowsRadio(!selectableRowsRadio)}
/>
}
label="Radio Button"
/>

<FormControlLabel
control={<Checkbox size="small" checked={tableHead} onChange={() => setNoHead(!tableHead)} />}
label="No Table Head"
Expand Down Expand Up @@ -240,7 +268,7 @@ const KitchenSink = () => {
data={data}
defaultSortField="title"
selectableRows={selectableRows}
selectableRowsComponentProps={{ type: 'radio' }}
selectableRowsComponentProps={selectableRowsComponentProps}
selectableRowsNoSelectAll={noSelectAll}
selectableRowsHighlight={selectableRowsHighlight}
selectableRowsSingle={selectableRowsSingle}
Expand All @@ -257,14 +285,8 @@ const KitchenSink = () => {
progressPending={loading}
noHeader={noHeader}
subHeader={subHeader}
subHeaderComponent={
<div style={{ display: 'flex', alignItems: 'center' }}>
<TextField id="outlined-basic" label="Search" variant="outlined" size="small" style={{ margin: '5px' }} />
<Icon1 style={{ margin: '5px' }} color="action" />
<Icon2 style={{ margin: '5px' }} color="action" />
<Icon3 style={{ margin: '5px' }} color="action" />
</div>
}
noContextMenu={noContextMenu}
subHeaderComponent={subHeaderComponent}
subHeaderAlign={subHeaderAlign}
fixedHeader={fixedHeader}
fixedHeaderScrollHeight="300px"
Expand Down

0 comments on commit 614a1b7

Please sign in to comment.