Skip to content
This repository was archived by the owner on Jul 31, 2019. It is now read-only.

Commit f7fb9d0

Browse files
authored
Merge pull request #3 from EmpaticoOrg/onHover
added onHover for CellPlot and Rectangle components
2 parents 2fb9ecc + f413c02 commit f7fb9d0

File tree

6 files changed

+239
-24
lines changed

6 files changed

+239
-24
lines changed

src/CellPlot.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from 'react';
22

33
import Frame from './Frame';
4-
import Cells from './Cells';
4+
import Cells, {Coordinate} from './Cells';
55
import {PlotContext, PlotContextProps} from './PlotContext';
66
import {PlotConsumer, PlotConsumerProps} from './PlotConsumer';
77

@@ -14,7 +14,8 @@ export interface Props {
1414
horizontalBorders?: string[]; // a cyclic sequence of strings for the css border property
1515
ys: number[];
1616

17-
onClick?: (x: number, y: number) => void;
17+
onClick?: (x: number, y: number) => void; // TODO: convert to Coordinate in 2.x release
18+
onHover?: (coordinate: Coordinate) => void;
1819
}
1920

2021
function snap(value: number, interval: number): number {
@@ -92,7 +93,8 @@ export default class CellPlot extends React.Component<Props> {
9293
verticalBorders={this.props.verticalBorders}
9394
ys={this.props.ys}
9495
horizontalBorders={this.props.horizontalBorders}
95-
onClick={this.props.onClick} />
96+
onClick={this.props.onClick}
97+
onHover={this.props.onHover} />
9698

9799
{this.props.children}
98100
</Frame>

src/Cells.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
import * as React from 'react';
22
import styled from 'styled-components';
33

4+
export interface Coordinate {
5+
x: number;
6+
y: number;
7+
}
8+
49
export interface Props {
510
xs: any[];
611
ys: any[];
712
verticalBorders?: string[];
813
horizontalBorders?: string[];
914
onClick?: (x: any, y: any) => void;
15+
onHover?: (coordinate: Coordinate | null) => void;
1016
}
1117

1218
const Columns = styled.div`
@@ -31,7 +37,7 @@ const Cell = styled.div`
3137
* Cells can be placed inside a Frame to create vertical and horizontal gridlines. It acts as a
3238
* background layer, and does not render children.
3339
*/
34-
const Cells: React.SFC<Props> = ({xs, ys, verticalBorders, horizontalBorders, onClick}) => {
40+
const Cells: React.SFC<Props> = ({xs, ys, verticalBorders, horizontalBorders, onClick, onHover}) => {
3541
return (
3642
<Columns>
3743
{xs.map((x, idx) => (
@@ -44,6 +50,9 @@ const Cells: React.SFC<Props> = ({xs, ys, verticalBorders, horizontalBorders, on
4450
<Cell
4551
key={y}
4652
onClick={() => onClick && onClick(x, y)}
53+
onMouseOver={() => onHover && onHover({x, y})}
54+
onMouseEnter={() => onHover && onHover({x, y})}
55+
onMouseLeave={() => onHover && onHover(null)}
4756
style={{
4857
borderBottom: horizontalBorders && horizontalBorders[(idy + 1) % horizontalBorders.length]
4958
}} />

src/Rectangle.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface Props {
88
y: number;
99
width: number | string;
1010
height: number | string;
11+
onHover?: (hovering: boolean) => void;
1112
}
1213

1314
const Container = styled.div`
@@ -24,7 +25,7 @@ export default class Rectangle extends React.Component<Props & React.HTMLAttribu
2425
context: PlotContext;
2526

2627
render() {
27-
const {x, y, height, width, style, ...remaining} = this.props;
28+
const {x, y, height, width, style, onHover, ...remaining} = this.props;
2829

2930
// invisible, don't render
3031
if (
@@ -55,6 +56,12 @@ export default class Rectangle extends React.Component<Props & React.HTMLAttribu
5556
};
5657

5758
// provided styles override calculated styles.
58-
return <Container {...remaining} style={{...layout, ...style}} />;
59+
return <Container
60+
onMouseEnter={() => onHover && onHover(true)}
61+
onMouseOver={() => onHover && onHover(true)}
62+
onMouseLeave={() => onHover && onHover(false)}
63+
{...remaining}
64+
style={{...layout, ...style}}
65+
/>;
5966
}
6067
}

0 commit comments

Comments
 (0)