-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
166 additions
and
34 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -1,15 +1,86 @@ | ||
import React, {Component} from 'react' | ||
import {render} from 'react-dom' | ||
import React from 'react'; | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import { render } from 'react-dom'; | ||
|
||
import Example from '../../src' | ||
import Sortable from '../../src'; | ||
|
||
class Demo extends Component { | ||
render() { | ||
return <div> | ||
<h1>react-sort-data Demo</h1> | ||
<Example/> | ||
</div> | ||
} | ||
} | ||
const characters = [ | ||
{ name: 'Captain Scarlet', series: 'Captain Scarlet and the Mysterons', year: '1967' }, | ||
{ name: 'Captain Black', series: 'Captain Scarlet and the Mysterons', year: '1967' }, | ||
{ name: 'Brains', series: 'Thunderbirds', year: '1965' }, | ||
{ name: 'Lady Penelope', series: 'Thunderbirds', year: '1965' }, | ||
{ name: 'Scott Tracy', series: 'Thunderbirds', year: '1965' }, | ||
{ name: 'Joe McLaine', series: 'Joe 90', year: '1968' }, | ||
]; | ||
|
||
render(<Demo/>, document.querySelector('#demo')) | ||
const TableExample = () => { | ||
const fields = [ | ||
{ key: 'name', default: true }, | ||
{ key: 'series' }, | ||
{ key: 'year', reversed: true }, | ||
]; | ||
|
||
return ( | ||
<Sortable data={characters} fields={fields}> | ||
{ | ||
({ | ||
data, reversed, setSortField, sortField, | ||
}) => ( | ||
<table> | ||
<thead> | ||
<tr> | ||
{fields.map(({ key }) => { | ||
const selected = (sortField === key); | ||
return ( | ||
<th key={key} onClick={() => setSortField(key)}> | ||
{selected && '> '}{key} {selected && (reversed ? '^' : 'v')} | ||
</th> | ||
); | ||
})} | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{data.map(row => ( | ||
<tr key={row.name}> | ||
<td>{row.name}</td> | ||
<td>{row.series}</td> | ||
<td>{row.year}</td> | ||
</tr> | ||
))} | ||
</tbody> | ||
</table> | ||
) | ||
} | ||
</Sortable> | ||
); | ||
}; | ||
|
||
const ULExample = () => ( | ||
<Sortable | ||
data={characters.map(character => character.name)} | ||
defaultSort={reversed => (a, b) => (reversed ? -1 : 1) * a.localeCompare(b)} | ||
render={({ data, setSortField }) => ( | ||
<div> | ||
<ul> | ||
{data.map(character => <li key={character}>{character}</li>)} | ||
</ul> | ||
<button onClick={() => setSortField()}>Reverse</button> | ||
</div> | ||
)} | ||
/> | ||
); | ||
|
||
const Demo = () => ( | ||
<div> | ||
<h1>react-sort-data Demo</h1> | ||
|
||
<h2>Example using a Table-based display, rendering via children</h2> | ||
<TableExample /> | ||
|
||
<h2>Example using UL-based display, an alternative sorting method, and render prop</h2> | ||
<ULExample /> | ||
</div> | ||
); | ||
|
||
// eslint-disable-next-line no-undef | ||
render(<Demo />, document.querySelector('#demo')); |
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
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