-
Notifications
You must be signed in to change notification settings - Fork 5
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
1 parent
cedb081
commit 0c8fdaa
Showing
5 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
www/content/docs/learn/examples/using-sparklines-example.page.md
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,46 @@ | ||
--- | ||
title: Sparklines Example | ||
--- | ||
|
||
This example shows how to use integrate a sparkline component in a DataGrid column. | ||
|
||
For this demo, we're using the [`react-sparklines`](https://www.npmjs.com/package/react-sparklines) library. | ||
|
||
The most important part is the <PropLink name="columns.renderValue" /> property, which allows you to render a custom React component for the cell value. | ||
|
||
```tsx {11-23} title="Using column.renderValue to render a sparkline" | ||
const columns = { | ||
// ... other columns | ||
id: { | ||
field: 'id', | ||
defaultWidth: 100, | ||
}, | ||
bugFixes: { | ||
field: 'bugFixes', | ||
header: 'Bug Fixes', | ||
defaultWidth: 300, | ||
renderValue: ({ value }) => { | ||
return ( | ||
<Sparklines | ||
data={value} | ||
style={{ | ||
width: '100%', | ||
}} | ||
height={30} | ||
> | ||
<SparklinesLine color="#253e56" /> | ||
</Sparklines> | ||
); | ||
}, | ||
}, | ||
} | ||
``` | ||
|
||
|
||
<Sandpack size="md" viewMode="preview" deps="react-sparklines" title="Using a sparkline component"> | ||
|
||
```tsx file="./using-sparklines-example.page.tsx" | ||
|
||
``` | ||
|
||
</Sandpack> |
96 changes: 96 additions & 0 deletions
96
www/content/docs/learn/examples/using-sparklines-example.page.tsx
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,96 @@ | ||
import { | ||
InfiniteTable, | ||
DataSource, | ||
InfiniteTableColumn, | ||
} from '@infinite-table/infinite-react'; | ||
import { useMemo, useState } from 'react'; | ||
import { Sparklines, SparklinesLine } from 'react-sparklines'; | ||
|
||
export type Employee = { | ||
id: number; | ||
companyName: string; | ||
companySize: string; | ||
firstName: string; | ||
lastName: string; | ||
country: string; | ||
countryCode: string; | ||
city: string; | ||
bugFixes: number[]; | ||
streetName: string; | ||
streetPrefix: string; | ||
streetNo: string; | ||
department: string; | ||
team: string; | ||
salary: number; | ||
currency: number; | ||
age: number; | ||
email: string; | ||
}; | ||
export const columns: Record<string, InfiniteTableColumn<Employee>> = { | ||
firstName: { | ||
field: 'firstName', | ||
header: 'First Name', | ||
}, | ||
country: { | ||
field: 'country', | ||
header: 'Country', | ||
columnGroup: 'location', | ||
}, | ||
bugFixes: { | ||
field: 'bugFixes', | ||
header: 'Bug Fixes', | ||
defaultWidth: 300, | ||
renderValue: ({ value }) => { | ||
return ( | ||
<Sparklines | ||
data={value} | ||
style={{ | ||
width: '100%', | ||
}} | ||
height={30} | ||
> | ||
<SparklinesLine color="#253e56" /> | ||
</Sparklines> | ||
); | ||
}, | ||
}, | ||
city: { | ||
field: 'city', | ||
header: 'City', | ||
columnGroup: 'address', | ||
}, | ||
salary: { | ||
field: 'salary', | ||
type: 'number', | ||
header: 'Salary', | ||
}, | ||
department: { | ||
field: 'department', | ||
header: 'Department', | ||
}, | ||
team: { | ||
field: 'team', | ||
header: 'Team', | ||
}, | ||
}; | ||
|
||
const dataSource = () => { | ||
return fetch(process.env.NEXT_PUBLIC_BASE_URL + '/employees10k') | ||
.then((r) => r.json()) | ||
.then((data: Employee[]) => { | ||
return data.map((employee) => { | ||
return { | ||
...employee, | ||
bugFixes: [...Array(10)].map(() => Math.round(Math.random() * 100)), | ||
}; | ||
}); | ||
}); | ||
}; | ||
|
||
export default function App() { | ||
return ( | ||
<DataSource<Employee> data={dataSource} primaryKey="id"> | ||
<InfiniteTable<Employee> columns={columns} columnDefaultWidth={150} /> | ||
</DataSource> | ||
); | ||
} |
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
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