11import _ from 'lodash'
2- import React , { Component } from 'react'
2+ import React from 'react'
33import { Table } from 'semantic-ui-react'
44
55const tableData = [
@@ -9,69 +9,71 @@ const tableData = [
99 { name : 'Ben' , age : 70 , gender : 'Male' } ,
1010]
1111
12- export default class TableExampleSortable extends Component {
13- state = {
14- column : null ,
15- data : tableData ,
16- direction : null ,
17- }
12+ function exampleReducer ( state , action ) {
13+ switch ( action . type ) {
14+ case 'CHANGE_SORT' :
15+ if ( state . column === action . column ) {
16+ return {
17+ ...state ,
18+ data : state . data . reverse ( ) ,
19+ direction :
20+ state . direction === 'ascending' ? 'descending' : 'ascending' ,
21+ }
22+ }
1823
19- handleSort = ( clickedColumn ) => ( ) => {
20- const { column, data, direction } = this . state
21-
22- if ( column !== clickedColumn ) {
23- this . setState ( {
24- column : clickedColumn ,
25- data : _ . sortBy ( data , [ clickedColumn ] ) ,
24+ return {
25+ column : action . column ,
26+ data : _ . sortBy ( state . data , [ action . column ] ) ,
2627 direction : 'ascending' ,
27- } )
28-
29- return
30- }
31-
32- this . setState ( {
33- data : data . reverse ( ) ,
34- direction : direction === 'ascending' ? 'descending' : 'ascending' ,
35- } )
28+ }
29+ default :
30+ throw new Error ( )
3631 }
32+ }
3733
38- render ( ) {
39- const { column, data, direction } = this . state
34+ function TableExampleSortable ( ) {
35+ const [ state , dispatch ] = React . useReducer ( exampleReducer , {
36+ column : null ,
37+ data : tableData ,
38+ direction : null ,
39+ } )
40+ const { column, data, direction } = state
4041
41- return (
42- < Table sortable celled fixed >
43- < Table . Header >
44- < Table . Row >
45- < Table . HeaderCell
46- sorted = { column === 'name' ? direction : null }
47- onClick = { this . handleSort ( 'name' ) }
48- >
49- Name
50- </ Table . HeaderCell >
51- < Table . HeaderCell
52- sorted = { column === 'age' ? direction : null }
53- onClick = { this . handleSort ( 'age' ) }
54- >
55- Age
56- </ Table . HeaderCell >
57- < Table . HeaderCell
58- sorted = { column === 'gender' ? direction : null }
59- onClick = { this . handleSort ( 'gender' ) }
60- >
61- Gender
62- </ Table . HeaderCell >
42+ return (
43+ < Table sortable celled fixed >
44+ < Table . Header >
45+ < Table . Row >
46+ < Table . HeaderCell
47+ sorted = { column === 'name' ? direction : null }
48+ onClick = { ( ) => dispatch ( { type : 'CHANGE_SORT' , column : 'name' } ) }
49+ >
50+ Name
51+ </ Table . HeaderCell >
52+ < Table . HeaderCell
53+ sorted = { column === 'age' ? direction : null }
54+ onClick = { ( ) => dispatch ( { type : 'CHANGE_SORT' , column : 'age' } ) }
55+ >
56+ Age
57+ </ Table . HeaderCell >
58+ < Table . HeaderCell
59+ sorted = { column === 'gender' ? direction : null }
60+ onClick = { ( ) => dispatch ( { type : 'CHANGE_SORT' , column : 'gender' } ) }
61+ >
62+ Gender
63+ </ Table . HeaderCell >
64+ </ Table . Row >
65+ </ Table . Header >
66+ < Table . Body >
67+ { data . map ( ( { age, gender, name } ) => (
68+ < Table . Row key = { name } >
69+ < Table . Cell > { name } </ Table . Cell >
70+ < Table . Cell > { age } </ Table . Cell >
71+ < Table . Cell > { gender } </ Table . Cell >
6372 </ Table . Row >
64- </ Table . Header >
65- < Table . Body >
66- { _ . map ( data , ( { age, gender, name } ) => (
67- < Table . Row key = { name } >
68- < Table . Cell > { name } </ Table . Cell >
69- < Table . Cell > { age } </ Table . Cell >
70- < Table . Cell > { gender } </ Table . Cell >
71- </ Table . Row >
72- ) ) }
73- </ Table . Body >
74- </ Table >
75- )
76- }
73+ ) ) }
74+ </ Table . Body >
75+ </ Table >
76+ )
7777}
78+
79+ export default TableExampleSortable
0 commit comments