-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert Sparkline to typescript (#2347)
* Convert sparkline to typescript * Consistent type defs * better variable names
- Loading branch information
1 parent
8e7d4f6
commit 3d09422
Showing
4 changed files
with
195 additions
and
129 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 was deleted.
Oops, something went wrong.
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,135 @@ | ||
import { Meta, StoryObj } from '@storybook/react' | ||
|
||
import Sparkline, { SparklineProps } from './Sparkline' | ||
|
||
export default { | ||
title: 'Components/Sparkline', | ||
component: Sparkline, | ||
} as Meta | ||
|
||
const renderTemplate = (args: SparklineProps) => ( | ||
<div className="flex h-[50px] w-[50%]"> | ||
{/* Sparkline conforms to the width and height of it's parent. */} | ||
<Sparkline {...args} /> | ||
</div> | ||
) | ||
|
||
const renderManyTemplate = (args: SparklineProps) => { | ||
const range = 200 | ||
const largeDataSetWithReusedData = Array(50) | ||
.fill(0) | ||
.map(() => Math.random() * range) | ||
return ( | ||
<> | ||
{Array(50) | ||
.fill(0) | ||
.map((_, i) => { | ||
return ( | ||
<div key={`short-${i}`} className="flex h-[20px] w-[100%]"> | ||
<Sparkline {...args} datum={largeDataSetWithReusedData} /> | ||
</div> | ||
) | ||
})} | ||
</> | ||
) | ||
} | ||
|
||
type Story = StoryObj<typeof Sparkline> | ||
const range = 200 | ||
const createTestData = Array(20) | ||
.fill(0) | ||
.map(() => Math.random() * range - range / 2) | ||
|
||
export const NormalSparkline: Story = { | ||
args: { | ||
datum: createTestData, | ||
description: 'storybook sparkline', | ||
dataTemplate: (d) => `Foo ${d}%`, | ||
}, | ||
render: (args) => { | ||
return renderTemplate(args) | ||
}, | ||
} | ||
|
||
const createTestDataWithMissingValues = Array(30) | ||
.fill(0) | ||
.map(() => (Math.random() > 0.4 ? Math.random() * range - range / 2 : null)) | ||
|
||
export const SparklineWithMissingData: Story = { | ||
args: { | ||
datum: createTestDataWithMissingValues, | ||
description: 'storybook sparkline', | ||
dataTemplate: (d) => `Foo ${d}%`, | ||
}, | ||
render: (args) => { | ||
return renderTemplate(args) | ||
}, | ||
} | ||
|
||
const createTestDataWithMissingValuesBeginning = Array(7) | ||
.fill(0) | ||
.map((_, i) => (i > 2 ? Math.random() * range - range / 2 : null)) | ||
|
||
export const SparklineWithMissingDataBeginning: Story = { | ||
args: { | ||
datum: createTestDataWithMissingValuesBeginning, | ||
description: 'storybook sparkline', | ||
dataTemplate: (d) => `Foo ${d}%`, | ||
}, | ||
render: (args) => { | ||
return renderTemplate(args) | ||
}, | ||
} | ||
|
||
const createTestDataWithMissingValuesEnding = Array(20) | ||
.fill(0) | ||
.map((_, i) => (i < 18 ? Math.random() * range - range / 2 : null)) | ||
|
||
export const SparklineWithMissingDataEnding: Story = { | ||
args: { | ||
datum: createTestDataWithMissingValuesEnding, | ||
description: 'storybook sparkline', | ||
dataTemplate: (d) => `Foo ${d}%`, | ||
}, | ||
render: (args) => { | ||
return renderTemplate(args) | ||
}, | ||
} | ||
|
||
const createTestDataComplex = Array(10) | ||
.fill(0) | ||
.map((_) => ({ value: Math.random() * range - range / 2, foo: 'bar' })) | ||
|
||
export const SparklineWithComplexData: Story = { | ||
args: { | ||
datum: createTestDataComplex, | ||
select: (d) => d?.value, | ||
description: 'storybook sparkline', | ||
dataTemplate: (d) => `Foo ${d}%`, | ||
}, | ||
render: (args) => { | ||
return renderTemplate(args) | ||
}, | ||
} | ||
|
||
export const SparklineCustomLineWidth: Story = { | ||
args: { | ||
datum: createTestData, | ||
description: 'storybook sparkline', | ||
dataTemplate: (d) => `Foo ${d}%`, | ||
lineSize: 2, | ||
}, | ||
render: (args) => { | ||
return renderTemplate(args) | ||
}, | ||
} | ||
|
||
export const ManySparklines: Story = { | ||
args: { | ||
description: 'storybook sparkline', | ||
dataTemplate: (d) => `${d}%`, | ||
}, | ||
render: (args) => { | ||
return renderManyTemplate(args) | ||
}, | ||
} |
Oops, something went wrong.