11import React from 'react'
22import PropTypes from 'prop-types'
3+ import matchSorter from 'match-sorter'
34import { calculatePages , sortData , paginateData } from './utils'
5+
46class DataSort extends React . Component {
57 static propTypes = {
68 data : PropTypes . array . isRequired ,
@@ -11,7 +13,9 @@ class DataSort extends React.Component {
1113 itemsPerPage : PropTypes . number ,
1214 activePage : PropTypes . number ,
1315 defaultSortBy : PropTypes . string ,
14- defaultDirection : PropTypes . string
16+ defaultDirection : PropTypes . string ,
17+ searchQuery : PropTypes . string ,
18+ searchInKeys : PropTypes . array
1519 }
1620 static defaultProps = {
1721 data : [ ] ,
@@ -23,7 +27,8 @@ class DataSort extends React.Component {
2327 direction : this . props . defaultDirection || 'asc' ,
2428 pages : null ,
2529 activePage : this . props . defaultActivePage || 0 ,
26- data : [ ]
30+ data : [ ] ,
31+ searchQuery : ''
2732 }
2833 componentDidMount ( ) {
2934 const { itemsPerPage, paginate, data } = this . props
@@ -40,6 +45,9 @@ class DataSort extends React.Component {
4045 isDirectionControlled ( ) {
4146 return typeof this . props . direction !== 'undefined'
4247 }
48+ isSearchControlled ( ) {
49+ return typeof this . props . searchQuery !== 'undefined'
50+ }
4351 reset = ( ) => {
4452 this . setState ( {
4553 sortBy : null ,
@@ -90,13 +98,22 @@ class DataSort extends React.Component {
9098 direction : this . state . direction === 'asc' ? 'desc' : 'asc'
9199 } )
92100 }
101+ search = value => {
102+ this . setState ( { searchQuery : value } )
103+ }
93104 render ( ) {
94105 const { render, paginate, itemsPerPage, data } = this . props
95106 const { activePage } = this . isPaginationControlled ( ) ? this . props : this . state
96107 const { sortBy } = this . isSortByControlled ( ) ? this . props : this . state
97108 const { direction } = this . isDirectionControlled ( ) ? this . props : this . state
109+ const { searchQuery } = this . isSearchControlled ( ) ? this . props : this . state
98110 const { pages } = this . state
99- const sorted = sortBy === null ? data : sortData ( data , sortBy , direction )
111+ const keys = this . props . searchInKeys || Object . keys ( data [ 0 ] )
112+
113+ // Search & sort data
114+ const searched = searchQuery === '' ? data : matchSorter ( data , searchQuery , { keys } )
115+ const sorted = sortBy === null ? searched : sortData ( searched , sortBy , direction )
116+
100117 return typeof render !== 'function'
101118 ? null
102119 : render ( {
@@ -105,13 +122,15 @@ class DataSort extends React.Component {
105122 pages,
106123 sortBy,
107124 direction,
125+ searchQuery,
108126 toggleDirection : this . toggleDirection ,
109127 reset : this . reset ,
110128 prevPage : this . prevPage ,
111129 nextPage : this . nextPage ,
112130 goToPage : this . goToPage ,
113131 setDirection : this . setDirection ,
114- setSortBy : this . setSortBy
132+ setSortBy : this . setSortBy ,
133+ search : this . search
115134 } )
116135 }
117136}
0 commit comments