Skip to content

add support for disabling data table selection (checkbox/radio) #3126

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

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
341 changes: 253 additions & 88 deletions components/data-table/__docs__/__snapshots__/storybook-stories.storyshot

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,39 @@ const CustomDataTableCell = ({ children, ...props }) => (
);
CustomDataTableCell.displayName = DataTableCell.displayName;

const items = [
{
id: '8IKZHZZV80',
opportunityName: 'Acme - 1,200 Widgets',
accountName: 'Acme',
closeDate: '4/10/15',
stage: 'Value Proposition',
confidence: '70%',
amount: '$25,000,000',
contact: '[email protected]',
},
{
id: '5GJOOOPWU7',
opportunityName: 'Acme - 200 Widgets',
accountName: 'Acme',
closeDate: '1/31/15',
stage: 'Prospecting',
confidence: '30%',
amount: '$5,000,000',
contact: '[email protected]',
},
{
id: '8IKZHZZV81',
opportunityName: 'salesforce.com - 1,000 Widgets',
accountName: 'salesforce.com',
closeDate: '1/31/15 3:45PM',
stage: 'Id. Decision Makers',
confidence: '60%',
amount: '$25,000',
contact: '[email protected]',
},
];

class Example extends React.Component {
static displayName = 'DataTableExample';

Expand All @@ -23,43 +56,16 @@ class Example extends React.Component {
sortColumnDirection: {
opportunityName: 'asc',
},
items: [
{
id: '8IKZHZZV80',
opportunityName: 'Acme - 1,200 Widgets',
accountName: 'Acme',
closeDate: '4/10/15',
stage: 'Value Proposition',
confidence: '70%',
amount: '$25,000,000',
contact: '[email protected]',
},
{
id: '5GJOOOPWU7',
opportunityName: 'Acme - 200 Widgets',
accountName: 'Acme',
closeDate: '1/31/15',
stage: 'Prospecting',
confidence: '30%',
amount: '$5,000,000',
contact: '[email protected]',
},
{
id: '8IKZHZZV81',
opportunityName: 'salesforce.com - 1,000 Widgets',
accountName: 'salesforce.com',
closeDate: '1/31/15 3:45PM',
stage: 'Id. Decision Makers',
confidence: '60%',
amount: '$25,000',
contact: '[email protected]',
},
],
items,
selection: [],
disabledSelection: [items[0]],
};

handleChanged = (event, data) => {
Copy link
Contributor

@interactivellama interactivellama Aug 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the event fire on a disabled checkbox? It doesn't fire for me.

If the change event never fired, then the consumer doesn't need to worry about filtering.

Copy link
Contributor Author

@meizibupt meizibupt Aug 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since checkbox is disabled, it is not triggered

Copy link
Contributor

@interactivellama interactivellama Aug 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm asking about this line.

I'm thinking that disabled doesn't mean a row can't be selected. You could force select a row (that is make it selected) and then disable it.

const selections = data.selection.filter(
			(s) => !this.state.disabledSelection.includes(s)
		);

It's up to the application to decide whether to filter by disabled rows.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. Makes sense. A row can be preselected but disabled.

this.setState({ selection: data.selection });
const selections = data.selection.filter(
(s) => !this.state.disabledSelection.includes(s)
);
this.setState({ selection: selections });
console.log('event: ', event, ', data: ', data);
};

Expand Down Expand Up @@ -124,7 +130,8 @@ class Example extends React.Component {
onRowChange={this.handleChanged}
onSort={this.handleSort}
selection={this.state.selection}
selectRows="radio"
disabledSelection={this.state.disabledSelection}
selectRows="checkbox"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the single select example for consumers. Please keep radio or choose another example.

Copy link
Contributor Author

@meizibupt meizibupt Aug 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reverted the changes. I added a disabled checkbox in fixed-header example, which is already showing checkbox

>
<DataTableColumn
isSorted={this.state.sortColumn === 'opportunityName'}
Expand Down
68 changes: 36 additions & 32 deletions components/data-table/__examples__/advanced-single-select.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,39 @@ const CustomDataTableCell = ({ children, ...props }) => (
);
CustomDataTableCell.displayName = DataTableCell.displayName;

const items = [
{
id: '8IKZHZZV80',
opportunityName: 'Acme - 1,200 Widgets',
accountName: 'Acme',
closeDate: '4/10/15',
stage: 'Value Proposition',
confidence: '70%',
amount: '$25,000,000',
contact: '[email protected]',
},
{
id: '5GJOOOPWU7',
opportunityName: 'Acme - 200 Widgets',
accountName: 'Acme',
closeDate: '1/31/15',
stage: 'Prospecting',
confidence: '30%',
amount: '$5,000,000',
contact: '[email protected]',
},
{
id: '8IKZHZZV81',
opportunityName: 'salesforce.com - 1,000 Widgets',
accountName: 'salesforce.com',
closeDate: '1/31/15 3:45PM',
stage: 'Id. Decision Makers',
confidence: '60%',
amount: '$25,000',
contact: '[email protected]',
},
];

class Example extends React.Component {
static displayName = 'DataTableExample';

Expand All @@ -23,39 +56,9 @@ class Example extends React.Component {
sortColumnDirection: {
opportunityName: 'asc',
},
items: [
{
id: '8IKZHZZV80',
opportunityName: 'Acme - 1,200 Widgets',
accountName: 'Acme',
closeDate: '4/10/15',
stage: 'Value Proposition',
confidence: '70%',
amount: '$25,000,000',
contact: '[email protected]',
},
{
id: '5GJOOOPWU7',
opportunityName: 'Acme - 200 Widgets',
accountName: 'Acme',
closeDate: '1/31/15',
stage: 'Prospecting',
confidence: '30%',
amount: '$5,000,000',
contact: '[email protected]',
},
{
id: '8IKZHZZV81',
opportunityName: 'salesforce.com - 1,000 Widgets',
accountName: 'salesforce.com',
closeDate: '1/31/15 3:45PM',
stage: 'Id. Decision Makers',
confidence: '60%',
amount: '$25,000',
contact: '[email protected]',
},
],
items,
selection: [],
disabledSelection: [items[0]],
};

handleChanged = (event, data) => {
Expand Down Expand Up @@ -123,6 +126,7 @@ class Example extends React.Component {
onRowChange={this.handleChanged}
onSort={this.handleSort}
selection={this.state.selection}
disabledSelection={this.state.disabledSelection}
selectRows="radio"
>
<DataTableColumn
Expand Down
39 changes: 39 additions & 0 deletions components/data-table/__tests__/data-table.browser-test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,26 @@ describe('DataTable: ', function describeFunction() {
checkedBoxes.should.have.length(1);
});

it('can start with a row disabled', function () {
renderTable(
<DataTable
{...defaultProps}
selection={[]}
disabledSelection={defaultSelection}
>
{columns.map((columnProps) => (
<DataTableColumn {...columnProps} key={columnProps.property} />
))}
</DataTable>
).call(this);

const tbody = getTable(this.dom).querySelectorAll('tbody')[0];
const disabledRows = tbody.querySelectorAll(
'.slds-checkbox input:disabled'
);
disabledRows.should.have.length(1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should spy the onRowChange and make sure it doesn't fire OR make sure the data.selection parameter is still 0 in length. See can select a row below and negate it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated test

});

it('can deselect a row', function (done) {
this.onRowChange = (event, { selection }) => {
selection.should.have.length(0);
Expand Down Expand Up @@ -421,6 +441,25 @@ describe('DataTable: ', function describeFunction() {
radios.should.have.length(1);
});

it('can start with a row disabled', function () {
renderTable(
<DataTable
{...defaultProps}
selection={[]}
disabledSelection={defaultSelection}
selectRows="radio"
>
{columns.map((columnProps) => (
<DataTableColumn {...columnProps} key={columnProps.property} />
))}
</DataTable>
).call(this);

const tbody = getTable(this.dom).querySelectorAll('tbody')[0];
const radios = tbody.querySelectorAll('.slds-radio input:disabled');
radios.should.have.length(1);
});

it('can select a row', function (done) {
this.onRowChange = (event, { selection }) => {
selection.should.have.length(1);
Expand Down
13 changes: 12 additions & 1 deletion components/data-table/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const defaultProps = {
loadingMore: 'Loading more',
},
selection: [],
disabledSelection: [],
hasMore: false,
loadMoreOffset: 20,
resizable: false,
Expand Down Expand Up @@ -344,6 +345,11 @@ class DataTable extends React.Component {
* An array of objects of selected rows. See `items` prop for shape of objects.
*/
selection: PropTypes.array,

/**
* An array of objects of rows that selection is disabled. See `items` prop for shape of objects.
*/
disabledSelection: PropTypes.array,
/**
* Specifies a row selection UX pattern.
* * `checkbox`: Multiple row selection.
Expand Down Expand Up @@ -1090,7 +1096,11 @@ class DataTable extends React.Component {
const numRows = count(this.props.items);
const numSelected = count(this.props.selection);
const numNonHeaderRows = count(
this.props.items.filter((item) => item.type !== 'header-row')
this.props.items.filter(
(item) =>
item.type !== 'header-row' &&
!this.props.disabledSelection.includes(item)
)
);
const canSelectRows =
this.props.selectRows && numNonHeaderRows > 0
Expand Down Expand Up @@ -1234,6 +1244,7 @@ class DataTable extends React.Component {
key={rowId}
onToggle={this.handleRowToggle}
selection={this.props.selection}
disabledSelection={this.props.disabledSelection}
rowActions={RowActions}
tableId={this.getId()}
rowIndex={index + numHeaderRows}
Expand Down
1 change: 1 addition & 0 deletions components/data-table/private/row.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ declare module '@salesforce/design-system-react/components/data-table/private/ro
onToggle?: (v: any) => any;
rowActions?: React.ReactElement;
selection?: any[];
disabledSelection?: any[];
tableId?: string;
};
/**
Expand Down
7 changes: 7 additions & 0 deletions components/data-table/private/row.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const propTypes = {
rowActions: PropTypes.element,
selection: PropTypes.array,
tableId: PropTypes.string,
disabledSelection: PropTypes.array,
};

/**
Expand Down Expand Up @@ -87,6 +88,9 @@ const DataTableRow = (props) => {
);

const isSelected = !!find(props.selection, item);
const isDisabled =
props.disabledSelection && !!find(props.disabledSelection, item);

const ariaProps = useMemo(() => {
const result = {};

Expand Down Expand Up @@ -140,6 +144,7 @@ const DataTableRow = (props) => {
labelId={`${props.id}-SelectRow-label`}
name={`${props.tableId}-SelectRow`}
onChange={handleToggle}
disabled={isDisabled}
/>
) : (
<InteractiveCheckbox
Expand All @@ -154,6 +159,7 @@ const DataTableRow = (props) => {
labelId={`${props.id}-SelectRow-label`}
name={`SelectRow${props.index + 1}`}
onChange={handleToggle}
disabled={isDisabled}
/>
)}
</CellContext.Provider>
Expand Down Expand Up @@ -211,6 +217,7 @@ const DataTableRow = (props) => {
handleToggle,
hasFocus,
isSelected,
isDisabled,
item,
tabIndex,
props.assistiveText.selectRow,
Expand Down