Skip to content

Commit

Permalink
feat(UncontrolledTable): allow disabled attribute to be passed in wit…
Browse files Browse the repository at this point in the history
…h rows
  • Loading branch information
CRiva committed Nov 8, 2023
1 parent f2d33dd commit 02c6951
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/components/Table/SortableTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ function getSelectableCell(row, rowSelected, onSelect) {
checked={rowSelected(row)}
onClick={(e) => e.stopPropagation()}
onChange={(e) => onSelect(row, e.target.checked)}
disabled={!!row.disabled}
/>
</>
);
Expand Down
18 changes: 18 additions & 0 deletions src/components/Table/SortableTable.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,24 @@ describe('<SortableTable />', () => {
assert.equal(wrapper.find('input').length, 4, 'select checkbox missing');
});

it('should disable select checkbox when specified', () => {
const rows = [
{ name: 'Alpha', disabled: false },
{ name: 'Bravo', disabled: true },
{ name: 'Charlie', disabled: true },
{ name: 'Delta', disabled: 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'), false);
assert.equal(trs.at(2).find('input').prop('disabled'), true);
assert.equal(trs.at(3).find('input').prop('disabled'), true);
assert.equal(trs.at(4).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 @@ -78,7 +78,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) => !row.disabled);
return this.props.rows.length && this.state.selected.length === selectableRows.length;
}

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

toggleAll = () => {
const newSelection = this.allSelected ? [] : this.props.rows;
const selectableRows = this.props.rows.filter((row) => !row.disabled);
const newSelection = this.allSelected ? [] : selectableRows;

this.setState({ selected: newSelection }, () => {
this.props.onSelect(newSelection);
Expand Down
34 changes: 34 additions & 0 deletions src/components/Table/UncontrolledTable.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,40 @@ 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', disabled: true },
{ name: 'Bravo', disabled: false },
{ name: 'Charlie', disabled: false },
{ name: 'Delta' },
];
const wrapper = mount(<UncontrolledTable columns={columns} rows={rows} selectable />);

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

it('should only select on selectable rows when selecting selectAll', () => {
const columns = [{ header: 'Name', cell: (row) => row.name }];
const rows = [
{ name: 'Alpha', disabled: true },
{ name: 'Bravo', disabled: false },
{ name: 'Charlie', disabled: false },
{ name: 'Delta', disabled: false },
];
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: 'maxine.turner@example.com',
dob: new Date(1961, 8, 19),
disabled: true,
},
{
key: '000',
Expand All @@ -85,6 +86,7 @@ const DATA = [
last: 'Headroom',
email: 'max.headroom@example.com',
dob: new Date(1984, 6, 1),
disabled: true,
},
];

Expand Down

0 comments on commit 02c6951

Please sign in to comment.