-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enforce stricter types for default interactions config
- Loading branch information
Showing
17 changed files
with
143 additions
and
99 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { DefaultInteractions, ResetZoomButton, VisCanvas } from '@h5web/lib'; | ||
import type { DefaultInteractionsProps } from '@h5web/lib/src/interactions/DefaultInteractions'; | ||
import type { Meta, Story } from '@storybook/react'; | ||
|
||
import FillHeight from './decorators/FillHeight'; | ||
|
||
const Template: Story<DefaultInteractionsProps> = (args) => { | ||
return ( | ||
<VisCanvas | ||
abscissaConfig={{ visDomain: [-10, 0], showGrid: true }} | ||
ordinateConfig={{ visDomain: [50, 100], showGrid: true }} | ||
> | ||
<DefaultInteractions {...args} /> | ||
<ResetZoomButton /> | ||
</VisCanvas> | ||
); | ||
}; | ||
|
||
export const Default = Template.bind({}); | ||
|
||
export default { | ||
title: 'Building Blocks/DefaultInteractions', | ||
component: DefaultInteractions, | ||
decorators: [FillHeight], | ||
parameters: { layout: 'fullscreen' }, | ||
argTypes: { | ||
keepRatio: { defaultValue: false }, | ||
pan: { defaultValue: {} }, | ||
zoom: { defaultValue: {} }, | ||
xAxisZoom: { defaultValue: { modifierKey: 'Alt' } }, | ||
yAxisZoom: { defaultValue: { modifierKey: 'Shift' } }, | ||
selectToZoom: { defaultValue: { modifierKey: 'Control' } }, | ||
xSelectToZoom: { defaultValue: { modifierKey: ['Control', 'Alt'] } }, | ||
ySelectToZoom: { defaultValue: { modifierKey: ['Control', 'Shift'] } }, | ||
}, | ||
} as Meta<DefaultInteractionsProps>; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,79 @@ | ||
import { merge } from 'lodash'; | ||
|
||
import Pan from '../interactions/Pan'; | ||
import type { PanProps } from '../interactions/Pan'; | ||
import SelectToZoom from '../interactions/SelectToZoom'; | ||
import type { SelectToZoomProps } from '../interactions/SelectToZoom'; | ||
import XAxisZoom from '../interactions/XAxisZoom'; | ||
import type { XAxisZoomProps } from '../interactions/XAxisZoom'; | ||
import YAxisZoom from '../interactions/YAxisZoom'; | ||
import type { YAxisZoomProps } from '../interactions/YAxisZoom'; | ||
import Zoom from '../interactions/Zoom'; | ||
import type { ZoomProps } from '../interactions/Zoom'; | ||
import AxialSelectToZoom from './AxialSelectToZoom'; | ||
import type { Interactions } from './models'; | ||
import { getDefaultInteractions } from './utils'; | ||
import type { AxialSelectToZoomProps } from './AxialSelectToZoom'; | ||
|
||
export interface DefaultInteractionsConfig { | ||
pan?: PanProps | false; | ||
zoom?: ZoomProps | false; | ||
xAxisZoom?: XAxisZoomProps | false; | ||
yAxisZoom?: YAxisZoomProps | false; | ||
selectToZoom?: Omit<SelectToZoomProps, 'keepRatio'> | false; | ||
xSelectToZoom?: Omit<AxialSelectToZoomProps, 'axis'> | false; | ||
ySelectToZoom?: Omit<AxialSelectToZoomProps, 'axis'> | false; | ||
} | ||
|
||
interface Props { | ||
interactions?: Interactions; | ||
interface Props extends DefaultInteractionsConfig { | ||
keepRatio?: boolean; | ||
} | ||
|
||
function DefaultInteractions(props: Props) { | ||
const { interactions: givenInteractions = {}, keepRatio } = props; | ||
|
||
const parsedInteractions = Object.fromEntries( | ||
Object.entries(givenInteractions).map(([k, v]) => { | ||
if (v === true) { | ||
return [k, {}]; | ||
} | ||
|
||
if (v === false) { | ||
return [k, null]; | ||
} | ||
|
||
return [k, v]; | ||
}) | ||
); | ||
|
||
const interactions = merge( | ||
getDefaultInteractions(keepRatio), | ||
parsedInteractions | ||
); | ||
const { keepRatio, ...interactions } = props; | ||
|
||
return ( | ||
<> | ||
{interactions.Pan && <Pan {...interactions.Pan} />} | ||
{interactions.Zoom && <Zoom {...interactions.Zoom} />} | ||
{interactions.XAxisZoom && <XAxisZoom {...interactions.XAxisZoom} />} | ||
{interactions.YAxisZoom && <YAxisZoom {...interactions.YAxisZoom} />} | ||
{interactions.SelectToZoom && ( | ||
<SelectToZoom {...interactions.SelectToZoom} keepRatio={keepRatio} /> | ||
{interactions.pan !== false && <Pan {...interactions.pan} />} | ||
{interactions.zoom !== false && <Zoom {...interactions.zoom} />} | ||
|
||
{interactions.xAxisZoom !== false && ( | ||
<XAxisZoom | ||
modifierKey="Alt" | ||
disabled={keepRatio} | ||
{...interactions.xAxisZoom} | ||
/> | ||
)} | ||
{interactions.yAxisZoom !== false && ( | ||
<YAxisZoom | ||
modifierKey="Shift" | ||
disabled={keepRatio} | ||
{...interactions.yAxisZoom} | ||
/> | ||
)} | ||
|
||
{interactions.selectToZoom !== false && ( | ||
<SelectToZoom | ||
keepRatio={keepRatio} | ||
modifierKey="Control" | ||
{...interactions.selectToZoom} | ||
/> | ||
)} | ||
{interactions.XSelectToZoom && ( | ||
<AxialSelectToZoom axis="x" {...interactions.XSelectToZoom} /> | ||
{interactions.xSelectToZoom !== false && ( | ||
<AxialSelectToZoom | ||
axis="x" | ||
modifierKey={['Control', 'Alt']} | ||
disabled={keepRatio} | ||
{...interactions.xSelectToZoom} | ||
/> | ||
)} | ||
{interactions.YSelectToZoom && ( | ||
<AxialSelectToZoom axis="y" {...interactions.YSelectToZoom} /> | ||
{interactions.ySelectToZoom !== false && ( | ||
<AxialSelectToZoom | ||
axis="y" | ||
modifierKey={['Control', 'Shift']} | ||
disabled={keepRatio} | ||
{...interactions.ySelectToZoom} | ||
/> | ||
)} | ||
</> | ||
); | ||
} | ||
|
||
export type { Props as DefaultInteractionsProps }; | ||
export default DefaultInteractions; |
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
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
Oops, something went wrong.