baseui 10.9.2 → 10.10.0

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.
@@ -19,14 +19,6 @@ function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return
19
19
 
20
20
  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; }
21
21
 
22
- function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
23
-
24
- function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
25
-
26
- function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
27
-
28
- function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
29
-
30
22
  function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
31
23
 
32
24
  function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
@@ -39,50 +31,63 @@ function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(
39
31
 
40
32
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
41
33
 
42
- // https://github.com/Swizec/useDimensions
43
- function useDimensions() {
44
- var _React$useState = React.useState({}),
45
- _React$useState2 = _slicedToArray(_React$useState, 2),
46
- dimensions = _React$useState2[0],
47
- setDimensions = _React$useState2[1];
34
+ // Measures the column header + sampled data
35
+ function MeasureColumn(_ref) {
36
+ var sampleIndexes = _ref.sampleIndexes,
37
+ column = _ref.column,
38
+ columnIndex = _ref.columnIndex,
39
+ rows = _ref.rows,
40
+ isSelectable = _ref.isSelectable,
41
+ onLayout = _ref.onLayout;
48
42
 
49
- var _React$useState3 = React.useState(null),
50
- _React$useState4 = _slicedToArray(_React$useState3, 2),
51
- node = _React$useState4[0],
52
- setNode = _React$useState4[1];
43
+ var _useStyletron = (0, _index.useStyletron)(),
44
+ _useStyletron2 = _slicedToArray(_useStyletron, 1),
45
+ css = _useStyletron2[0];
53
46
 
54
- var ref = React.useCallback(function (node) {
55
- setNode(node);
56
- }, []);
47
+ var ref = (0, React.useRef)();
57
48
  React.useEffect(function () {
58
49
  if (typeof document !== 'undefined') {
59
- if (node) {
60
- window.requestAnimationFrame(function () {
61
- setDimensions(node.getBoundingClientRect());
62
- });
50
+ if (ref.current) {
51
+ onLayout(columnIndex, ref.current.getBoundingClientRect());
63
52
  }
64
53
  }
65
- }, [node]);
66
- return [ref, dimensions];
67
- }
68
-
69
- function ElementMeasurer(props) {
70
- var onDimensionsChange = props.onDimensionsChange;
71
-
72
- var _useDimensions = useDimensions(),
73
- _useDimensions2 = _slicedToArray(_useDimensions, 2),
74
- ref = _useDimensions2[0],
75
- dimensions = _useDimensions2[1];
76
-
77
- React.useEffect(function () {
78
- onDimensionsChange(dimensions);
79
- }, [dimensions, onDimensionsChange]);
80
- return /*#__PURE__*/React.cloneElement(props.item, {
81
- ref: ref
82
- });
54
+ }, []);
55
+ return /*#__PURE__*/React.createElement("div", {
56
+ ref: ref,
57
+ className: css({
58
+ display: 'flex',
59
+ flexDirection: 'column',
60
+ width: 'fit-content'
61
+ })
62
+ }, /*#__PURE__*/React.createElement(_headerCell.default, {
63
+ index: columnIndex,
64
+ isHovered: true,
65
+ isMeasured: true,
66
+ isSelectedAll: false,
67
+ isSelectedIndeterminate: false,
68
+ onMouseEnter: function onMouseEnter() {},
69
+ onMouseLeave: function onMouseLeave() {},
70
+ onSelectAll: function onSelectAll() {},
71
+ onSelectNone: function onSelectNone() {},
72
+ onSort: function onSort(i) {},
73
+ sortable: column.sortable,
74
+ sortDirection: null,
75
+ title: column.title,
76
+ isSelectable: isSelectable
77
+ }), sampleIndexes.map(function (rowIndex, i) {
78
+ var Cell = column.renderCell;
79
+ return /*#__PURE__*/React.createElement(Cell, {
80
+ key: "measure-".concat(i),
81
+ value: column.mapDataToValue(rows[rowIndex].data),
82
+ isSelectable: isSelectable,
83
+ isMeasured: true,
84
+ sortable: column.sortable,
85
+ x: 0,
86
+ y: rowIndex
87
+ });
88
+ }));
83
89
  }
84
90
 
85
- // sample size could likely be generated based on row count, to have higher confidence
86
91
  var MAX_SAMPLE_SIZE = 50;
87
92
 
88
93
  function generateSampleIndices(inputMin, inputMax, maxSamples) {
@@ -114,57 +119,46 @@ function generateSampleIndices(inputMin, inputMax, maxSamples) {
114
119
  return indices;
115
120
  }
116
121
 
117
- function MeasureColumnWidths(_ref) {
118
- var columns = _ref.columns,
119
- rows = _ref.rows,
120
- widths = _ref.widths,
121
- isSelectable = _ref.isSelectable,
122
- onWidthsChange = _ref.onWidthsChange;
122
+ function MeasureColumnWidths(_ref2) {
123
+ var columns = _ref2.columns,
124
+ rows = _ref2.rows,
125
+ widths = _ref2.widths,
126
+ isSelectable = _ref2.isSelectable,
127
+ onWidthsChange = _ref2.onWidthsChange;
123
128
 
124
- var _useStyletron = (0, _index.useStyletron)(),
125
- _useStyletron2 = _slicedToArray(_useStyletron, 1),
126
- css = _useStyletron2[0];
129
+ var _useStyletron3 = (0, _index.useStyletron)(),
130
+ _useStyletron4 = _slicedToArray(_useStyletron3, 1),
131
+ css = _useStyletron4[0];
127
132
 
128
- var measurementCount = React.useRef(0);
129
- var dimensionsCache = React.useRef(widths);
133
+ var widthMap = React.useMemo(function () {
134
+ return new Map();
135
+ }, []);
130
136
  var sampleSize = rows.length < MAX_SAMPLE_SIZE ? rows.length : MAX_SAMPLE_SIZE;
131
137
  var finishedMeasurementCount = (sampleSize + 1) * columns.length;
132
- var sampleRowIndicesByColumn = React.useMemo(function () {
133
- measurementCount.current = 0;
134
- dimensionsCache.current = widths;
135
- var indices = generateSampleIndices(0, rows.length - 1, sampleSize);
136
- return columns.map(function () {
137
- return indices;
138
- });
138
+ var sampleIndexes = React.useMemo(function () {
139
+ return generateSampleIndices(0, rows.length - 1, sampleSize);
139
140
  }, [columns, rows, widths, sampleSize]);
140
- var handleDimensionsChange = React.useCallback(function (columnIndex, rowIndex, dimensions) {
141
- if (dimensions.width === undefined) return;
141
+ var handleDimensionsChange = React.useCallback(function (columnIndex, dimensions) {
142
+ var nextWidth = Math.min(Math.max(columns[columnIndex].minWidth || 0, widthMap.get(columnIndex) || 0, dimensions.width + 1), columns[columnIndex].maxWidth || Infinity);
142
143
 
143
- if (columns[columnIndex] === undefined || dimensionsCache.current[columnIndex] === undefined) {
144
- return;
144
+ if (nextWidth !== widthMap.get(columnIndex)) {
145
+ widthMap.set(columnIndex, nextWidth);
145
146
  }
146
147
 
147
- measurementCount.current += 1;
148
- var nextWidth = Math.min(Math.max(columns[columnIndex].minWidth || 0, dimensionsCache.current[columnIndex], dimensions.width + 1), columns[columnIndex].maxWidth || Infinity);
149
-
150
- if (nextWidth !== dimensionsCache.current[columnIndex]) {
151
- var nextWidths = _toConsumableArray(dimensionsCache.current);
152
-
153
- nextWidths[columnIndex] = nextWidth;
154
- dimensionsCache.current = nextWidths;
155
- }
156
-
157
- if (measurementCount.current >= finishedMeasurementCount) {
158
- onWidthsChange(dimensionsCache.current);
148
+ if ( // Refresh at 100% of done
149
+ widthMap.size === columns.length || // ...50%
150
+ widthMap.size === Math.floor(columns.length / 2) || // ...25%
151
+ widthMap.size === Math.floor(columns.length / 4)) {
152
+ onWidthsChange(Array.from(widthMap.values()));
159
153
  }
160
154
  }, [columns, finishedMeasurementCount, onWidthsChange]);
161
155
  var hiddenStyle = css({
162
156
  position: 'absolute',
163
157
  overflow: 'hidden',
164
158
  height: 0
165
- });
159
+ }); // Remove the measurement nodes after we are done updating our column width
166
160
 
167
- if (measurementCount.current >= finishedMeasurementCount) {
161
+ if (widthMap.size === columns.length) {
168
162
  return null;
169
163
  }
170
164
 
@@ -175,45 +169,15 @@ function MeasureColumnWidths(_ref) {
175
169
  className: hiddenStyle,
176
170
  "aria-hidden": true,
177
171
  role: "none"
178
- }, sampleRowIndicesByColumn.map(function (rowIndices, columnIndex) {
179
- var Cell = columns[columnIndex].renderCell;
180
- return rowIndices.map(function (rowIndex) {
181
- return /*#__PURE__*/React.createElement(ElementMeasurer, {
182
- key: "measure-".concat(columnIndex, "-").concat(rowIndex),
183
- onDimensionsChange: function onDimensionsChange(dimensions) {
184
- return handleDimensionsChange(columnIndex, rowIndex, dimensions);
185
- },
186
- item: /*#__PURE__*/React.createElement(Cell, {
187
- value: columns[columnIndex].mapDataToValue(rows[rowIndex].data),
188
- isMeasured: true,
189
- onSelect: isSelectable && columnIndex === 0 ? function () {} : undefined,
190
- x: columnIndex,
191
- y: rowIndex
192
- })
193
- });
194
- });
195
- }), columns.map(function (column, columnIndex) {
196
- return /*#__PURE__*/React.createElement(ElementMeasurer, {
197
- key: "measure-column-".concat(columnIndex),
198
- onDimensionsChange: function onDimensionsChange(dimensions) {
199
- return handleDimensionsChange(columnIndex, -1, dimensions);
200
- },
201
- item: /*#__PURE__*/React.createElement(_headerCell.default, {
202
- index: columnIndex,
203
- isHovered: true,
204
- isMeasured: true,
205
- isSelectable: isSelectable && columnIndex === 0,
206
- isSelectedAll: false,
207
- isSelectedIndeterminate: false,
208
- onMouseEnter: function onMouseEnter() {},
209
- onMouseLeave: function onMouseLeave() {},
210
- onSelectAll: function onSelectAll() {},
211
- onSelectNone: function onSelectNone() {},
212
- onSort: function onSort(i) {},
213
- sortable: column.sortable,
214
- sortDirection: null,
215
- title: column.title
216
- })
172
+ }, columns.map(function (column, i) {
173
+ return /*#__PURE__*/React.createElement(MeasureColumn, {
174
+ key: column.title + i,
175
+ column: column,
176
+ rows: rows,
177
+ isSelectable: isSelectable,
178
+ onLayout: handleDimensionsChange,
179
+ columnIndex: i,
180
+ sampleIndexes: sampleIndexes
217
181
  });
218
182
  }))
219
183
  );
@@ -12,46 +12,71 @@ import {useStyletron} from '../styles/index.js';
12
12
 
13
13
  import HeaderCell from './header-cell.js';
14
14
  import type {ColumnT, RowT} from './types.js';
15
+ import {useRef} from 'react';
15
16
 
16
- // https://github.com/Swizec/useDimensions
17
- function useDimensions() {
18
- const [dimensions, setDimensions] = React.useState({});
19
- const [node, setNode] = React.useState(null);
17
+ // Measures the column header + sampled data
18
+ function MeasureColumn({
19
+ sampleIndexes,
20
+ column,
21
+ columnIndex,
22
+ rows,
23
+ isSelectable,
24
+ onLayout,
25
+ }) {
26
+ const [css] = useStyletron();
20
27
 
21
- const ref = React.useCallback(node => {
22
- setNode(node);
23
- }, []);
28
+ const ref = useRef();
24
29
 
25
30
  React.useEffect(() => {
26
31
  if (__BROWSER__) {
27
- if (node) {
28
- window.requestAnimationFrame(() => {
29
- setDimensions(node.getBoundingClientRect());
30
- });
32
+ if (ref.current) {
33
+ onLayout(columnIndex, ref.current.getBoundingClientRect());
31
34
  }
32
35
  }
33
- }, [node]);
34
-
35
- return [ref, dimensions];
36
- }
37
-
38
- type ElementMeasurerPropsT = {
39
- onDimensionsChange: (dimensions: {width: number}) => void,
40
- // eslint-disable-next-line flowtype/no-weak-types
41
- item: React.Element<any>,
42
- };
43
-
44
- function ElementMeasurer(props: ElementMeasurerPropsT) {
45
- const {onDimensionsChange} = props;
46
- const [ref, dimensions] = useDimensions();
47
-
48
- React.useEffect(() => {
49
- onDimensionsChange(dimensions);
50
- }, [dimensions, onDimensionsChange]);
36
+ }, []);
51
37
 
52
- return React.cloneElement(props.item, {ref});
38
+ return (
39
+ <div
40
+ ref={ref}
41
+ className={css({
42
+ display: 'flex',
43
+ flexDirection: 'column',
44
+ width: 'fit-content',
45
+ })}
46
+ >
47
+ <HeaderCell
48
+ index={columnIndex}
49
+ isHovered
50
+ isMeasured
51
+ isSelectedAll={false}
52
+ isSelectedIndeterminate={false}
53
+ onMouseEnter={() => {}}
54
+ onMouseLeave={() => {}}
55
+ onSelectAll={() => {}}
56
+ onSelectNone={() => {}}
57
+ onSort={i => {}}
58
+ sortable={column.sortable}
59
+ sortDirection={null}
60
+ title={column.title}
61
+ isSelectable={isSelectable}
62
+ />
63
+ {sampleIndexes.map((rowIndex, i) => {
64
+ const Cell = column.renderCell;
65
+ return (
66
+ <Cell
67
+ key={`measure-${i}`}
68
+ value={column.mapDataToValue(rows[rowIndex].data)}
69
+ isSelectable={isSelectable}
70
+ isMeasured
71
+ sortable={column.sortable}
72
+ x={0}
73
+ y={rowIndex}
74
+ />
75
+ );
76
+ })}
77
+ </div>
78
+ );
53
79
  }
54
-
55
80
  type MeasureColumnWidthsPropsT = {
56
81
  columns: ColumnT<>[],
57
82
  // if selectable, measure the first column with checkbox included
@@ -61,7 +86,6 @@ type MeasureColumnWidthsPropsT = {
61
86
  widths: number[],
62
87
  };
63
88
 
64
- // sample size could likely be generated based on row count, to have higher confidence
65
89
  const MAX_SAMPLE_SIZE = 50;
66
90
 
67
91
  function generateSampleIndices(inputMin, inputMax, maxSamples) {
@@ -96,51 +120,41 @@ export default function MeasureColumnWidths({
96
120
  }: MeasureColumnWidthsPropsT) {
97
121
  const [css] = useStyletron();
98
122
 
99
- const measurementCount = React.useRef(0);
100
- const dimensionsCache = React.useRef(widths);
123
+ const widthMap = React.useMemo(() => {
124
+ return new Map();
125
+ }, []);
101
126
 
102
127
  const sampleSize =
103
128
  rows.length < MAX_SAMPLE_SIZE ? rows.length : MAX_SAMPLE_SIZE;
104
129
  const finishedMeasurementCount = (sampleSize + 1) * columns.length;
105
130
 
106
- const sampleRowIndicesByColumn = React.useMemo<number[][]>(() => {
107
- measurementCount.current = 0;
108
- dimensionsCache.current = widths;
109
-
110
- const indices = generateSampleIndices(0, rows.length - 1, sampleSize);
111
- return columns.map(() => indices);
131
+ const sampleIndexes = React.useMemo<number[]>(() => {
132
+ return generateSampleIndices(0, rows.length - 1, sampleSize);
112
133
  }, [columns, rows, widths, sampleSize]);
113
134
 
114
135
  const handleDimensionsChange = React.useCallback(
115
- (columnIndex, rowIndex, dimensions) => {
116
- if (dimensions.width === undefined) return;
117
-
118
- if (
119
- columns[columnIndex] === undefined ||
120
- dimensionsCache.current[columnIndex] === undefined
121
- ) {
122
- return;
123
- }
124
-
125
- measurementCount.current += 1;
126
-
136
+ (columnIndex, dimensions) => {
127
137
  const nextWidth = Math.min(
128
138
  Math.max(
129
139
  columns[columnIndex].minWidth || 0,
130
- dimensionsCache.current[columnIndex],
140
+ widthMap.get(columnIndex) || 0,
131
141
  dimensions.width + 1,
132
142
  ),
133
143
  columns[columnIndex].maxWidth || Infinity,
134
144
  );
135
145
 
136
- if (nextWidth !== dimensionsCache.current[columnIndex]) {
137
- const nextWidths = [...dimensionsCache.current];
138
- nextWidths[columnIndex] = nextWidth;
139
- dimensionsCache.current = nextWidths;
146
+ if (nextWidth !== widthMap.get(columnIndex)) {
147
+ widthMap.set(columnIndex, nextWidth);
140
148
  }
141
-
142
- if (measurementCount.current >= finishedMeasurementCount) {
143
- onWidthsChange(dimensionsCache.current);
149
+ if (
150
+ // Refresh at 100% of done
151
+ widthMap.size === columns.length ||
152
+ // ...50%
153
+ widthMap.size === Math.floor(columns.length / 2) ||
154
+ // ...25%
155
+ widthMap.size === Math.floor(columns.length / 4)
156
+ ) {
157
+ onWidthsChange(Array.from(widthMap.values()));
144
158
  }
145
159
  },
146
160
  [columns, finishedMeasurementCount, onWidthsChange],
@@ -152,61 +166,27 @@ export default function MeasureColumnWidths({
152
166
  height: 0,
153
167
  });
154
168
 
155
- if (measurementCount.current >= finishedMeasurementCount) {
169
+ // Remove the measurement nodes after we are done updating our column width
170
+ if (widthMap.size === columns.length) {
156
171
  return null;
157
172
  }
158
173
 
159
174
  return (
160
175
  // eslint-disable-next-line jsx-a11y/role-supports-aria-props
161
176
  <div className={hiddenStyle} aria-hidden role="none">
162
- {sampleRowIndicesByColumn.map((rowIndices, columnIndex) => {
163
- const Cell = columns[columnIndex].renderCell;
164
- return rowIndices.map(rowIndex => (
165
- <ElementMeasurer
166
- key={`measure-${columnIndex}-${rowIndex}`}
167
- onDimensionsChange={dimensions =>
168
- handleDimensionsChange(columnIndex, rowIndex, dimensions)
169
- }
170
- item={
171
- <Cell
172
- value={columns[columnIndex].mapDataToValue(rows[rowIndex].data)}
173
- isMeasured
174
- onSelect={
175
- isSelectable && columnIndex === 0 ? () => {} : undefined
176
- }
177
- x={columnIndex}
178
- y={rowIndex}
179
- />
180
- }
177
+ {columns.map((column, i) => {
178
+ return (
179
+ <MeasureColumn
180
+ key={column.title + i}
181
+ column={column}
182
+ rows={rows}
183
+ isSelectable={isSelectable}
184
+ onLayout={handleDimensionsChange}
185
+ columnIndex={i}
186
+ sampleIndexes={sampleIndexes}
181
187
  />
182
- ));
188
+ );
183
189
  })}
184
- {columns.map((column, columnIndex) => (
185
- <ElementMeasurer
186
- key={`measure-column-${columnIndex}`}
187
- onDimensionsChange={dimensions =>
188
- handleDimensionsChange(columnIndex, -1, dimensions)
189
- }
190
- item={
191
- <HeaderCell
192
- index={columnIndex}
193
- isHovered
194
- isMeasured
195
- isSelectable={isSelectable && columnIndex === 0}
196
- isSelectedAll={false}
197
- isSelectedIndeterminate={false}
198
- onMouseEnter={() => {}}
199
- onMouseLeave={() => {}}
200
- onSelectAll={() => {}}
201
- onSelectNone={() => {}}
202
- onSort={i => {}}
203
- sortable={column.sortable}
204
- sortDirection={null}
205
- title={column.title}
206
- />
207
- }
208
- />
209
- ))}
210
190
  </div>
211
191
  );
212
192
  }