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

Add disabled row option to disable input on specific rows #1190

Merged
merged 1 commit into from
Nov 8, 2023
Merged
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
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: '[email protected]',
dob: new Date(1961, 8, 19),
disabled: true,
},
{
key: '000',
Expand All @@ -85,6 +86,7 @@ const DATA = [
last: 'Headroom',
email: '[email protected]',
dob: new Date(1984, 6, 1),
disabled: true,
},
];

Expand Down
Loading