Skip to content

Commit

Permalink
feat(UncontrolledTable): allow selectable attribute to be passed in w…
Browse files Browse the repository at this point in the history
…ith rows
  • Loading branch information
CRiva committed Sep 15, 2023
1 parent 1de10bf commit 71bbc30
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/components/Table/SortableTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ function defaultRenderRow(

function getSelectableCell(row, rowSelected, onSelect) {
const selectRowId = uniqueId('select-row-');
const rowSelectable = Object.hasOwn(row, 'selectable') ? row.selectable : true
return (
<>
<Label for={selectRowId} hidden>
Expand All @@ -86,6 +87,7 @@ function getSelectableCell(row, rowSelected, onSelect) {
checked={rowSelected(row)}
onClick={(e) => e.stopPropagation()}
onChange={(e) => onSelect(row, e.target.checked)}
disabled={!rowSelectable}
/>
</>
);
Expand Down
13 changes: 13 additions & 0 deletions src/components/Table/SortableTable.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,19 @@ describe('<SortableTable />', () => {
assert.equal(wrapper.find('input').length, 4, 'select checkbox missing');
});

it('should disable select checkbox when specified', () => {
const rows = [
{name: 'Alpha', selectable: false},
{name: 'Bravo', selectable: true},
{name: 'Charlie', selectable: true},
{name: 'Delta', selectable: true}
];
const wrapper = mount(<SortableTable columns={columns} rows={rows} rowSelected={() => false} />);

const trs = wrapper.find('tr');
assert.equal(trs.at(1).find('input').prop('disabled'), true);
});

it('should call onSelect when clicked', () => {
const onSelect = sinon.stub();
const wrapper = mount(
Expand Down
6 changes: 4 additions & 2 deletions src/components/Table/UncontrolledTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ export default class UncontrolledTable extends React.Component {
};

get allSelected() {
return this.props.rows.length && this.state.selected.length === this.props.rows.length;
const selectableRows = this.props.rows.filter((row) => Object.hasOwn(row, 'selectable') ? row.selectable : true)
return this.props.rows.length && this.state.selected.length === selectableRows.length;
}

selected(value) {
Expand All @@ -88,7 +89,8 @@ export default class UncontrolledTable extends React.Component {
};

toggleAll = () => {
const newSelection = this.allSelected ? [] : this.props.rows;
const selectableRows = this.props.rows.filter((row) => Object.hasOwn(row, 'selectable') ? row.selectable : true)
const newSelection = this.allSelected ? [] : selectableRows;

this.setState({ selected: newSelection }, () => {
this.props.onSelect(newSelection);
Expand Down
31 changes: 31 additions & 0 deletions src/components/Table/UncontrolledTable.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,37 @@ describe('<UncontrolledTable />', () => {
assert.equal(ths.length, columns.length + 1); // For selectable column
});

it('should disable select checkbox when specified', () => {
const columns = [{ header: 'Name', cell: (row) => row.name }];
const rows = [
{name: 'Alpha', selectable: false},
{name: 'Bravo', selectable: true},
{name: 'Charlie', selectable: true},
{name: 'Delta', selectable: true}
];
const wrapper = mount(<UncontrolledTable columns={columns} rows={rows} selectable />);

const trs = wrapper.find('tr');
assert.equal(trs.at(1).find('input').prop('disabled'), true);
});

it('should only select on selectable rows when selecting selectAll', () => {
const columns = [{ header: 'Name', cell: (row) => row.name }];
const rows = [
{name: 'Alpha', selectable: false},
{name: 'Bravo', selectable: true},
{name: 'Charlie', selectable: true},
{name: 'Delta', selectable: true}
];
const wrapper = mount(<UncontrolledTable columns={columns} rows={rows} selectable />);

wrapper
.find({ type: 'checkbox' })
.first()
.simulate('change', { target: { checked: true } });
assert.equal(wrapper.state().selected.length, 3);
});

it('should call onSelect when selectable row picked', () => {
const columns = [{ header: 'Name', cell: (row) => row }];
const rows = ['Alpha', 'Bravo', 'Charlie', 'Delta', 'Echo', 'Foxtrot', 'Golf', 'Hotel'];
Expand Down
2 changes: 2 additions & 0 deletions src/components/Table/UncontrolledTable.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const DATA = [
last: 'Turner',
email: '[email protected]',
dob: new Date(1961, 8, 19),
selectable: false
},
{
key: '000',
Expand All @@ -85,6 +86,7 @@ const DATA = [
last: 'Headroom',
email: '[email protected]',
dob: new Date(1984, 6, 1),
selectable: false
},
];

Expand Down

0 comments on commit 71bbc30

Please sign in to comment.