This repository has been archived by the owner on Jul 31, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
130 lines (114 loc) · 3.92 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import * as React from 'react';
import {storiesOf} from '@storybook/react';
import {action} from '@storybook/addon-actions';
import {number} from '@storybook/addon-knobs';
import styled from 'styled-components';
import CellPlot, {Rectangle, Coordinate} from '../src';
const teal = '#58A9A3';
const gold = '#CEB471';
const blue = '#53697D';
const red = '#F7846B';
const highlight = '#DDDD00';
const Container = styled.div`
width: 300px;
height: 300px;
display: flex;
margin: 10px;
`;
const Centered = styled.div`
display: flex;
flex: 1 1 auto;
justify-content: center;
align-items: center;
`;
const XLabel = Centered.extend`
margin-bottom: 5px;
`;
const YLabel = Centered.extend`
justify-content: flex-end;
margin-right: 5px;
`;
const Box = Centered.extend`
height: calc(100% - 1px);
width: calc(100% - 1px);
margin-right: 1px;
margin-bottom: 1px;
color: white;
opacity: 0.8;
`;
function rangeToArray(r: [number, number]): number[] {
return Array(r[1] - r[0] + 1).fill(0).map((_, idx) => idx + r[0]);
}
interface DemoProps {
xMin: number;
yMin: number;
xMax: number;
yMax: number;
}
interface DemoState {
cellHover: Coordinate | null;
rectangleHoverEast: boolean;
rectangleHoverWest: boolean;
rectangleHoverNorth: boolean;
rectangleHoverSouth: boolean;
}
class Demo extends React.Component<DemoProps, DemoState> {
constructor(props: DemoProps) {
super(props);
this.state = {
cellHover: null,
rectangleHoverEast: false,
rectangleHoverWest: false,
rectangleHoverNorth: false,
rectangleHoverSouth: false
};
}
render() {
return <Container>
<CellPlot
xLabels={[<XLabel>left</XLabel>, <XLabel>center</XLabel>, <XLabel>right</XLabel>]}
yLabels={[<YLabel>top</YLabel>, <YLabel>center</YLabel>, <YLabel>bottom</YLabel>]}
xs={rangeToArray([this.props.xMin, this.props.xMax])}
ys={rangeToArray([this.props.yMin, this.props.yMax])}
verticalBorders={['1px solid #555', '1px dotted #ccc']}
horizontalBorders={['1px solid #555', '1px dotted #ccc']}
onClick={action('onClick')}
onHover={(coord) => this.setState({cellHover: coord})}>
{this.state.cellHover &&
<Rectangle style={{zIndex: 1, pointerEvents: 'none'}} x={this.state.cellHover.x} y={this.state.cellHover.y} width={1} height={1}>
<Box style={{backgroundColor: highlight}}></Box>
</Rectangle>
}
<Rectangle x={5} y={3} width={2} height={2} onHover={(hovering: boolean) => this.setState({rectangleHoverEast: hovering})}>
<Box style={{backgroundColor: this.state.rectangleHoverEast ? highlight : teal}}>
East
</Box>
</Rectangle>
<Rectangle x={3} y={5} width={2} height={2} onHover={(hovering: boolean) => this.setState({rectangleHoverSouth: hovering})}>
<Box style={{backgroundColor: this.state.rectangleHoverSouth ? highlight : red}}>
South
</Box>
</Rectangle>
<Rectangle x={1} y={3} width={2} height={2} onHover={(hovering: boolean) => this.setState({rectangleHoverWest: hovering})}>
<Box style={{backgroundColor: this.state.rectangleHoverWest ? highlight : gold}}>
West
</Box>
</Rectangle>
<Rectangle x={3} y={1} width={2} height={2} onHover={(hovering: boolean) => this.setState({rectangleHoverNorth: hovering})}>
<Box style={{backgroundColor: this.state.rectangleHoverNorth ? highlight : blue}}>
North
</Box>
</Rectangle>
</CellPlot>
</Container>;
}
}
storiesOf('CellPlot', module)
.add('Grid', () => {
return <Demo
xMin={number('xMin', 1, {range: true, min: 0, max: 5, step: 1})}
yMin={number('yMin', 1, {range: true, min: 0, max: 5, step: 1})}
xMax={number('xMax', 6, {range: true, min: 6, max: 20, step: 1})}
yMax={number('yMax', 6, {range: true, min: 6, max: 20, step: 1})}
/>;
});