baseui 0.0.0-alpha-80026d1 → 0.0.0-alpha-47b62ee

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,7 +11,7 @@ var React = _interopRequireWildcard(require("react"));
11
11
 
12
12
  var _reactWindow = require("react-window");
13
13
 
14
- var _reactVirtualizedAutoSizer = _interopRequireDefault(require("react-virtualized-auto-sizer"));
14
+ var _reactVirtualizedAutoSizer = _interopRequireDefault(require("./react-virtualized-auto-sizer.js"));
15
15
 
16
16
  var _index = require("../button/index.js");
17
17
 
@@ -568,24 +568,27 @@ function DataTable(_ref2) {
568
568
  }
569
569
 
570
570
  return rowHeight;
571
- }, [rowHeight]);
572
- var gridRef = React.useRef(null); // // We use state for our ref, to allow hooks to update when the ref changes.
573
- // // eslint-disable-next-line flowtype/no-weak-types
574
- // const [gridRef, setGridRef] = React.useState<?VariableSizeGrid<any>>(null);
571
+ }, [rowHeight]); // We use state for our ref, to allow hooks to update when the ref changes.
572
+ // eslint-disable-next-line flowtype/no-weak-types
575
573
 
576
- var _React$useState7 = React.useState(columns.map(function () {
577
- return 0;
578
- })),
574
+ var _React$useState7 = React.useState(null),
579
575
  _React$useState8 = _slicedToArray(_React$useState7, 2),
580
- measuredWidths = _React$useState8[0],
581
- setMeasuredWidths = _React$useState8[1];
576
+ gridRef = _React$useState8[0],
577
+ setGridRef = _React$useState8[1];
582
578
 
583
579
  var _React$useState9 = React.useState(columns.map(function () {
584
580
  return 0;
585
581
  })),
586
582
  _React$useState10 = _slicedToArray(_React$useState9, 2),
587
- resizeDeltas = _React$useState10[0],
588
- setResizeDeltas = _React$useState10[1];
583
+ measuredWidths = _React$useState10[0],
584
+ setMeasuredWidths = _React$useState10[1];
585
+
586
+ var _React$useState11 = React.useState(columns.map(function () {
587
+ return 0;
588
+ })),
589
+ _React$useState12 = _slicedToArray(_React$useState11, 2),
590
+ resizeDeltas = _React$useState12[0],
591
+ setResizeDeltas = _React$useState12[1];
589
592
 
590
593
  React.useEffect(function () {
591
594
  setMeasuredWidths(function (prev) {
@@ -600,11 +603,11 @@ function DataTable(_ref2) {
600
603
  });
601
604
  }, [columns]);
602
605
  var resetAfterColumnIndex = React.useCallback(function (columnIndex) {
603
- if (gridRef.current) {
606
+ if (gridRef) {
604
607
  // $FlowFixMe trigger react-window to layout the elements again
605
- gridRef.current.resetAfterColumnIndex(columnIndex, true);
608
+ gridRef.resetAfterColumnIndex(columnIndex, true);
606
609
  }
607
- }, [gridRef.current]);
610
+ }, [gridRef]);
608
611
  var handleWidthsChange = React.useCallback(function (nextWidths) {
609
612
  setMeasuredWidths(nextWidths);
610
613
  resetAfterColumnIndex(0);
@@ -617,20 +620,20 @@ function DataTable(_ref2) {
617
620
  resetAfterColumnIndex(columnIndex);
618
621
  }, [setResizeDeltas, resetAfterColumnIndex]);
619
622
 
620
- var _React$useState11 = React.useState(0),
621
- _React$useState12 = _slicedToArray(_React$useState11, 2),
622
- scrollLeft = _React$useState12[0],
623
- setScrollLeft = _React$useState12[1];
624
-
625
- var _React$useState13 = React.useState(false),
623
+ var _React$useState13 = React.useState(0),
626
624
  _React$useState14 = _slicedToArray(_React$useState13, 2),
627
- isScrollingX = _React$useState14[0],
628
- setIsScrollingX = _React$useState14[1];
625
+ scrollLeft = _React$useState14[0],
626
+ setScrollLeft = _React$useState14[1];
629
627
 
630
628
  var _React$useState15 = React.useState(false),
631
629
  _React$useState16 = _slicedToArray(_React$useState15, 2),
632
- recentlyScrolledX = _React$useState16[0],
633
- setRecentlyScrolledX = _React$useState16[1];
630
+ isScrollingX = _React$useState16[0],
631
+ setIsScrollingX = _React$useState16[1];
632
+
633
+ var _React$useState17 = React.useState(false),
634
+ _React$useState18 = _slicedToArray(_React$useState17, 2),
635
+ recentlyScrolledX = _React$useState18[0],
636
+ setRecentlyScrolledX = _React$useState18[1];
634
637
 
635
638
  React.useLayoutEffect(function () {
636
639
  if (recentlyScrolledX !== isScrollingX) {
@@ -752,19 +755,18 @@ function DataTable(_ref2) {
752
755
  return result;
753
756
  }, [sortedIndices, filteredIndices, onIncludedRowsChange, allRows]);
754
757
 
755
- var _React$useState17 = React.useState(0),
756
- _React$useState18 = _slicedToArray(_React$useState17, 2),
757
- browserScrollbarWidth = _React$useState18[0],
758
- setBrowserScrollbarWidth = _React$useState18[1];
758
+ var _React$useState19 = React.useState(0),
759
+ _React$useState20 = _slicedToArray(_React$useState19, 2),
760
+ browserScrollbarWidth = _React$useState20[0],
761
+ setBrowserScrollbarWidth = _React$useState20[1];
759
762
 
760
763
  var normalizedWidths = React.useMemo(function () {
761
764
  var resizedWidths = measuredWidths.map(function (w, i) {
762
765
  return Math.floor(w) + Math.floor(resizeDeltas[i]);
763
766
  });
764
767
 
765
- if (gridRef.current) {
766
- // $FlowFixMe
767
- var gridProps = gridRef.current.props;
768
+ if (gridRef) {
769
+ var gridProps = gridRef.props;
768
770
  var isContentTallerThanContainer = false;
769
771
  var visibleRowHeight = 0;
770
772
 
@@ -801,8 +803,7 @@ function DataTable(_ref2) {
801
803
  }
802
804
 
803
805
  return resizedWidths;
804
- }, [// gridRef,
805
- measuredWidths, resizeDeltas, browserScrollbarWidth, rows.length, columns]);
806
+ }, [gridRef, measuredWidths, resizeDeltas, browserScrollbarWidth, rows.length, columns]);
806
807
  var isSelectable = batchActions ? !!batchActions.length : false;
807
808
  var isSelectedAll = React.useMemo(function () {
808
809
  if (!selectedRowIds) {
@@ -846,23 +847,23 @@ function DataTable(_ref2) {
846
847
  }
847
848
  }, [onSort]);
848
849
 
849
- var _React$useState19 = React.useState(-1),
850
- _React$useState20 = _slicedToArray(_React$useState19, 2),
851
- columnHighlightIndex = _React$useState20[0],
852
- setColumnHighlightIndex = _React$useState20[1];
853
-
854
850
  var _React$useState21 = React.useState(-1),
855
851
  _React$useState22 = _slicedToArray(_React$useState21, 2),
856
- rowHighlightIndex = _React$useState22[0],
857
- setRowHighlightIndex = _React$useState22[1];
852
+ columnHighlightIndex = _React$useState22[0],
853
+ setColumnHighlightIndex = _React$useState22[1];
854
+
855
+ var _React$useState23 = React.useState(-1),
856
+ _React$useState24 = _slicedToArray(_React$useState23, 2),
857
+ rowHighlightIndex = _React$useState24[0],
858
+ setRowHighlightIndex = _React$useState24[1];
858
859
 
859
860
  function handleRowHighlightIndexChange(nextIndex) {
860
861
  setRowHighlightIndex(nextIndex);
861
862
 
862
- if (gridRef.current) {
863
+ if (gridRef) {
863
864
  if (nextIndex >= 0) {
864
865
  // $FlowFixMe - unable to get react-window types
865
- gridRef.current.scrollToItem({
866
+ gridRef.scrollToItem({
866
867
  rowIndex: nextIndex
867
868
  });
868
869
  }
@@ -953,7 +954,7 @@ function DataTable(_ref2) {
953
954
  }
954
955
  }, /*#__PURE__*/React.createElement(_reactWindow.VariableSizeGrid // eslint-disable-next-line flowtype/no-weak-types
955
956
  , {
956
- ref: gridRef,
957
+ ref: setGridRef,
957
958
  overscanRowCount: 10,
958
959
  overscanColumnCount: 5,
959
960
  innerElementType: InnerTableElement,
@@ -8,7 +8,7 @@ LICENSE file in the root directory of this source tree.
8
8
 
9
9
  import * as React from 'react';
10
10
  import {VariableSizeGrid} from 'react-window';
11
- import AutoSizer from 'react-virtualized-auto-sizer';
11
+ import AutoSizer from './react-virtualized-auto-sizer.js';
12
12
 
13
13
  import {
14
14
  Button,
@@ -699,12 +699,9 @@ export function DataTable({
699
699
  [rowHeight],
700
700
  );
701
701
 
702
- const gridRef = React.useRef<typeof VariableSizeGrid | null>(null);
703
-
704
- // // We use state for our ref, to allow hooks to update when the ref changes.
705
- // // eslint-disable-next-line flowtype/no-weak-types
706
- // const [gridRef, setGridRef] = React.useState<?VariableSizeGrid<any>>(null);
707
-
702
+ // We use state for our ref, to allow hooks to update when the ref changes.
703
+ // eslint-disable-next-line flowtype/no-weak-types
704
+ const [gridRef, setGridRef] = React.useState<?VariableSizeGrid<any>>(null);
708
705
  const [measuredWidths, setMeasuredWidths] = React.useState(
709
706
  columns.map(() => 0),
710
707
  );
@@ -720,12 +717,12 @@ export function DataTable({
720
717
 
721
718
  const resetAfterColumnIndex = React.useCallback(
722
719
  columnIndex => {
723
- if (gridRef.current) {
720
+ if (gridRef) {
724
721
  // $FlowFixMe trigger react-window to layout the elements again
725
- gridRef.current.resetAfterColumnIndex(columnIndex, true);
722
+ gridRef.resetAfterColumnIndex(columnIndex, true);
726
723
  }
727
724
  },
728
- [gridRef.current],
725
+ [gridRef],
729
726
  );
730
727
  const handleWidthsChange = React.useCallback(
731
728
  nextWidths => {
@@ -854,9 +851,8 @@ export function DataTable({
854
851
  const resizedWidths = measuredWidths.map(
855
852
  (w, i) => Math.floor(w) + Math.floor(resizeDeltas[i]),
856
853
  );
857
- if (gridRef.current) {
858
- // $FlowFixMe
859
- const gridProps = gridRef.current.props;
854
+ if (gridRef) {
855
+ const gridProps = gridRef.props;
860
856
 
861
857
  let isContentTallerThanContainer = false;
862
858
  let visibleRowHeight = 0;
@@ -893,7 +889,7 @@ export function DataTable({
893
889
  }
894
890
  return resizedWidths;
895
891
  }, [
896
- // gridRef,
892
+ gridRef,
897
893
  measuredWidths,
898
894
  resizeDeltas,
899
895
  browserScrollbarWidth,
@@ -956,10 +952,10 @@ export function DataTable({
956
952
 
957
953
  function handleRowHighlightIndexChange(nextIndex) {
958
954
  setRowHighlightIndex(nextIndex);
959
- if (gridRef.current) {
955
+ if (gridRef) {
960
956
  if (nextIndex >= 0) {
961
957
  // $FlowFixMe - unable to get react-window types
962
- gridRef.current.scrollToItem({rowIndex: nextIndex});
958
+ gridRef.scrollToItem({rowIndex: nextIndex});
963
959
  }
964
960
  if (onRowHighlightChange) {
965
961
  onRowHighlightChange(nextIndex, rows[nextIndex - 1]);
@@ -1059,7 +1055,7 @@ export function DataTable({
1059
1055
  >
1060
1056
  <VariableSizeGrid
1061
1057
  // eslint-disable-next-line flowtype/no-weak-types
1062
- ref={(gridRef: any)}
1058
+ ref={(setGridRef: any)}
1063
1059
  overscanRowCount={10}
1064
1060
  overscanColumnCount={5}
1065
1061
  innerElementType={InnerTableElement}
@@ -0,0 +1,200 @@
1
+ "use strict";
2
+
3
+ function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.default = void 0;
9
+
10
+ var React = _interopRequireWildcard(require("react"));
11
+
12
+ var _reactVirtualizedDetectElementResize = _interopRequireDefault(require("./react-virtualized-detect-element-resize.js"));
13
+
14
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
+
16
+ function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
17
+
18
+ function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
19
+
20
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
21
+
22
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
23
+
24
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
25
+
26
+ 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); } }
27
+
28
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
29
+
30
+ function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
31
+
32
+ function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
33
+
34
+ function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
35
+
36
+ function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
37
+
38
+ function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
39
+
40
+ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
41
+
42
+ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
43
+
44
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
45
+
46
+ var AutoSizer = /*#__PURE__*/function (_React$PureComponent) {
47
+ _inherits(AutoSizer, _React$PureComponent);
48
+
49
+ var _super = _createSuper(AutoSizer);
50
+
51
+ function AutoSizer() {
52
+ var _this;
53
+
54
+ _classCallCheck(this, AutoSizer);
55
+
56
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
57
+ args[_key] = arguments[_key];
58
+ }
59
+
60
+ _this = _super.call.apply(_super, [this].concat(args));
61
+
62
+ _defineProperty(_assertThisInitialized(_this), "state", {
63
+ height: _this.props.defaultHeight || 0,
64
+ width: _this.props.defaultWidth || 0
65
+ });
66
+
67
+ _defineProperty(_assertThisInitialized(_this), "_parentNode", void 0);
68
+
69
+ _defineProperty(_assertThisInitialized(_this), "_autoSizer", void 0);
70
+
71
+ _defineProperty(_assertThisInitialized(_this), "_detectElementResize", void 0);
72
+
73
+ _defineProperty(_assertThisInitialized(_this), "_onResize", function () {
74
+ var _this$props = _this.props,
75
+ disableHeight = _this$props.disableHeight,
76
+ disableWidth = _this$props.disableWidth,
77
+ onResize = _this$props.onResize;
78
+
79
+ if (_this._parentNode) {
80
+ // Guard against AutoSizer component being removed from the DOM immediately after being added.
81
+ // This can result in invalid style values which can result in NaN values if we don't handle them.
82
+ // See issue #150 for more context.
83
+ console.log(_this._parentNode.offsetWidth, _this._parentNode.offsetHeight);
84
+ var height = _this._parentNode.offsetHeight || 0;
85
+ var width = _this._parentNode.offsetWidth || 0;
86
+ var style = window.getComputedStyle(_this._parentNode) || {};
87
+ var paddingLeft = parseInt(style.paddingLeft, 10) || 0;
88
+ var paddingRight = parseInt(style.paddingRight, 10) || 0;
89
+ var paddingTop = parseInt(style.paddingTop, 10) || 0;
90
+ var paddingBottom = parseInt(style.paddingBottom, 10) || 0;
91
+ var newHeight = height - paddingTop - paddingBottom;
92
+ var newWidth = width - paddingLeft - paddingRight;
93
+
94
+ if (!disableHeight && _this.state.height !== newHeight || !disableWidth && _this.state.width !== newWidth) {
95
+ _this.setState({
96
+ height: height - paddingTop - paddingBottom,
97
+ width: width - paddingLeft - paddingRight
98
+ });
99
+
100
+ onResize({
101
+ height: height,
102
+ width: width
103
+ });
104
+ }
105
+ }
106
+ });
107
+
108
+ _defineProperty(_assertThisInitialized(_this), "_setRef", function (autoSizer) {
109
+ _this._autoSizer = autoSizer;
110
+ });
111
+
112
+ return _this;
113
+ }
114
+
115
+ _createClass(AutoSizer, [{
116
+ key: "componentDidMount",
117
+ value: function componentDidMount() {
118
+ var nonce = this.props.nonce;
119
+
120
+ if (this._autoSizer && this._autoSizer.parentNode && this._autoSizer.parentNode.ownerDocument && this._autoSizer.parentNode.ownerDocument.defaultView && this._autoSizer.parentNode instanceof this._autoSizer.parentNode.ownerDocument.defaultView.HTMLElement) {
121
+ // Delay access of parentNode until mount.
122
+ // This handles edge-cases where the component has already been unmounted before its ref has been set,
123
+ // As well as libraries like react-lite which have a slightly different lifecycle.
124
+ this._parentNode = this._autoSizer.parentNode; // Defer requiring resize handler in order to support server-side rendering.
125
+ // See issue #41
126
+
127
+ this._detectElementResize = (0, _reactVirtualizedDetectElementResize.default)(nonce);
128
+
129
+ this._detectElementResize.addResizeListener(this._parentNode, this._onResize);
130
+
131
+ this._onResize();
132
+ }
133
+ }
134
+ }, {
135
+ key: "componentWillUnmount",
136
+ value: function componentWillUnmount() {
137
+ if (this._detectElementResize && this._parentNode) {
138
+ this._detectElementResize.removeResizeListener(this._parentNode, this._onResize);
139
+ }
140
+ }
141
+ }, {
142
+ key: "render",
143
+ value: function render() {
144
+ var _this$props2 = this.props,
145
+ children = _this$props2.children,
146
+ className = _this$props2.className,
147
+ disableHeight = _this$props2.disableHeight,
148
+ disableWidth = _this$props2.disableWidth,
149
+ style = _this$props2.style;
150
+ var _this$state = this.state,
151
+ height = _this$state.height,
152
+ width = _this$state.width; // Outer div should not force width/height since that may prevent containers from shrinking.
153
+ // Inner component should overflow and use calculated width/height.
154
+ // See issue #68 for more information.
155
+
156
+ var outerStyle = {
157
+ overflow: 'visible'
158
+ };
159
+ var childParams = {}; // Avoid rendering children before the initial measurements have been collected.
160
+ // At best this would just be wasting cycles.
161
+
162
+ var bailoutOnChildren = false;
163
+
164
+ if (!disableHeight) {
165
+ if (height === 0) {
166
+ bailoutOnChildren = true;
167
+ }
168
+
169
+ outerStyle.height = 0;
170
+ childParams.height = height;
171
+ }
172
+
173
+ if (!disableWidth) {
174
+ if (width === 0) {
175
+ bailoutOnChildren = true;
176
+ }
177
+
178
+ outerStyle.width = 0;
179
+ childParams.width = width;
180
+ }
181
+
182
+ return /*#__PURE__*/React.createElement("div", {
183
+ className: className,
184
+ ref: this._setRef,
185
+ style: _objectSpread(_objectSpread({}, outerStyle), style)
186
+ }, children(childParams));
187
+ }
188
+ }]);
189
+
190
+ return AutoSizer;
191
+ }(React.PureComponent);
192
+
193
+ exports.default = AutoSizer;
194
+
195
+ _defineProperty(AutoSizer, "defaultProps", {
196
+ onResize: function onResize() {},
197
+ disableHeight: false,
198
+ disableWidth: false,
199
+ style: {}
200
+ });
@@ -0,0 +1,201 @@
1
+ // https://github.com/bvaughn/react-virtualized-auto-sizer
2
+ // MIT license https://github.com/bvaughn/react-virtualized-auto-sizer/blob/1275e69264dd960d15aca42fea8bd73194672778/package.json#L11
3
+ // @flow
4
+
5
+ import * as React from 'react';
6
+ import createDetectElementResize from './react-virtualized-detect-element-resize.js';
7
+
8
+ type Size = {
9
+ height: number,
10
+ width: number,
11
+ };
12
+
13
+ type Props = {
14
+ /** Function responsible for rendering children.*/
15
+ children: Size => React.Element<*>,
16
+
17
+ /** Optional custom CSS class name to attach to root AutoSizer element. */
18
+ className?: string,
19
+
20
+ /** Default height to use for initial render; useful for SSR */
21
+ defaultHeight?: number,
22
+
23
+ /** Default width to use for initial render; useful for SSR */
24
+ defaultWidth?: number,
25
+
26
+ /** Disable dynamic :height property */
27
+ disableHeight: boolean,
28
+
29
+ /** Disable dynamic :width property */
30
+ disableWidth: boolean,
31
+
32
+ /** Nonce of the inlined stylesheet for Content Security Policy */
33
+ nonce?: string,
34
+
35
+ /** Callback to be invoked on-resize */
36
+ onResize: Size => void,
37
+
38
+ /** Optional inline style */
39
+ style: ?Object,
40
+ };
41
+
42
+ type State = {
43
+ height: number,
44
+ width: number,
45
+ };
46
+
47
+ type ResizeHandler = (element: HTMLElement, onResize: () => void) => void;
48
+
49
+ type DetectElementResize = {
50
+ addResizeListener: ResizeHandler,
51
+ removeResizeListener: ResizeHandler,
52
+ };
53
+
54
+ export default class AutoSizer extends React.PureComponent<Props, State> {
55
+ static defaultProps = {
56
+ onResize: () => {},
57
+ disableHeight: false,
58
+ disableWidth: false,
59
+ style: {},
60
+ };
61
+
62
+ state = {
63
+ height: this.props.defaultHeight || 0,
64
+ width: this.props.defaultWidth || 0,
65
+ };
66
+
67
+ _parentNode: ?HTMLElement;
68
+ _autoSizer: ?HTMLElement;
69
+ _detectElementResize: DetectElementResize;
70
+
71
+ componentDidMount() {
72
+ const {nonce} = this.props;
73
+ if (
74
+ this._autoSizer &&
75
+ this._autoSizer.parentNode &&
76
+ this._autoSizer.parentNode.ownerDocument &&
77
+ this._autoSizer.parentNode.ownerDocument.defaultView &&
78
+ this._autoSizer.parentNode instanceof
79
+ this._autoSizer.parentNode.ownerDocument.defaultView.HTMLElement
80
+ ) {
81
+ // Delay access of parentNode until mount.
82
+ // This handles edge-cases where the component has already been unmounted before its ref has been set,
83
+ // As well as libraries like react-lite which have a slightly different lifecycle.
84
+ this._parentNode = this._autoSizer.parentNode;
85
+
86
+ // Defer requiring resize handler in order to support server-side rendering.
87
+ // See issue #41
88
+ this._detectElementResize = createDetectElementResize(nonce);
89
+ this._detectElementResize.addResizeListener(
90
+ this._parentNode,
91
+ this._onResize,
92
+ );
93
+
94
+ this._onResize();
95
+ }
96
+ }
97
+
98
+ componentWillUnmount() {
99
+ if (this._detectElementResize && this._parentNode) {
100
+ this._detectElementResize.removeResizeListener(
101
+ this._parentNode,
102
+ this._onResize,
103
+ );
104
+ }
105
+ }
106
+
107
+ render() {
108
+ const {
109
+ children,
110
+ className,
111
+ disableHeight,
112
+ disableWidth,
113
+ style,
114
+ } = this.props;
115
+ const {height, width} = this.state;
116
+
117
+ // Outer div should not force width/height since that may prevent containers from shrinking.
118
+ // Inner component should overflow and use calculated width/height.
119
+ // See issue #68 for more information.
120
+ const outerStyle: Object = {overflow: 'visible'};
121
+ const childParams: Object = {};
122
+
123
+ // Avoid rendering children before the initial measurements have been collected.
124
+ // At best this would just be wasting cycles.
125
+ let bailoutOnChildren = false;
126
+
127
+ if (!disableHeight) {
128
+ if (height === 0) {
129
+ bailoutOnChildren = true;
130
+ }
131
+ outerStyle.height = 0;
132
+ childParams.height = height;
133
+ }
134
+
135
+ if (!disableWidth) {
136
+ if (width === 0) {
137
+ bailoutOnChildren = true;
138
+ }
139
+ outerStyle.width = 0;
140
+ childParams.width = width;
141
+ }
142
+
143
+ return (
144
+ <div
145
+ className={className}
146
+ ref={this._setRef}
147
+ style={{
148
+ ...outerStyle,
149
+ ...style,
150
+ }}
151
+ >
152
+ {children(childParams)}
153
+ {/* {!bailoutOnChildren && children(childParams)} */}
154
+ </div>
155
+ );
156
+ }
157
+
158
+ _onResize = () => {
159
+ const {disableHeight, disableWidth, onResize} = this.props;
160
+
161
+ if (this._parentNode) {
162
+ // Guard against AutoSizer component being removed from the DOM immediately after being added.
163
+ // This can result in invalid style values which can result in NaN values if we don't handle them.
164
+ // See issue #150 for more context.
165
+
166
+ console.log(this._parentNode.offsetWidth, this._parentNode.offsetHeight);
167
+
168
+ const height = this._parentNode.offsetHeight || 0;
169
+ const width = this._parentNode.offsetWidth || 0;
170
+
171
+ const style = window.getComputedStyle(this._parentNode) || {};
172
+ const paddingLeft = parseInt(style.paddingLeft, 10) || 0;
173
+ const paddingRight = parseInt(style.paddingRight, 10) || 0;
174
+ const paddingTop = parseInt(style.paddingTop, 10) || 0;
175
+ const paddingBottom = parseInt(style.paddingBottom, 10) || 0;
176
+
177
+ const newHeight = height - paddingTop - paddingBottom;
178
+ const newWidth = width - paddingLeft - paddingRight;
179
+
180
+ if (
181
+ (!disableHeight && this.state.height !== newHeight) ||
182
+ (!disableWidth && this.state.width !== newWidth)
183
+ ) {
184
+ this.setState({
185
+ height: height - paddingTop - paddingBottom,
186
+ width: width - paddingLeft - paddingRight,
187
+ });
188
+
189
+ onResize({height, width});
190
+ }
191
+ }
192
+ };
193
+
194
+ _setRef = (autoSizer: ?HTMLElement) => {
195
+ this._autoSizer = autoSizer;
196
+ };
197
+ }
198
+
199
+ declare var __DEV__: boolean;
200
+ declare var __NODE__: boolean;
201
+ declare var __BROWSER__: boolean;