Skip to content

Commit 731488b

Browse files
committed
chore(pkg): Upgrade flow, react-draggable
1 parent fcad7a3 commit 731488b

11 files changed

+1467
-1026
lines changed

.eslintrc

+2-3
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@
4444
},
4545
"globals": {
4646
// For Flow
47-
"ReactElement",
48-
"ReactClass",
49-
"$PropertyType"
47+
"$PropertyType",
48+
"$Exact"
5049
}
5150
}

.flowconfig

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
[version]
2-
0.44.1
2+
0.53.1
33

44
[ignore]
5-
.*/node_modules/react/.*
6-
.*/node_modules/babel.*
7-
.*/node_modules/fbjs/.*
85
.*test
6+
/build
97

108
[include]
119
./lib
@@ -20,5 +18,5 @@ suppress_comment=\\(.\\|\n\\)*\\s*\\$FlowBug.*
2018
suppress_comment=\\(.\\|\n\\)*\\s*\\$FlowIgnore.*
2119
suppress_comment=\\(.\\|\n\\)*\\s*\\$FlowNewLine.*
2220
suppress_comment=\\(.\\|\n\\)*\\s*\\$FlowIssue
23-
esproposal.class_instance_fields=ignore
24-
esproposal.class_static_fields=ignore
21+
esproposal.class_instance_fields=enable
22+
esproposal.class_static_fields=enable

dist/react-grid-layout.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/GridItem.jsx

+56-11
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,65 @@
11
// @flow
2-
import React from 'react';
2+
import * as React from 'react';
33
import PropTypes from 'prop-types';
44
import {DraggableCore} from 'react-draggable';
55
import {Resizable} from 'react-resizable';
66
import {perc, setTopLeft, setTransform} from './utils';
77
import classNames from 'classnames';
88

9-
import type {DragCallbackData, Position} from './utils';
9+
import type {ReactDraggableCallbackData, GridDragEvent, GridResizeEvent, Position, Size} from './utils';
10+
11+
type PartialPosition = {top: number, left: number};
12+
type GridItemCallback<Data: GridDragEvent | GridResizeEvent> = (i: string, w: number, h: number, Data) => void;
1013

1114
type State = {
1215
resizing: ?{width: number, height: number},
1316
dragging: ?{top: number, left: number},
1417
className: string
1518
};
1619

20+
type Props = {
21+
children: React.Element<any>,
22+
cols: number,
23+
containerWidth: number,
24+
margin: [number, number],
25+
containerPadding: [number, number],
26+
rowHeight: number,
27+
maxRows: number,
28+
isDraggable: boolean,
29+
isResizable: boolean,
30+
static?: boolean,
31+
useCSSTransforms?: boolean,
32+
usePercentages?: boolean,
33+
34+
className: string,
35+
style?: Object,
36+
// Draggability
37+
cancel: string,
38+
handle: string,
39+
40+
x: number,
41+
y: number,
42+
w: number,
43+
h: number,
44+
45+
minW: number,
46+
maxW: number,
47+
minH: number,
48+
maxH: number,
49+
i: string,
50+
51+
onDrag?: GridItemCallback<GridDragEvent>,
52+
onDragStart?: GridItemCallback<GridDragEvent>,
53+
onDragStop?: GridItemCallback<GridDragEvent>,
54+
onResize?: GridItemCallback<GridResizeEvent>,
55+
onResizeStart?: GridItemCallback<GridResizeEvent>,
56+
onResizeStop?: GridItemCallback<GridResizeEvent>,
57+
};
58+
1759
/**
1860
* An individual item within a ReactGridLayout.
1961
*/
20-
export default class GridItem extends React.Component {
62+
export default class GridItem extends React.Component<Props, State> {
2163

2264
static propTypes = {
2365
// Children must be only a single element
@@ -92,10 +134,11 @@ export default class GridItem extends React.Component {
92134
static defaultProps = {
93135
className: '',
94136
cancel: '',
137+
handle: '',
95138
minH: 1,
96139
minW: 1,
97140
maxH: Infinity,
98-
maxW: Infinity
141+
maxW: Infinity,
99142
};
100143

101144
state: State = {
@@ -285,10 +328,11 @@ export default class GridItem extends React.Component {
285328
* @return {Function} Handler function.
286329
*/
287330
onDragHandler(handlerName:string) {
288-
return (e:Event, {node, deltaX, deltaY}: DragCallbackData) => {
289-
if (!this.props[handlerName]) return;
331+
return (e:Event, {node, deltaX, deltaY}: ReactDraggableCallbackData) => {
332+
const handler = this.props[handlerName];
333+
if (!handler) return;
290334

291-
const newPosition: {top: number, left: number} = {top: 0, left: 0};
335+
const newPosition: PartialPosition = {top: 0, left: 0};
292336

293337
// Get new XY
294338
switch (handlerName) {
@@ -319,7 +363,7 @@ export default class GridItem extends React.Component {
319363

320364
const {x, y} = this.calcXY(newPosition.top, newPosition.left);
321365

322-
this.props[handlerName](this.props.i, x, y, {e, node, newPosition});
366+
handler.call(this, this.props.i, x, y, {e, node, newPosition});
323367
};
324368
}
325369

@@ -333,7 +377,8 @@ export default class GridItem extends React.Component {
333377
*/
334378
onResizeHandler(handlerName: string) {
335379
return (e:Event, {node, size}: {node: HTMLElement, size: Position}) => {
336-
if (!this.props[handlerName]) return;
380+
const handler = this.props[handlerName];
381+
if (!handler) return;
337382
const {cols, x, i, maxW, minW, maxH, minH} = this.props;
338383

339384
// Get new XY
@@ -350,11 +395,11 @@ export default class GridItem extends React.Component {
350395

351396
this.setState({resizing: handlerName === 'onResizeStop' ? null : size});
352397

353-
this.props[handlerName](i, w, h, {e, node, size});
398+
handler.call(this, i, w, h, {e, node, size});
354399
};
355400
}
356401

357-
render(): React.Element<any> {
402+
render(): React.Node {
358403
const {x, y, w, h, isDraggable, isResizable, useCSSTransforms} = this.props;
359404

360405
const pos = this.calcPosition(x, y, w, h, this.state);

lib/ReactGridLayout.jsx

+46-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// @flow
2-
import React from 'react';
2+
import * as React from 'react';
33
import PropTypes from 'prop-types';
44
import isEqual from 'lodash.isequal';
55
import classNames from 'classnames';
@@ -9,7 +9,7 @@ import GridItem from './GridItem';
99
const noop = function() {};
1010

1111
// Types
12-
import type {ResizeEvent, DragEvent, Layout, LayoutItem} from './utils';
12+
import type {EventCallback, GridResizeEvent, GridDragEvent, Layout, LayoutItem} from './utils';
1313
type State = {
1414
activeDrag: ?LayoutItem,
1515
layout: Layout,
@@ -18,13 +18,42 @@ type State = {
1818
oldLayout: ?Layout,
1919
oldResizeItem: ?LayoutItem
2020
};
21+
22+
export type Props = {
23+
className: string,
24+
style: Object,
25+
width: number,
26+
autoSize: boolean,
27+
cols: number,
28+
draggableCancel: string,
29+
draggableHandle: string,
30+
verticalCompact: boolean,
31+
layout: Layout,
32+
margin: [number, number],
33+
containerPadding: [number, number],
34+
rowHeight: number,
35+
maxRows: number,
36+
isDraggable: boolean,
37+
isResizable: boolean,
38+
useCSSTransforms: boolean,
39+
40+
// Callbacks
41+
onLayoutChange: (Layout) => void,
42+
onDrag: EventCallback,
43+
onDragStart: EventCallback,
44+
onDragStop: EventCallback,
45+
onResize: EventCallback,
46+
onResizeStart: EventCallback,
47+
onResizeStop: EventCallback,
48+
children: React.ChildrenArray<React.Element<any>>,
49+
};
2150
// End Types
2251

2352
/**
2453
* A reactive, fluid grid layout with draggable, resizable components.
2554
*/
2655

27-
export default class ReactGridLayout extends React.Component {
56+
export default class ReactGridLayout extends React.Component<Props, State> {
2857
// TODO publish internal ReactClass displayName transform
2958
static displayName = "ReactGridLayout";
3059

@@ -94,7 +123,7 @@ export default class ReactGridLayout extends React.Component {
94123
// Callback so you can save the layout. Calls after each drag & resize stops.
95124
onLayoutChange: PropTypes.func,
96125

97-
// Calls when drag starts. Callback is of the signature (layout, oldItem, newItem, placeholder, e).
126+
// Calls when drag starts. Callback is of the signature (layout, oldItem, newItem, placeholder, e, ?node).
98127
// All callbacks below have the same signature. 'start' and 'stop' callbacks omit the 'placeholder'.
99128
onDragStart: PropTypes.func,
100129
// Calls on each drag movement.
@@ -158,7 +187,7 @@ export default class ReactGridLayout extends React.Component {
158187
oldResizeItem: null,
159188
};
160189

161-
constructor(props: $PropertyType<ReactGridLayout, 'props'>, context: any): void {
190+
constructor(props: Props, context: any): void {
162191
super(props, context);
163192
autoBindHandlers(this, ['onDragStart', 'onDrag', 'onDragStop', 'onResizeStart', 'onResize', 'onResizeStop']);
164193
}
@@ -170,7 +199,7 @@ export default class ReactGridLayout extends React.Component {
170199
this.onLayoutMaybeChanged(this.state.layout, this.props.layout);
171200
}
172201

173-
componentWillReceiveProps(nextProps: $PropertyType<ReactGridLayout, 'props'>) {
202+
componentWillReceiveProps(nextProps: Props) {
174203
let newLayoutBase;
175204
// Allow parent to set layout directly.
176205
if (!isEqual(nextProps.layout, this.props.layout)) {
@@ -213,7 +242,7 @@ export default class ReactGridLayout extends React.Component {
213242
* @param {Event} e The mousedown event
214243
* @param {Element} node The current dragging DOM element
215244
*/
216-
onDragStart(i:string, x:number, y:number, {e, node}: DragEvent) {
245+
onDragStart(i:string, x:number, y:number, {e, node}: GridDragEvent) {
217246
const {layout} = this.state;
218247
var l = getLayoutItem(layout, i);
219248
if (!l) return;
@@ -231,7 +260,7 @@ export default class ReactGridLayout extends React.Component {
231260
* @param {Event} e The mousedown event
232261
* @param {Element} node The current dragging DOM element
233262
*/
234-
onDrag(i:string, x:number, y:number, {e, node}: DragEvent) {
263+
onDrag(i:string, x:number, y:number, {e, node}: GridDragEvent) {
235264
const {oldDragItem} = this.state;
236265
let {layout} = this.state;
237266
var l = getLayoutItem(layout, i);
@@ -261,7 +290,7 @@ export default class ReactGridLayout extends React.Component {
261290
* @param {Event} e The mousedown event
262291
* @param {Element} node The current dragging DOM element
263292
*/
264-
onDragStop(i:string, x:number, y:number, {e, node}: DragEvent) {
293+
onDragStop(i:string, x:number, y:number, {e, node}: GridDragEvent) {
265294
const {oldDragItem} = this.state;
266295
let {layout} = this.state;
267296
const l = getLayoutItem(layout, i);
@@ -292,7 +321,7 @@ export default class ReactGridLayout extends React.Component {
292321
}
293322
}
294323

295-
onResizeStart(i:string, w:number, h:number, {e, node}: ResizeEvent) {
324+
onResizeStart(i:string, w:number, h:number, {e, node}: GridResizeEvent) {
296325
const {layout} = this.state;
297326
var l = getLayoutItem(layout, i);
298327
if (!l) return;
@@ -305,7 +334,7 @@ export default class ReactGridLayout extends React.Component {
305334
this.props.onResizeStart(layout, l, l, null, e, node);
306335
}
307336

308-
onResize(i:string, w:number, h:number, {e, node}: ResizeEvent) {
337+
onResize(i:string, w:number, h:number, {e, node}: GridResizeEvent) {
309338
const {layout, oldResizeItem} = this.state;
310339
var l = getLayoutItem(layout, i);
311340
if (!l) return;
@@ -328,7 +357,7 @@ export default class ReactGridLayout extends React.Component {
328357
});
329358
}
330359

331-
onResizeStop(i:string, w:number, h:number, {e, node}: ResizeEvent) {
360+
onResizeStop(i:string, w:number, h:number, {e, node}: GridResizeEvent) {
332361
const {layout, oldResizeItem} = this.state;
333362
var l = getLayoutItem(layout, i);
334363

@@ -386,7 +415,7 @@ export default class ReactGridLayout extends React.Component {
386415
*/
387416
processGridItem(child: React.Element<any>): ?React.Element<any> {
388417
if (!child.key) return;
389-
const l = getLayoutItem(this.state.layout, child.key);
418+
const l = getLayoutItem(this.state.layout, String(child.key));
390419
if (!l) return null;
391420
const {width, cols, margin, containerPadding, rowHeight,
392421
maxRows, isDraggable, isResizable, useCSSTransforms,
@@ -444,7 +473,10 @@ export default class ReactGridLayout extends React.Component {
444473

445474
return (
446475
<div className={classNames('react-grid-layout', className)} style={mergedStyle}>
447-
{React.Children.map(this.props.children, (child) => this.processGridItem(child))}
476+
{
477+
// $FlowIgnore: Appears to think map calls back w/array
478+
React.Children.map(this.props.children, (child) => this.processGridItem(child))
479+
}
448480
{this.placeholder()}
449481
</div>
450482
);

lib/ResponsiveReactGridLayout.jsx

+24-6
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,41 @@
11
// @flow
2-
import React from 'react';
2+
import * as React from 'react';
33
import PropTypes from 'prop-types';
44
import isEqual from 'lodash.isequal';
55

66
import {cloneLayout, synchronizeLayoutWithChildren, validateLayout} from './utils';
77
import {getBreakpointFromWidth, getColsFromBreakpoint, findOrGenerateResponsiveLayout} from './responsiveUtils';
88
import ReactGridLayout from './ReactGridLayout';
9+
import type {Props as RGLProps} from './ReactGridLayout';
10+
import type {Layout} from './utils';
911

1012
const noop = function(){};
1113
const type = (obj) => Object.prototype.toString.call(obj);
1214

13-
import type {Layout} from './utils';
1415
type State = {
1516
layout: Layout,
1617
breakpoint: string,
1718
cols: number
1819
};
1920

20-
export default class ResponsiveReactGridLayout extends React.Component {
21+
type Props<Breakpoint: string = string> = {
22+
...$Exact<RGLProps>,
23+
24+
// Responsive config
25+
breakpoint: Breakpoint,
26+
breakpoints: {[key: Breakpoint]: number},
27+
cols: {[key: Breakpoint]: number},
28+
layouts: {[key: Breakpoint]: Layout},
29+
width: number,
30+
31+
// Callbacks
32+
onBreakpointChange: (Breakpoint, cols: number) => void,
33+
onLayoutChange: (Layout, {[key: Breakpoint]: Layout}) => void,
34+
onWidthChange:
35+
(containerWidth: number, margin: [number, number], cols: number, containerPadding: [number, number]) => void
36+
};
37+
38+
export default class ResponsiveReactGridLayout extends React.Component<Props<>, State> {
2139

2240
// This should only include propTypes needed in this code; RGL itself
2341
// will do validation of the rest props passed to it.
@@ -79,7 +97,7 @@ export default class ResponsiveReactGridLayout extends React.Component {
7997
onWidthChange: noop,
8098
};
8199

82-
state: State = this.generateInitialState();
100+
state = this.generateInitialState();
83101

84102
generateInitialState(): State {
85103
const {width, breakpoints, layouts, verticalCompact, cols} = this.props;
@@ -97,7 +115,7 @@ export default class ResponsiveReactGridLayout extends React.Component {
97115
};
98116
}
99117

100-
componentWillReceiveProps(nextProps: Object) {
118+
componentWillReceiveProps(nextProps: Props<*>) {
101119

102120
// Allow parent to set width or breakpoint directly.
103121
if (
@@ -132,7 +150,7 @@ export default class ResponsiveReactGridLayout extends React.Component {
132150
* When the width changes work through breakpoints and reset state with the new width & breakpoint.
133151
* Width changes are necessary to figure out the widget widths.
134152
*/
135-
onWidthChange(nextProps: typeof ResponsiveReactGridLayout.prototype.props) {
153+
onWidthChange(nextProps: Props<*>) {
136154
const {breakpoints, cols, layouts, verticalCompact} = nextProps;
137155
const newBreakpoint = nextProps.breakpoint || getBreakpointFromWidth(nextProps.breakpoints, nextProps.width);
138156

0 commit comments

Comments
 (0)