Skip to content

Commit

Permalink
Reduce flow config (react-grid-layout#685)
Browse files Browse the repository at this point in the history
* Reduce flow config

* Remove jsxhint
  • Loading branch information
TrySound authored and STRML committed Dec 12, 2017
1 parent 6f98f31 commit aee8228
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 274 deletions.
11 changes: 0 additions & 11 deletions .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,10 @@
0.61.0

[ignore]
.*test
./build

[include]
./lib
index.js

[libs]
interfaces/

[options]
suppress_comment=\\(.\\|\n\\)*\\s*\\$FlowFixMe.*
suppress_comment=\\(.\\|\n\\)*\\s*\\$FlowBug.*
suppress_comment=\\(.\\|\n\\)*\\s*\\$FlowIgnore.*
suppress_comment=\\(.\\|\n\\)*\\s*\\$FlowNewLine.*
suppress_comment=\\(.\\|\n\\)*\\s*\\$FlowIssue
esproposal.class_instance_fields=enable
esproposal.class_static_fields=enable
9 changes: 3 additions & 6 deletions lib/ReactGridLayout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -609,18 +609,15 @@ export default class ReactGridLayout extends React.Component<Props, State> {
render() {
const { className, style } = this.props;

const mergedClassName = classNames("react-grid-layout", className);
const mergedStyle = {
height: this.containerHeight(),
...style
};

return (
<div
className={classNames("react-grid-layout", className)}
style={mergedStyle}
>
{// $FlowIgnore: Appears to think map calls back w/array
React.Children.map(this.props.children, child =>
<div className={mergedClassName} style={mergedStyle}>
{React.Children.map(this.props.children, child =>
this.processGridItem(child)
)}
{this.placeholder()}
Expand Down
7 changes: 4 additions & 3 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ export function cloneLayoutItem(layoutItem: LayoutItem): LayoutItem {
* This will catch differences in keys, order, and length.
*/
export function childrenEqual(a: ReactChildren, b: ReactChildren): boolean {
// $FlowIgnore: Appears to think map calls back w/array
return isEqual(
React.Children.map(a, c => c.key),
React.Children.map(b, c => c.key)
Expand Down Expand Up @@ -632,8 +631,10 @@ export function synchronizeLayoutWithChildren(
* @param {String} [contextName] Context name for errors.
* @throw {Error} Validation error.
*/
export function validateLayout(layout: Layout, contextName: string): void {
contextName = contextName || "Layout";
export function validateLayout(
layout: Layout,
contextName: string = "Layout"
): void {
const subProps = ["x", "y", "w", "h"];
if (!Array.isArray(layout))
throw new Error(contextName + " must be an array!");
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
"husky": "^0.14.3",
"imports-loader": "^0.7.1",
"jest-cli": "^21.2.1",
"jsxhint": "^0.15.1",
"lint-staged": "^6.0.0",
"lodash": "^4.3.0",
"opener": "^1.4.3",
Expand Down
59 changes: 41 additions & 18 deletions test/spec/utils-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @flow

import {
bottom,
collides,
Expand All @@ -7,16 +8,23 @@ import {
compact,
sortLayoutItemsByRowCol
} from "../../lib/utils.js";
/*:: import type { Layout } from "../../lib/utils.js"; */
import assert from "power-assert";

/*:: declare function describe(name: string, fn: Function): void; */
/*:: declare function it(name: string, fn: Function): void; */

describe("bottom", () => {
it("Handles an empty layout as input", () => {
assert(bottom([]) === 0);
});

it("Returns the bottom coordinate of the layout", () => {
assert(
bottom([{ x: 0, y: 1, w: 1, h: 1 }, { x: 1, y: 2, w: 1, h: 1 }]) === 3
bottom([
{ i: "1", x: 0, y: 1, w: 1, h: 1 },
{ i: "2", x: 1, y: 2, w: 1, h: 1 }
]) === 3
);
});
});
Expand All @@ -39,10 +47,16 @@ describe("sortLayoutItemsByRowCol", () => {
describe("collides", () => {
it("Returns whether the layout items collide", () => {
assert(
collides({ x: 0, y: 1, w: 1, h: 1 }, { x: 1, y: 2, w: 1, h: 1 }) === false
collides(
{ i: "1", x: 0, y: 1, w: 1, h: 1 },
{ i: "2", x: 1, y: 2, w: 1, h: 1 }
) === false
);
assert(
collides({ x: 0, y: 1, w: 1, h: 1 }, { x: 0, y: 1, w: 1, h: 1 }) === true
collides(
{ i: "1", x: 0, y: 1, w: 1, h: 1 },
{ i: "2", x: 0, y: 1, w: 1, h: 1 }
) === true
);
});
});
Expand All @@ -52,20 +66,27 @@ describe("validateLayout", () => {
validateLayout([]);
});
it("Validates a populated layout", () => {
validateLayout([{ x: 0, y: 1, w: 1, h: 1 }, { x: 1, y: 2, w: 1, h: 1 }]);
validateLayout([
{ i: "1", x: 0, y: 1, w: 1, h: 1 },
{ i: "2", x: 1, y: 2, w: 1, h: 1 }
]);
});
it("Throws errors on invalid input", () => {
assert.throws(() => {
validateLayout([{ x: 0, y: 1, w: 1, h: 1 }, { x: 1, y: 2, w: 1 }]);
// $FlowFixMe: dynamic check
validateLayout([
{ i: "1", x: 0, y: 1, w: 1, h: 1 },
{ i: "2", x: 1, y: 2, w: 1 }
]);
}, /layout\[1\]\.h must be a number!/i);
});
});

describe("moveElement", () => {
it("Does not change layout when colliding on no rearrangement mode", () => {
const layout = [
{ x: 0, y: 1, w: 1, h: 1, moved: false },
{ x: 1, y: 2, w: 1, h: 1, moved: false }
{ i: "1", x: 0, y: 1, w: 1, h: 1, moved: false },
{ i: "2", x: 1, y: 2, w: 1, h: 1, moved: false }
];
const layoutItem = layout[0];
assert.deepEqual(
Expand All @@ -75,19 +96,21 @@ describe("moveElement", () => {
1,
2, // x, y
true,
true // isUserAction, preventCollision
true, // isUserAction, preventCollision
null,
2
),
[
{ x: 0, y: 1, w: 1, h: 1, moved: false },
{ x: 1, y: 2, w: 1, h: 1, moved: false }
{ i: "1", x: 0, y: 1, w: 1, h: 1, moved: false },
{ i: "2", x: 1, y: 2, w: 1, h: 1, moved: false }
]
);
});

it("Does change layout when colliding in rearrangement mode", () => {
const layout = [
{ x: 0, y: 0, w: 1, h: 1, moved: false },
{ x: 1, y: 0, w: 1, h: 1, moved: false }
{ i: "1", x: 0, y: 0, w: 1, h: 1, moved: false },
{ i: "2", x: 1, y: 0, w: 1, h: 1, moved: false }
];
const layoutItem = layout[0];
assert.deepEqual(
Expand All @@ -102,8 +125,8 @@ describe("moveElement", () => {
2 // compactType, cols
),
[
{ x: 1, y: 0, w: 1, h: 1, moved: true },
{ x: 1, y: 1, w: 1, h: 1, moved: true }
{ i: "1", x: 1, y: 0, w: 1, h: 1, moved: true },
{ i: "2", x: 1, y: 1, w: 1, h: 1, moved: true }
]
);
});
Expand Down Expand Up @@ -223,9 +246,9 @@ describe("moveElement", () => {

describe("compact vertical", () => {
it("Removes empty vertical space above item", () => {
const layout = [{ x: 0, y: 1, w: 1, h: 1 }];
const layout = [{ i: "1", x: 0, y: 1, w: 1, h: 1 }];
assert.deepEqual(compact(layout, "vertical", 10), [
{ x: 0, y: 0, w: 1, h: 1, moved: false }
{ i: "1", x: 0, y: 0, w: 1, h: 1, moved: false }
]);
});

Expand Down Expand Up @@ -260,9 +283,9 @@ describe("compact vertical", () => {

describe("compact horizontal", () => {
it("compact horizontal should remove empty horizontal space to left of item", () => {
const layout = [{ x: 5, y: 5, w: 1, h: 1 }];
const layout = [{ i: "1", x: 5, y: 5, w: 1, h: 1 }];
assert.deepEqual(compact(layout, "horizontal", 10), [
{ x: 0, y: 0, w: 1, h: 1, moved: false }
{ i: "1", x: 0, y: 0, w: 1, h: 1, moved: false }
]);
});

Expand Down
Loading

0 comments on commit aee8228

Please sign in to comment.