Skip to content

Commit

Permalink
docs(@clayui/table): adjust table sorting example
Browse files Browse the repository at this point in the history
  • Loading branch information
matuzalemsteles committed Feb 22, 2025
1 parent 539bc70 commit 534b60f
Showing 1 changed file with 38 additions and 17 deletions.
55 changes: 38 additions & 17 deletions packages/clay-core/docs/table.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -238,16 +238,21 @@ It is also possible to implement your own logic on the client side when your dat
</div>

```jsx preview
export function App() {
const [sort, setSort] = (useState < Sorting) | (null > null);
import React, {useMemo, useState} from 'react';
import {Body, Cell, Text, Head, Row, Table, Provider} from '@clayui/core';

import '@clayui/css/lib/css/atlas.css';

export default function App() {
const [sort, setSort] = useState(null);
const [items, setItems] = useState([
{files: 22, id: 1, name: 'Games', type: 'File folder'},
{files: 7, id: 2, name: 'Program Files', type: 'File folder'},
]);

const filteredItems = useMemo(() => {
if (!sort) {
return;
return items;
}

return items.sort((a, b) => {
Expand All @@ -265,21 +270,37 @@ export function App() {
}, [sort, items]);

return (
<Table onSortChange={setSort} sort={sort}>
<Head>
<Cell key="name" sortable>
Name
</Cell>
<Cell key="files" sortable>
Files
</Cell>
<Cell key="type" sortable>
Type
</Cell>
</Head>
<Provider spritemap="/public/icons.svg">
<div className="p-4">
<Table onSortChange={setSort} sort={sort}>
<Head>
<Cell key="name" sortable>
Name
</Cell>
<Cell key="files" sortable>
Files
</Cell>
<Cell key="type" sortable>
Type
</Cell>
</Head>

<Body defaultItems={filteredItems}>...</Body>
</Table>
<Body defaultItems={filteredItems}>
{(row) => (
<Row>
<Cell>
<Text size={3} weight="semi-bold">
{row['name']}
</Text>
</Cell>
<Cell>{row['files']}</Cell>
<Cell>{row['type']}</Cell>
</Row>
)}
</Body>
</Table>
</div>
</Provider>
);
}
```
Expand Down

0 comments on commit 534b60f

Please sign in to comment.