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

added onHover for CellPlot and Rectangle components #3

Merged
merged 3 commits into from
Jan 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/CellPlot.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';

import Frame from './Frame';
import Cells from './Cells';
import Cells, {Coordinate} from './Cells';
import {PlotContext, PlotContextProps} from './PlotContext';
import {PlotConsumer, PlotConsumerProps} from './PlotConsumer';

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

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

function snap(value: number, interval: number): number {
Expand Down Expand Up @@ -92,7 +93,8 @@ export default class CellPlot extends React.Component<Props> {
verticalBorders={this.props.verticalBorders}
ys={this.props.ys}
horizontalBorders={this.props.horizontalBorders}
onClick={this.props.onClick} />
onClick={this.props.onClick}
onHover={this.props.onHover} />

{this.props.children}
</Frame>
Expand Down
11 changes: 10 additions & 1 deletion src/Cells.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import * as React from 'react';
import styled from 'styled-components';

export interface Coordinate {
x: number;
y: number;
}

export interface Props {
xs: any[];
ys: any[];
verticalBorders?: string[];
horizontalBorders?: string[];
onClick?: (x: any, y: any) => void;
onHover?: (coordinate: Coordinate | null) => void;
}

const Columns = styled.div`
Expand All @@ -31,7 +37,7 @@ const Cell = styled.div`
* Cells can be placed inside a Frame to create vertical and horizontal gridlines. It acts as a
* background layer, and does not render children.
*/
const Cells: React.SFC<Props> = ({xs, ys, verticalBorders, horizontalBorders, onClick}) => {
const Cells: React.SFC<Props> = ({xs, ys, verticalBorders, horizontalBorders, onClick, onHover}) => {
return (
<Columns>
{xs.map((x, idx) => (
Expand All @@ -44,6 +50,9 @@ const Cells: React.SFC<Props> = ({xs, ys, verticalBorders, horizontalBorders, on
<Cell
key={y}
onClick={() => onClick && onClick(x, y)}
onMouseOver={() => onHover && onHover({x, y})}
onMouseEnter={() => onHover && onHover({x, y})}
onMouseLeave={() => onHover && onHover(null)}
style={{
borderBottom: horizontalBorders && horizontalBorders[(idy + 1) % horizontalBorders.length]
}} />
Expand Down
11 changes: 9 additions & 2 deletions src/Rectangle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface Props {
y: number;
width: number | string;
height: number | string;
onHover?: (hovering: boolean) => void;
}

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

render() {
const {x, y, height, width, style, ...remaining} = this.props;
const {x, y, height, width, style, onHover, ...remaining} = this.props;

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

// provided styles override calculated styles.
return <Container {...remaining} style={{...layout, ...style}} />;
return <Container
onMouseEnter={() => onHover && onHover(true)}
onMouseOver={() => onHover && onHover(true)}
onMouseLeave={() => onHover && onHover(false)}
{...remaining}
style={{...layout, ...style}}
/>;
}
}
Loading