forked from esnet/react-timeseries-charts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResizable.js
103 lines (82 loc) · 8.01 KB
/
Resizable.js
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
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require("react");
var _react2 = _interopRequireDefault(_react);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /**
* Copyright (c) 2015-present, The Regents of the University of California,
* through Lawrence Berkeley National Laboratory (subject to receipt
* of any required approvals from the U.S. Dept. of Energy).
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* This takes a single child and inserts a prop 'width' on it that is the
* current width of the this container. This is handy if you want to surround
* a chart or other svg diagram and have this drive the chart width.
*/
var Resizable = function (_React$Component) {
_inherits(Resizable, _React$Component);
function Resizable(props) {
_classCallCheck(this, Resizable);
var _this = _possibleConstructorReturn(this, (Resizable.__proto__ || Object.getPrototypeOf(Resizable)).call(this, props));
_this.state = { width: 0 };
return _this;
}
_createClass(Resizable, [{
key: "componentDidMount",
value: function componentDidMount() {
var _this2 = this;
window.addEventListener("resize", function () {
return _this2.handleResize();
});
this.handleResize();
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
var _this3 = this;
window.removeEventListener("resize", function () {
return _this3.handleResize();
});
}
}, {
key: "handleResize",
value: function handleResize() {
if (this.container) {
this.setState({
width: this.container.offsetWidth
});
}
}
}, {
key: "render",
value: function render() {
var _this4 = this;
var child = _react2.default.Children.only(this.props.children);
var childElement = this.state.width ? _react2.default.cloneElement(child, { width: this.state.width }) : null;
return _react2.default.createElement(
"div",
_extends({
ref: function ref(c) {
_this4.container = c;
}
}, this.props),
childElement
);
}
}]);
return Resizable;
}(_react2.default.Component);
exports.default = Resizable;
Resizable.propTypes = {
children: _react2.default.PropTypes.node
};