Skip to content

Commit

Permalink
chore: use single, not double clicks for SizedPlot (#484)
Browse files Browse the repository at this point in the history
* feat: add singleClick to interactionHandlers, and make the SizedPlot respond to single; not doubleclicks

* chore: fix test

* chore: updating version in package.json
  • Loading branch information
jrenee42 authored Mar 3, 2021
1 parent 370281a commit a9fa73f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
2 changes: 1 addition & 1 deletion giraffe/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@influxdata/giraffe",
"version": "2.3.0",
"version": "2.4.0",
"main": "dist/index.js",
"module": "src/index.js",
"license": "MIT",
Expand Down
21 changes: 14 additions & 7 deletions giraffe/src/components/SizedPlot.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,22 @@ describe('the SizedPlot', () => {
layerCanvasRef={layersRef}
/>
)
fireEvent.doubleClick(screen.getByTestId('giraffe-inner-plot'))

// when the user (for real) does a single click, then a mouse up happens.
// chose mouse up because the single click listener wasn't triggering except on
// double clicks
fireEvent.mouseUp(screen.getByTestId('giraffe-inner-plot'))

expect(resetSpy).toHaveBeenCalled()
})
})

describe('the ability to override default behavior', () => {
it('handles double clicks', () => {
const fakeDoubleClickInteractionHandler = jest.fn()
it('handles single clicks', () => {
const fakeSingleClickInteractionHandler = jest.fn()
const localConfig = {
...config,
interactionHandlers: {doubleClick: fakeDoubleClickInteractionHandler},
interactionHandlers: {singleClick: fakeSingleClickInteractionHandler},
}

render(
Expand All @@ -78,14 +82,17 @@ describe('the SizedPlot', () => {
layerCanvasRef={layersRef}
/>
)
fireEvent.doubleClick(screen.getByTestId('giraffe-inner-plot'))
// when the user (for real) does a single click, then a mouse up happens.
// chose mouse up because the single click listener wasn't triggering except on
// double clicks
fireEvent.mouseUp(screen.getByTestId('giraffe-inner-plot'))

expect(resetSpy).not.toHaveBeenCalled()
expect(fakeDoubleClickInteractionHandler).toHaveBeenCalled()
expect(fakeSingleClickInteractionHandler).toHaveBeenCalled()

const [
[callbackArguments],
] = fakeDoubleClickInteractionHandler.mock.calls
] = fakeSingleClickInteractionHandler.mock.calls

// don't care what the values are, we just care that we pass these values back
expect(Object.keys(callbackArguments)).toEqual([
Expand Down
14 changes: 10 additions & 4 deletions giraffe/src/components/SizedPlot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ export const SizedPlot: FunctionComponent<Props> = ({
},
}

const doubleClick = config.interactionHandlers?.doubleClick
const singleClick = config.interactionHandlers?.singleClick
? () => {
config.interactionHandlers.doubleClick(plotInteraction)
config.interactionHandlers.singleClick(plotInteraction)
}
: memoizedResetDomains

Expand All @@ -108,7 +108,7 @@ export const SizedPlot: FunctionComponent<Props> = ({
}

const callbacks = {
doubleClick,
singleClick,
}

const fullsizeStyle: CSSProperties = {
Expand All @@ -119,6 +119,12 @@ export const SizedPlot: FunctionComponent<Props> = ({
bottom: 0,
}

// for single clicking; using mouseup, since the onClick only gets through
// with a double click; and the hover and drag target does not use a mouse up;
// they are: hover: mouseEnter, mousemove, mouseleave
// drag target: mouseDown
// and every time there is a single click, the mouse goes up. so using that instead.

return (
<div
className="giraffe-plot"
Expand All @@ -143,7 +149,7 @@ export const SizedPlot: FunctionComponent<Props> = ({
left: `${margins.left}px`,
cursor: `${userConfig.cursor || 'crosshair'}`,
}}
onDoubleClick={callbacks.doubleClick}
onMouseUp={callbacks.singleClick}
{...hoverTargetProps}
{...dragTargetProps}
>
Expand Down
1 change: 1 addition & 0 deletions giraffe/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export interface InteractionHandlerArguments {

export interface InteractionHandlers {
doubleClick?: (plotInteraction: InteractionHandlerArguments) => void
singleClick?: (plotInteraction: InteractionHandlerArguments) => void
hover?: (plotInteraction: InteractionHandlerArguments) => void
}

Expand Down

0 comments on commit a9fa73f

Please sign in to comment.