Skip to content

Commit 4d01780

Browse files
committed
Min/Max Constraints are working
1 parent 0482f21 commit 4d01780

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

TODO.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ Blocking issues to release with react-draggable v1
33
- [x] Basic Resizable functionality
44
- [x] Basic ResizableBox functionality
55
- [ ] Grid Snapping
6-
- [ ] Min/Max Constraints
6+
- [x] Min/Max Constraints
77
- [x] Aspect Ratios

lib/ResizableBox.jsx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ var PropTypes = React.PropTypes;
44
var Resizable = require('./Resizable');
55

66
// An example use of Resizable.
7+
type Size = {width: number, height: number};
8+
type ResizeData = {element: Element, size: Size};
79
var ResizableBox = module.exports = React.createClass({
810
displayName: 'ResizableBox',
911

@@ -30,28 +32,28 @@ var ResizableBox = module.exports = React.createClass({
3032
};
3133
},
3234

33-
onResize(event, {element, size}) {
35+
onResize(event: Event, {element, size}: ResizeData) {
3436
var {width, height} = size;
3537
var widthChanged = width !== this.state.width, heightChanged = height !== this.state.height;
3638
if (!widthChanged && !heightChanged) return;
3739

38-
if (this.props.lockAspectRatio) {
39-
[width, height] = this.preserveAspectRatio(width, height);
40-
}
40+
[width, height] = this.runConstraints(width, height);
4141

4242
this.setState({width, height}, () => {
43-
if (this.props.onResize) {
44-
this.props.onResize(event, {element, size: {width, height}});
45-
}
43+
if (this.props.onResize) {
44+
this.props.onResize(event, {element, size: {width, height}});
45+
}
4646
});
4747
},
4848

4949
// If you do this, be careful of constraints
50-
preserveAspectRatio(width: number, height: number) {
50+
runConstraints(width: number, height: number) {
5151
var [min, max] = [this.props.minConstraints, this.props.maxConstraints];
5252

53-
height = width / this.state.aspectRatio;
54-
width = height * this.state.aspectRatio;
53+
if (this.props.lockAspectRatio) {
54+
height = width / this.state.aspectRatio;
55+
width = height * this.state.aspectRatio;
56+
}
5557

5658
if (min) {
5759
width = Math.max(min[0], width);

0 commit comments

Comments
 (0)