-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(Table): add sortable table example (#1594)
* add example of sortable table * style(Table): style fixes to example * docs(TableExampleSortable): add fixed prop
- Loading branch information
1 parent
c8040bf
commit 40924fb
Showing
2 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
docs/app/Examples/collections/Table/Variations/TableExampleSortable.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import _ from 'lodash' | ||
import React, { Component } from 'react' | ||
import { Table } from 'semantic-ui-react' | ||
|
||
const tableData = [ | ||
{ name: 'John', age: 15, gender: 'Male' }, | ||
{ name: 'Amber', age: 40, gender: 'Female' }, | ||
{ name: 'Leslie', age: 25, gender: 'Female' }, | ||
{ name: 'Ben', age: 70, gender: 'Male' }, | ||
] | ||
|
||
export default class TableExampleSortable extends Component { | ||
state = { | ||
column: null, | ||
data: tableData, | ||
direction: null, | ||
} | ||
|
||
handleSort = clickedColumn => () => { | ||
const { column, data, direction } = this.state | ||
|
||
if (column !== clickedColumn) { | ||
this.setState({ | ||
column: clickedColumn, | ||
data: _.sortBy(data, [clickedColumn]), | ||
direction: 'ascending', | ||
}) | ||
|
||
return | ||
} | ||
|
||
this.setState({ | ||
data: data.reverse(), | ||
direction: direction === 'ascending' ? 'descending' : 'ascending', | ||
}) | ||
} | ||
|
||
render() { | ||
const { column, data, direction } = this.state | ||
|
||
return ( | ||
<Table sortable celled fixed> | ||
<Table.Header> | ||
<Table.Row> | ||
<Table.HeaderCell sorted={column === 'name' && direction} onClick={this.handleSort('name')}> | ||
Name | ||
</Table.HeaderCell> | ||
<Table.HeaderCell sorted={column === 'age' && direction} onClick={this.handleSort('age')}> | ||
Age | ||
</Table.HeaderCell> | ||
<Table.HeaderCell sorted={column === 'gender' && direction} onClick={this.handleSort('gender')}> | ||
Gender | ||
</Table.HeaderCell> | ||
</Table.Row> | ||
</Table.Header> | ||
<Table.Body> | ||
{_.map(data, ({ age, gender, name }) => ( | ||
<Table.Row key={name}> | ||
<Table.Cell>{name}</Table.Cell> | ||
<Table.Cell>{age}</Table.Cell> | ||
<Table.Cell>{gender}</Table.Cell> | ||
</Table.Row> | ||
))} | ||
</Table.Body> | ||
</Table> | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters