Skip to content

Commit 15f3887

Browse files
authored
Merge pull request #1 from sethu1504/first-time-sort-direction-data-tables
First time sort direction data tables
2 parents 15237cb + b198a34 commit 15f3887

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ describe('DataTable: ', function() {
374374
describe('Sortable', function() {
375375
afterEach(removeTable);
376376

377-
it('first clicked on sortable column header should result in ascending sort', function(done) {
377+
it('first clicked on sortable column header should result in ascending sort by default', function(done) {
378378
this.onSort = (data) => {
379379
data.property.should.equal('count');
380380
data.sortDirection.should.equal('asc');
@@ -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/column.jsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ DataTableColumn.propTypes = {
7878
* Width of column. This is required for advanced/fixed layout tables. Please provide units. (`rems` are recommended)
7979
*/
8080
width: PropTypes.string,
81+
/**
82+
* The default sort direction for the first time if the column is not sorted and sortDirection not given
83+
*/
84+
firstSortDirection: PropTypes.oneOf(['asc', 'desc'])
8185
};
8286

8387
export default DataTableColumn;

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

Lines changed: 16 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,10 @@ 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+
}
98111
const data = {
99112
property: this.props.property,
100113
sortDirection,
@@ -114,7 +127,7 @@ class DataTableHeaderCell extends React.Component {
114127
const { fixedHeader, isSorted, label, sortable, width } = this.props;
115128

116129
const labelType = typeof label;
117-
const sortDirection = this.props.sortDirection || this.state.sortDirection;
130+
const sortDirection = (!this.props.sortDirection && !this.state.sortDirection) ? this.props.firstSortDirection : (this.props.sortDirection || this.state.sortDirection);
118131
const expandedSortDirection =
119132
sortDirection === 'desc' ? 'descending' : 'ascending';
120133
const ariaSort = isSorted ? expandedSortDirection : 'none';
@@ -144,7 +157,7 @@ class DataTableHeaderCell extends React.Component {
144157
name={sortDirection === 'desc' ? 'arrowdown' : 'arrowup'}
145158
size="x-small"
146159
/>
147-
{sortDirection ? (
160+
{(sortDirection && this.state.sortable) ? (
148161
<span
149162
className="slds-assistive-text"
150163
aria-live="assertive"

0 commit comments

Comments
 (0)