Skip to content

Commit 6811977

Browse files
committed
New propery for first time sorting direction
1 parent 0a890ff commit 6811977

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

components/data-table/__examples__/advanced-single-select-fixed-header.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,12 @@ class Example extends React.Component {
131131
selectRows="radio"
132132
>
133133
<DataTableColumn
134-
isSorted={this.state.sortColumn === 'opportunityName'}
134+
isSorted={false}
135135
label="Name"
136136
primaryColumn
137137
property="opportunityName"
138138
sortable
139-
sortDirection={this.state.sortColumnDirection.opportunityName}
139+
sortDirection={null}
140140
width="10rem"
141141
>
142142
<CustomDataTableCell />

components/data-table/__tests__/data-table.browser-test.jsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,28 @@ describe('DataTable: ', function() {
396396
Simulate.click(sortButton, {});
397397
});
398398

399+
it('first clicked on sortable column header should result in sort specifid in firstSortDirection if given', function(done) {
400+
this.onSort = (data) => {
401+
data.property.should.equal('count');
402+
data.sortDirection.should.equal('desc');
403+
done();
404+
};
405+
406+
renderTable(
407+
<DataTable {...defaultProps} fixedLayout onSort={this.onSort}>
408+
{columns.map((columnProps) => (
409+
<DataTableColumn {...columnProps} firstSortDirection='desc' key={columnProps.property} />
410+
))}
411+
</DataTable>
412+
).call(this);
413+
414+
const thead = getTable(this.dom).querySelectorAll('thead')[0];
415+
const thirdColumn = thead.querySelectorAll('th')[2];
416+
const sortButton = thead.querySelectorAll('a')[0];
417+
418+
Simulate.click(sortButton, {});
419+
});
420+
399421
it('does not call onSort when a non-sortable column is clicked', function(done) {
400422
this.onSort = () => {
401423
done('sort called');

components/data-table/private/header-cell.jsx

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ import {
2525
DATA_TABLE_COLUMN,
2626
} from '../../../utilities/constants';
2727

28+
const defaultProps = {
29+
firstSortDirection: 'asc'
30+
};
31+
2832
/**
2933
* Used internally, renders each individual column heading.
3034
*/
@@ -70,12 +74,18 @@ class DataTableHeaderCell extends React.Component {
7074
* The current sort direction.
7175
*/
7276
sortDirection: PropTypes.oneOf(['desc', 'asc']),
77+
/**
78+
* The default sort direction for the first time if the column is not sorted and sortDirection not given
79+
*/
80+
firstSortDirection: PropTypes.oneOf(['asc', 'desc']),
7381
/**
7482
* Width of column. This is required for advanced/fixed layout tables. Please provide units. (`rems` are recommended)
7583
*/
7684
width: PropTypes.string,
7785
};
7886

87+
static defaultProps = defaultProps;
88+
7989
state = {
8090
sortDirection: null,
8191
};
@@ -94,7 +104,11 @@ class DataTableHeaderCell extends React.Component {
94104
handleSort = (e) => {
95105
const oldSortDirection =
96106
this.props.sortDirection || this.state.sortDirection;
97-
const sortDirection = oldSortDirection === 'asc' ? 'desc' : 'asc';
107+
// var sortDirection = this.props.firstSortDirection
108+
// if (oldSortDirection) {
109+
// sortDirection = oldSortDirection === 'asc' ? 'desc' : 'asc'
110+
// }
111+
const sortDirection = oldSortDirection ? (oldSortDirection === 'asc' ? 'desc' : 'asc') : (this.props.firstSortDirection)
98112
const data = {
99113
property: this.props.property,
100114
sortDirection,
@@ -114,7 +128,7 @@ class DataTableHeaderCell extends React.Component {
114128
const { fixedHeader, isSorted, label, sortable, width } = this.props;
115129

116130
const labelType = typeof label;
117-
const sortDirection = this.props.sortDirection || this.state.sortDirection;
131+
const sortDirection = (!this.props.sortDirection && !this.state.sortDirection) ? this.props.firstSortDirection : (this.props.sortDirection || this.state.sortDirection);
118132
const expandedSortDirection =
119133
sortDirection === 'desc' ? 'descending' : 'ascending';
120134
const ariaSort = isSorted ? expandedSortDirection : 'none';
@@ -144,7 +158,7 @@ class DataTableHeaderCell extends React.Component {
144158
name={sortDirection === 'desc' ? 'arrowdown' : 'arrowup'}
145159
size="x-small"
146160
/>
147-
{sortDirection ? (
161+
{(sortDirection && this.state.sortable) ? (
148162
<span
149163
className="slds-assistive-text"
150164
aria-live="assertive"

0 commit comments

Comments
 (0)