Skip to content

Commit b056238

Browse files
committed
fix: handle single child in Resizable, add version to demo
- Fix longstanding bug where single child caused "not iterable" error by using React.Children.toArray() instead of spreading directly - Remove array workaround from tests now that bug is fixed - Add version/commit display to demo page via webpack DefinePlugin - Add eslint global for Element (from merged PR #220)
1 parent 75f5012 commit b056238

File tree

7 files changed

+43
-9
lines changed

7 files changed

+43
-9
lines changed

__tests__/Resizable.test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ describe('render Resizable', () => {
2525
transformScale: 1,
2626
width: 50,
2727
};
28-
const userChildren = <span key="user-children" className={'children'} />;
29-
// Note: children.props.children must be an array for Resizable to spread it
30-
const resizableBoxChildren = <div style={{width: '50px', height: '50px'}}>{[userChildren]}</div>;
28+
const userChildren = <span className={'children'} />;
29+
const resizableBoxChildren = <div style={{width: '50px', height: '50px'}}>{userChildren}</div>;
3130

3231
beforeEach(() => {
3332
jest.clearAllMocks();

__tests__/ResizableBox.test.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@ describe('render ResizableBox', () => {
2121
transformScale: 1,
2222
width: 50,
2323
};
24-
// ResizableBox passes children to its inner div, and Resizable spreads
25-
// children.props.children, so we wrap in array for it to be iterable.
26-
// Note: This causes a propType warning since ResizableBox expects a single element,
27-
// but it's necessary for the tests to work with React Testing Library.
28-
const children = [<span key="child" className="children" />];
24+
const children = <span className="children" />;
2925

3026
beforeEach(() => {
3127
jest.clearAllMocks();

examples/ExampleLayout.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import ResizableBox from '../lib/ResizableBox';
44
import 'style-loader!css-loader!../css/styles.css';
55
import 'style-loader!css-loader!./example.css';
66

7+
/* global __VERSION__, __GIT_TAG__, __GIT_COMMIT__ */
8+
79
const CustomResizeHandle = React.forwardRef((props, ref) => {
810
const {handleAxis, ...restProps} = props;
911
return (
@@ -64,6 +66,10 @@ export default class ExampleLayout extends React.Component<{}, {width: number, h
6466
render() {
6567
return (
6668
<div>
69+
<p className="version-info">
70+
react-resizable {__GIT_TAG__ || __VERSION__}
71+
{__GIT_COMMIT__ && <span> ({__GIT_COMMIT__})</span>}
72+
</p>
6773

6874
<h3>Statically Positioned Layout</h3>
6975
<div className="layoutRoot">

examples/example.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,11 @@
115115
}
116116
.custom-resize-handle-component {
117117
background-color: red;
118+
}
119+
120+
.version-info {
121+
font-family: monospace;
122+
font-size: 12px;
123+
color: #666;
124+
margin: 10px 20px;
118125
}

lib/Resizable.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ export default class Resizable extends React.Component<Props, void> {
201201
...p,
202202
className: `${className ? `${className} ` : ''}react-resizable`,
203203
children: [
204-
...children.props.children,
204+
...React.Children.toArray(children.props.children),
205205
...resizeHandles.map((handleAxis) => {
206206
// Create a ref to the handle so that `<DraggableCore>` doesn't have to use ReactDOM.findDOMNode().
207207
const ref = (this.handleRefs[handleAxis]) ?? (this.handleRefs[handleAxis] = React.createRef());

lib/propTypes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// @flow
2+
/* global Element */
23
import PropTypes from 'prop-types';
34
import {DraggableCore} from "react-draggable";
45
import type {Element as ReactElement, ElementConfig} from 'react';

webpack.config.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
11
const webpack = require('webpack');
22
const path = require('path');
3+
const {execFileSync} = require('child_process');
4+
const pkg = require('./package.json');
35

46
const isProduction = process.env.NODE_ENV === 'production';
7+
8+
// Get git info for version display
9+
function getGitInfo() {
10+
try {
11+
const commitHash = execFileSync('git', ['rev-parse', '--short', 'HEAD']).toString().trim();
12+
let tag = '';
13+
try {
14+
tag = execFileSync('git', ['describe', '--tags', '--abbrev=0']).toString().trim();
15+
} catch (e) {
16+
// No tags found
17+
}
18+
return {commitHash, tag};
19+
} catch (e) {
20+
return {commitHash: 'unknown', tag: ''};
21+
}
22+
}
23+
const gitInfo = getGitInfo();
524
const isDevelopment = !isProduction;
625

726
module.exports = {
@@ -51,5 +70,11 @@ module.exports = {
5170
plugins: [
5271
// Scope hoisting
5372
new webpack.optimize.ModuleConcatenationPlugin(),
73+
// Inject version info
74+
new webpack.DefinePlugin({
75+
__VERSION__: JSON.stringify(pkg.version),
76+
__GIT_TAG__: JSON.stringify(gitInfo.tag),
77+
__GIT_COMMIT__: JSON.stringify(gitInfo.commitHash),
78+
}),
5479
]
5580
};

0 commit comments

Comments
 (0)