react-inclusive-sortable-table
Advanced tools
Comparing version 0.1.7 to 0.1.8
@@ -259,2 +259,3 @@ import React, { Component } from 'react'; | ||
_this.sortBy = _this.sortBy.bind(_this); | ||
_this.sort = _this.sort.bind(_this); | ||
_this.captionID = 'caption-' + Math.random().toString(36).substr(2, 9); | ||
@@ -265,10 +266,27 @@ return _this; | ||
createClass(Table, [{ | ||
key: 'sort', | ||
value: function sort(a, b, sortDir, i) { | ||
if (sortDir === 'ascending') { | ||
return a[i] > b[i] ? 1 : a[i] < b[i] ? -1 : 0; | ||
} else { | ||
return a[i] < b[i] ? 1 : a[i] > b[i] ? -1 : 0; | ||
} | ||
} | ||
}, { | ||
key: 'sortBy', | ||
value: function sortBy(i) { | ||
var sortDir = void 0; | ||
var ascending = this.state.sortDir === 'ascending'; | ||
if (i === this.state.sortedBy) { | ||
sortDir = !ascending ? 'ascending' : 'descending'; | ||
var _this2 = this; | ||
var customSort = this.props.customSort; | ||
var _state = this.state, | ||
sortDir = _state.sortDir, | ||
sortedBy = _state.sortedBy; | ||
var sortDirection = void 0; | ||
var ascending = sortDir === 'ascending'; | ||
if (i === sortedBy) { | ||
sortDirection = !ascending ? 'ascending' : 'descending'; | ||
} else { | ||
sortDir = 'ascending'; | ||
sortDirection = 'ascending'; | ||
} | ||
@@ -278,7 +296,8 @@ this.setState(function (prevState) { | ||
rows: prevState.rows.slice(0).sort(function (a, b) { | ||
if (sortDir === 'ascending') { | ||
return a[i] > b[i] ? 1 : a[i] < b[i] ? -1 : 0; | ||
} else { | ||
return a[i] < b[i] ? 1 : a[i] > b[i] ? -1 : 0; | ||
} | ||
return ( | ||
// If a custom sort method is provided for this column, use it. | ||
customSort[i] && customSort[i](a, b, sortDirection, i) || | ||
// Otherwise use the default sort. | ||
_this2.sort(a, b, sortDirection, i) | ||
); | ||
}), | ||
@@ -305,3 +324,3 @@ sortedBy: i, | ||
value: function render() { | ||
var _this2 = this; | ||
var _this3 = this; | ||
@@ -359,5 +378,5 @@ var _props = this.props, | ||
sortable: sortable, | ||
sortBy: _this2.sortBy, | ||
sortDir: _this2.state.sortDir, | ||
sortedBy: _this2.state.sortedBy, | ||
sortBy: _this3.sortBy, | ||
sortDir: _this3.state.sortDir, | ||
sortedBy: _this3.state.sortedBy, | ||
customArrow: customArrow | ||
@@ -398,2 +417,3 @@ }); | ||
customArrow: PropTypes.func, | ||
customSort: PropTypes.object, | ||
headers: PropTypes.array.isRequired, | ||
@@ -400,0 +420,0 @@ rowHeaders: PropTypes.bool, |
@@ -264,2 +264,3 @@ 'use strict'; | ||
_this.sortBy = _this.sortBy.bind(_this); | ||
_this.sort = _this.sort.bind(_this); | ||
_this.captionID = 'caption-' + Math.random().toString(36).substr(2, 9); | ||
@@ -270,10 +271,27 @@ return _this; | ||
createClass(Table, [{ | ||
key: 'sort', | ||
value: function sort(a, b, sortDir, i) { | ||
if (sortDir === 'ascending') { | ||
return a[i] > b[i] ? 1 : a[i] < b[i] ? -1 : 0; | ||
} else { | ||
return a[i] < b[i] ? 1 : a[i] > b[i] ? -1 : 0; | ||
} | ||
} | ||
}, { | ||
key: 'sortBy', | ||
value: function sortBy(i) { | ||
var sortDir = void 0; | ||
var ascending = this.state.sortDir === 'ascending'; | ||
if (i === this.state.sortedBy) { | ||
sortDir = !ascending ? 'ascending' : 'descending'; | ||
var _this2 = this; | ||
var customSort = this.props.customSort; | ||
var _state = this.state, | ||
sortDir = _state.sortDir, | ||
sortedBy = _state.sortedBy; | ||
var sortDirection = void 0; | ||
var ascending = sortDir === 'ascending'; | ||
if (i === sortedBy) { | ||
sortDirection = !ascending ? 'ascending' : 'descending'; | ||
} else { | ||
sortDir = 'ascending'; | ||
sortDirection = 'ascending'; | ||
} | ||
@@ -283,7 +301,8 @@ this.setState(function (prevState) { | ||
rows: prevState.rows.slice(0).sort(function (a, b) { | ||
if (sortDir === 'ascending') { | ||
return a[i] > b[i] ? 1 : a[i] < b[i] ? -1 : 0; | ||
} else { | ||
return a[i] < b[i] ? 1 : a[i] > b[i] ? -1 : 0; | ||
} | ||
return ( | ||
// If a custom sort method is provided for this column, use it. | ||
customSort[i] && customSort[i](a, b, sortDirection, i) || | ||
// Otherwise use the default sort. | ||
_this2.sort(a, b, sortDirection, i) | ||
); | ||
}), | ||
@@ -310,3 +329,3 @@ sortedBy: i, | ||
value: function render() { | ||
var _this2 = this; | ||
var _this3 = this; | ||
@@ -364,5 +383,5 @@ var _props = this.props, | ||
sortable: sortable, | ||
sortBy: _this2.sortBy, | ||
sortDir: _this2.state.sortDir, | ||
sortedBy: _this2.state.sortedBy, | ||
sortBy: _this3.sortBy, | ||
sortDir: _this3.state.sortDir, | ||
sortedBy: _this3.state.sortedBy, | ||
customArrow: customArrow | ||
@@ -403,2 +422,3 @@ }); | ||
customArrow: PropTypes.func, | ||
customSort: PropTypes.object, | ||
headers: PropTypes.array.isRequired, | ||
@@ -405,0 +425,0 @@ rowHeaders: PropTypes.bool, |
{ | ||
"name": "react-inclusive-sortable-table", | ||
"version": "0.1.7", | ||
"version": "0.1.8", | ||
"description": "An accessible, sortable table based off of Heydon Pickering's Sortable Table from Inclusive Components.", | ||
@@ -5,0 +5,0 @@ "keywords": ["accessibility", "a11y", "react", "inclusive components", "responsive", "table"], |
@@ -121,2 +121,19 @@ # react-inclusive-sortable-table | ||
### customSort | ||
This should take an object, with the keys of the object reflecting the index of the array you want to sort, and the value reflecting the method you want to pass to `Array.sort()`. | ||
Usage: | ||
```jsx | ||
<Table | ||
customSort={{ | ||
// This will pass a custom sort method to the 2nd column. | ||
1: (a, b, sortDirection, index) => { | ||
// do your sorting here | ||
} | ||
}} | ||
/> | ||
``` | ||
### headers | ||
@@ -196,3 +213,2 @@ | ||
- Customizable sorting methods | ||
@@ -199,0 +215,0 @@ ## License |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
61986
765
216