forked from apache/superset
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(demo): add helpers for resizable chart demo (apache#557)
- Loading branch information
1 parent
e9595d3
commit 5d117af
Showing
8 changed files
with
139 additions
and
42 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
45 changes: 45 additions & 0 deletions
45
.../superset-ui/packages/superset-ui-demo/storybook/shared/components/ResizableChartDemo.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,45 @@ | ||
import styled from '@superset-ui/style'; | ||
import React, { useState, ReactNode } from 'react'; | ||
import { DecoratorFunction } from '@storybook/addons'; | ||
import ResizablePanel, { Size } from './ResizablePanel'; | ||
|
||
export const SupersetBody = styled.div` | ||
background: #f5f5f5; | ||
padding: 16px; | ||
min-height: 100%; | ||
.panel { | ||
margin-bottom: 0; | ||
} | ||
`; | ||
|
||
export default function ResizableChartDemo({ | ||
children, | ||
panelPadding = 30, | ||
initialSize = { width: 500, height: 300 }, | ||
}: { | ||
children: (innerSize: Size) => ReactNode; | ||
panelPadding?: number; | ||
initialSize?: Size; | ||
}) { | ||
// size are all inner size | ||
const [size, setSize] = useState(initialSize); | ||
return ( | ||
<SupersetBody> | ||
<ResizablePanel initialSize={initialSize} onResize={(e, data) => setSize(data.size)}> | ||
{children({ width: size.width - panelPadding, height: size.height - panelPadding })} | ||
</ResizablePanel> | ||
</SupersetBody> | ||
); | ||
} | ||
|
||
export const withResizableChartDemo: DecoratorFunction<ReactNode> = (storyFn, context) => { | ||
const { | ||
parameters: { initialSize, panelPadding }, | ||
} = context; | ||
return ( | ||
<ResizableChartDemo initialSize={initialSize as Size | undefined} panelPadding={panelPadding}> | ||
{innerSize => storyFn({ ...context, ...innerSize })} | ||
</ResizableChartDemo> | ||
); | ||
}; |
40 changes: 40 additions & 0 deletions
40
...t_ui/superset-ui/packages/superset-ui-demo/storybook/shared/components/ResizablePanel.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,40 @@ | ||
import React, { PropsWithChildren, ReactNode } from 'react'; | ||
import { ResizableBox, ResizableBoxProps, ResizeCallbackData } from 'react-resizable'; | ||
|
||
import 'react-resizable/css/styles.css'; | ||
|
||
export type Size = ResizeCallbackData['size']; | ||
|
||
export default function ResizablePanel({ | ||
children, | ||
heading = undefined, | ||
initialSize = { width: 500, height: 300 }, | ||
minConstraints = [100, 100] as [number, number], | ||
onResize, | ||
...props | ||
}: PropsWithChildren<Omit<ResizableBoxProps, 'width' | 'height'>> & { | ||
heading?: ReactNode; | ||
initialSize?: Size; | ||
}) { | ||
const { width, height } = initialSize; | ||
return ( | ||
<ResizableBox | ||
className="panel" | ||
width={width} | ||
height={height} | ||
minConstraints={minConstraints} | ||
onResize={ | ||
onResize | ||
? (e, data) => { | ||
const { size } = data; | ||
onResize(e, { ...data, size }); | ||
} | ||
: undefined | ||
} | ||
{...props} | ||
> | ||
{heading ? <div className="panel-heading">{heading}</div> : null} | ||
<div className="panel-body">{children}</div> | ||
</ResizableBox> | ||
); | ||
} |
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
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