Skip to content

Commit 36b32e4

Browse files
committed
Merge branch 'master' into ts-type-declarations
2 parents a080f55 + 43c84d0 commit 36b32e4

File tree

11 files changed

+141
-34
lines changed

11 files changed

+141
-34
lines changed

CHANGELOG.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,27 @@
22

33
## NEXT VERSION
44

5+
- feat: add `ignoreFunctionInColumnCompare` to solve closure problem in renderers
6+
- chore: skip unnecessary cloneElement in `renderElement`
7+
8+
## v1.10.9 (2020-08-13)
9+
10+
- fix: input loses focus on unmount
11+
12+
## v1.10.8 (2020-08-11)
13+
14+
- fix: scroll position would be reset to top if column becomes frozen
15+
16+
## v1.10.7 (2020-07-28)
17+
18+
- fix: `getTotalRowsHeight` could be different before/after render in dynamic mode on initial render
19+
20+
## v1.10.6 (2020-07-25)
21+
22+
- fix: `getTotalRowsHeight` could be different before/after render in dynamic mode
23+
24+
## v1.10.5 (2020-07-10)
25+
526
- chore: do not clear row height cache automatically
627

728
## v1.10.4 (2020-07-02)

README.md

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,32 @@ import 'react-base-table/styles.css'
3434

3535
Learn more at the [website](https://autodesk.github.io/react-base-table/)
3636

37-
**Make sure each item in `data` is unique by a key, the default key is `id`, you can customize it via `rowKey`**
37+
### unique key
3838

39-
**`key` is required for column definition or the column will be ignored**
39+
`key` is required for column definition or the column will be ignored
4040

41-
**`width` and `height`(or `maxHeight`) are required to display the table properly**
41+
Make sure each item in `data` is unique by a key, the default key is `id`, you can customize it via `rowKey`
42+
43+
### size
44+
45+
`width` is required for column definition, but in flex mode(`fixed={false}`), you can set `width={0}` and `flexGrow={1}` to achieve flexible column width, checkout the [Flex Column](https://autodesk.github.io/react-base-table/examples/flex-column) example
46+
47+
`width` and `height`(or `maxHeight`) are required to display the table properly
4248

4349
In the [examples](https://autodesk.github.io/react-base-table/examples)
4450
we are using a wrapper `const Table = props => <BaseTable width={700} height={400} {...props} />` to do that
4551

46-
If you want it responsive, you can use the [`AutoResizer`](https://autodesk.github.io/react-base-table/api/autoresizer) to make the table fill the container, checkout the [Auto Resize example](https://autodesk.github.io/react-base-table/examples/auto-resize)
52+
If you want it responsive, you can use the [`AutoResizer`](https://autodesk.github.io/react-base-table/api/autoresizer) to make the table fill the container, checkout the [Auto Resize](https://autodesk.github.io/react-base-table/examples/auto-resize) example
53+
54+
### closure problem in custom renderers
55+
56+
In practice we tend to write inline functions for custom renderers, which would make `shouldUpdateComponent` always true as the inline function will create a new instance on every re-render, to avoid "unnecessary" re-renders, **`BaseTable` ignores functions when comparing column definition by default**, it works well in most cases before, but if we use external data instead of reference state in custom renderers, we always get the staled initial value although the data has changed
57+
58+
It's recommended to inject the external data in column definition to solve the problem, like `<Column foo={foo} bar={bar} cellRenderer={({ column: { foo, bar }}) => { ... } } />`, the column definition will update on external data change, with this pattern we can easily move the custom renderers out of column definition for sharing, the downside is it would bloat the column definition and bug prone
59+
60+
Things getting worse with the introduction of React hooks, we use primitive state instead of `this.state`, so it's easy to encounter the closure problem, but with React hooks, we can easily memoize functions via `useCallback` or `useMemo`, so the implicit optimization could be replaced with user land optimization which is more intuitive, to turn off the implicit optimization, set `ignoreFunctionInColumnCompare` to `false` which is introduced since `v1.11.0`
61+
62+
Here is an [example](https://autodesk.github.io/react-base-table/playground#MYewdgzgLgBKA2BXAtpGBeGBzApmHATgIZQ4DCISqEAFAIwAMAlAFCiSwAmJRG2ehEjgAiPGghSQANDABMDZixY4AHgAcQBLjgBmRRPFg0mGAHwwA3ixhxw0GAG1QiMKQIyIOKBRduAunwASjhEwFAAdIieAMpQQjSKNuz2DgCWWGCaOABiLmGp4B5eAJIZWblg+eABmMGhEVE4sfFQBIg4rEl2sGlgAFY4YRRUYEVQxf2D3pSSNTB1YZExcaQ0evCenTAEXogEYDA01jYwADymxydnnKkAbjDQAJ7wOOgWFjBqRJw3YFgAXDAACwyG4QNTwIiPQEAch0LxUMJkfSiUFSOkeFFceCgsPBoRwAFoAEZeADuODwMJgAF8aRcrldTsTEFAoOAYOAyPBUsAANZvYxmB5eHzYgjiEC+QgwADUMDoTHpstOAHoWWzwAzGTZTpDSfBtTrdakwGpWZdjTYoI81K8AETAAAWgz5xJAKntlqtztdOE4b3SmR2FSqYBp3uNXKdRD+rwsOGFnnGZRDeTR4BoOHCcQIuAivv5-qVkauqqNxtKwcTOnTBQOptsI1syC+O1LZ1V+pwho7eqIBorOtOpvNUA7VxtdvQjpd-PdnonJ0LfP9gcmQxmqAjVqu0djuDeifQ5mTEwGm5GWZzRDzXnCK+LO935aX56mMFUrV43DiMHZTaSDAnC6KaqQZmAfZdgOPZDp2Ny3HBpwACoDi8MA6KkKj+sBPBvL+RA0jAQblHW4ATMMkgUK2t7xiRaaVBB9J9pRqBLhY4ScRI1AOAwfjPlaBEAOJeG4gomCetjSgQAnGs44rrhe0zNgA-FJ4owICLggZh+CcLJZZwbqrEHBxXFbpADh0PxMCvlapwmZYnEPhZEAOLINl2caDkWU55kjG5ADMnlGWcjlmS5AUOECIWRmqqHEi8FZqtqrARkAA) to demonstrate
4763

4864
## Browser Support
4965

@@ -117,7 +133,11 @@ We are using a advanced table component based on `BaseTable` internally, with mu
117133

118134
![AdvanceTable](screenshots/advance-table.png)
119135

120-
[In real products](https://blogs.autodesk.com/bim360-release-notes/2019/11/18/bim-360-cost-management-update-november-2019/)
136+
[In real products](https://blogs.autodesk.com/bim360-release-notes/2019/11/18/bim-360-cost-management-update-november-2019/)
137+
138+
## Development
139+
140+
We use `Yarn` as the package manager, checkout this repo and run `yarn` under both root folder and `website` folder in install packages, then run `yarn start` to start the demo site powered by `Gatsby`
121141

122142
## Contributing
123143

docs/get-started.md

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,32 @@ import 'react-base-table/styles.css'
2727
...
2828
```
2929

30-
**Make sure each item in `data` is unique by a key, the default key is `id`, you can customize it via `rowKey`**
30+
### unique key
3131

32-
**`width` and `height` are required to display the table properly**
32+
`key` is required for column definition or the column will be ignored
33+
34+
Make sure each item in `data` is unique by a key, the default key is `id`, you can customize it via `rowKey`
35+
36+
### size
37+
38+
`width` is required for column definition, but in flex mode(`fixed={false}`), you can set `width={0}` and `flexGrow={1}` to achieve flexible column width, checkout the [Flex Column](https://autodesk.github.io/react-base-table/examples/flex-column) example
39+
40+
`width` and `height`(or `maxHeight`) are required to display the table properly
3341

3442
In the [examples](https://autodesk.github.io/react-base-table/examples)
3543
we are using a wrapper `const Table = props => <BaseTable width={700} height={400} {...props} />` to do that
3644

37-
If you want it responsive, you can use the [`AutoResizer`](https://autodesk.github.io/react-base-table/api/autoresizer) to make the table fill the container, checkout the [Auto Resize example](https://autodesk.github.io/react-base-table/examples/auto-resize)
45+
If you want it responsive, you can use the [`AutoResizer`](https://autodesk.github.io/react-base-table/api/autoresizer) to make the table fill the container, checkout the [Auto Resize](https://autodesk.github.io/react-base-table/examples/auto-resize) example
46+
47+
### closure problem in custom renderers
48+
49+
In practice we tend to write inline functions for custom renderers, which would make `shouldUpdateComponent` always true as the inline function will create a new instance on every re-render, to avoid "unnecessary" re-renders, **`BaseTable` ignores functions when comparing column definition by default**, it works well in most cases before, but if we use external data instead of reference state in custom renderers, we always get the staled initial value although the data has changed
50+
51+
It's recommended to inject the external data in column definition to solve the problem, like `<Column foo={foo} bar={bar} cellRenderer={({ column: { foo, bar }}) => { ... } } />`, the column definition will update on external data change, with this pattern we can easily move the custom renderers out of column definition for sharing, the downside is it would bloat the column definition and bug prone
52+
53+
Things getting worse with the introduction of React hooks, we use primitive state instead of `this.state`, so it's easy to encounter the closure problem, but with React hooks, we can easily memoize functions via `useCallback` or `useMemo`, so the implicit optimization could be replaced with user land optimization which is more intuitive, to turn off the implicit optimization, set `ignoreFunctionInColumnCompare` to `false` which is introduced since `v1.11.0`
54+
55+
Here is an [example](https://autodesk.github.io/react-base-table/playground#MYewdgzgLgBKA2BXAtpGBeGBzApmHATgIZQ4DCISqEAFAIwAMAlAFCiSwAmJRG2ehEjgAiPGghSQANDABMDZixY4AHgAcQBLjgBmRRPFg0mGAHwwA3ixhxw0GAG1QiMKQIyIOKBRduAunwASjhEwFAAdIieAMpQQjSKNuz2DgCWWGCaOABiLmGp4B5eAJIZWblg+eABmMGhEVE4sfFQBIg4rEl2sGlgAFY4YRRUYEVQxf2D3pSSNTB1YZExcaQ0evCenTAEXogEYDA01jYwADymxydnnKkAbjDQAJ7wOOgWFjBqRJw3YFgAXDAACwyG4QNTwIiPQEAch0LxUMJkfSiUFSOkeFFceCgsPBoRwAFoAEZeADuODwMJgAF8aRcrldTsTEFAoOAYOAyPBUsAANZvYxmB5eHzYgjiEC+QgwADUMDoTHpstOAHoWWzwAzGTZTpDSfBtTrdakwGpWZdjTYoI81K8AETAAAWgz5xJAKntlqtztdOE4b3SmR2FSqYBp3uNXKdRD+rwsOGFnnGZRDeTR4BoOHCcQIuAivv5-qVkauqqNxtKwcTOnTBQOptsI1syC+O1LZ1V+pwho7eqIBorOtOpvNUA7VxtdvQjpd-PdnonJ0LfP9gcmQxmqAjVqu0djuDeifQ5mTEwGm5GWZzRDzXnCK+LO935aX56mMFUrV43DiMHZTaSDAnC6KaqQZmAfZdgOPZDp2Ny3HBpwACoDi8MA6KkKj+sBPBvL+RA0jAQblHW4ATMMkgUK2t7xiRaaVBB9J9pRqBLhY4ScRI1AOAwfjPlaBEAOJeG4gomCetjSgQAnGs44rrhe0zNgA-FJ4owICLggZh+CcLJZZwbqrEHBxXFbpADh0PxMCvlapwmZYnEPhZEAOLINl2caDkWU55kjG5ADMnlGWcjlmS5AUOECIWRmqqHEi8FZqtqrARkAA) to demonstrate
3856

3957
## Browser Support
4058

@@ -45,8 +63,8 @@ If you want it responsive, you can use the [`AutoResizer`](https://autodesk.gith
4563
## Playground
4664

4765
```jsx live
48-
const columns = generateColumns(10)
49-
const data = generateData(columns, 200)
66+
const columns = generateColumns(10);
67+
const data = generateData(columns, 200);
5068

51-
export default () => <Table columns={columns} data={data} />
69+
export default () => <Table columns={columns} data={data} />;
5270
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-base-table",
3-
"version": "1.10.4",
3+
"version": "1.10.9",
44
"description": "a react table component to display large data set with high performance and flexibility",
55
"main": "lib/index.js",
66
"module": "es/index.js",

src/BaseTable.js

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,21 @@ class BaseTable extends React.PureComponent {
9797
this._depthMap = {};
9898
return flattenOnKeys(tree, keys, this._depthMap, dataKey);
9999
});
100-
this._resetColumnManager = memoize((columns, fixed) => {
101-
this.columnManager.reset(columns, fixed);
100+
this._resetColumnManager = memoize(
101+
(columns, fixed) => {
102+
this.columnManager.reset(columns, fixed);
102103

103-
if (this.props.estimatedRowHeight && fixed) {
104-
if (!this.columnManager.hasLeftFrozenColumns()) {
105-
this._leftRowHeightMap = {};
106-
}
107-
if (!this.columnManager.hasRightFrozenColumns()) {
108-
this._rightRowHeightMap = {};
104+
if (this.props.estimatedRowHeight && fixed) {
105+
if (!this.columnManager.hasLeftFrozenColumns()) {
106+
this._leftRowHeightMap = {};
107+
}
108+
if (!this.columnManager.hasRightFrozenColumns()) {
109+
this._rightRowHeightMap = {};
110+
}
109111
}
110-
}
111-
}, isObjectEqual);
112+
},
113+
(newArgs, lastArgs) => isObjectEqual(newArgs, lastArgs, this.props.ignoreFunctionInColumnCompare)
114+
);
112115

113116
this._isResetting = false;
114117
this._resetIndex = null;
@@ -125,6 +128,7 @@ class BaseTable extends React.PureComponent {
125128
this._rowHeightMapBuffer = {};
126129
this._resetIndex = null;
127130
this.forceUpdateTable();
131+
this.forceUpdate();
128132
this._isResetting = false;
129133
}, 0);
130134

@@ -564,6 +568,7 @@ class BaseTable extends React.PureComponent {
564568
ref={this._setLeftTableRef}
565569
data={this._data}
566570
columns={this.columnManager.getLeftFrozenColumns()}
571+
initialScrollTop={this._scroll.scrollTop}
567572
width={columnsWidth + offset}
568573
height={containerHeight}
569574
headerHeight={headerHeight}
@@ -597,6 +602,7 @@ class BaseTable extends React.PureComponent {
597602
ref={this._setRightTableRef}
598603
data={this._data}
599604
columns={this.columnManager.getRightFrozenColumns()}
605+
initialScrollTop={this._scroll.scrollTop}
600606
width={columnsWidth + scrollbarWidth}
601607
height={containerHeight}
602608
headerHeight={headerHeight}
@@ -695,6 +701,7 @@ class BaseTable extends React.PureComponent {
695701
}
696702
// should be after `this._data` assigned
697703
this._calcScrollbarSizes();
704+
this._totalRowsHeight = this.getTotalRowsHeight();
698705

699706
const containerStyle = {
700707
...style,
@@ -732,7 +739,7 @@ class BaseTable extends React.PureComponent {
732739
}
733740

734741
componentDidUpdate(prevProps, prevState) {
735-
const { data, height, maxHeight } = this.props;
742+
const { data, height, maxHeight, estimatedRowHeight } = this.props;
736743
if (data !== prevProps.data) {
737744
this._lastScannedRowIndex = -1;
738745
this._hasDataChangedSinceEndReached = true;
@@ -742,6 +749,12 @@ class BaseTable extends React.PureComponent {
742749
this._maybeCallOnEndReached();
743750
}
744751
this._maybeScrollbarPresenceChange();
752+
753+
if (estimatedRowHeight) {
754+
if (this.getTotalRowsHeight() !== this._totalRowsHeight) {
755+
this.forceUpdate();
756+
}
757+
}
745758
}
746759

747760
_prefixClass(className) {
@@ -1026,6 +1039,7 @@ BaseTable.defaultProps = {
10261039
overscanRowCount: 1,
10271040
onEndReachedThreshold: 500,
10281041
getScrollbarSize: defaultGetScrollbarSize,
1042+
ignoreFunctionInColumnCompare: true,
10291043

10301044
onScroll: noop,
10311045
onRowsRendered: noop,
@@ -1276,6 +1290,10 @@ BaseTable.propTypes = {
12761290
* Each of the handlers is of the shape of `({ rowData, rowIndex, rowKey, event }) => object`
12771291
*/
12781292
rowEventHandlers: PropTypes.object,
1293+
/**
1294+
* whether to ignore function properties while comparing column definition
1295+
*/
1296+
ignoreFunctionInColumnCompare: PropTypes.bool,
12791297
/**
12801298
* A object for the custom components, like `ExpandIcon` and `SortIndicator`
12811299
*/

src/ColumnResizer.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,26 @@ export function addUserSelectStyles(doc) {
1313
styleEl = doc.createElement('style');
1414
styleEl.type = 'text/css';
1515
styleEl.id = 'react-draggable-style-el';
16-
styleEl.innerHTML = '.react-draggable-transparent-selection *::-moz-selection {background: transparent;}\n';
17-
styleEl.innerHTML += '.react-draggable-transparent-selection *::selection {background: transparent;}\n';
16+
styleEl.innerHTML = '.react-draggable-transparent-selection *::-moz-selection {all: inherit;}\n';
17+
styleEl.innerHTML += '.react-draggable-transparent-selection *::selection {all: inherit;}\n';
1818
doc.getElementsByTagName('head')[0].appendChild(styleEl);
1919
}
2020
if (doc.body) addClassName(doc.body, 'react-draggable-transparent-selection');
2121
}
2222

2323
export function removeUserSelectStyles(doc) {
24+
if (!doc) return;
2425
try {
25-
if (doc && doc.body) removeClassName(doc.body, 'react-draggable-transparent-selection');
26+
if (doc.body) removeClassName(doc.body, 'react-draggable-transparent-selection');
2627
if (doc.selection) {
2728
doc.selection.empty();
2829
} else {
29-
window.getSelection().removeAllRanges(); // remove selection caused by scroll
30+
// Remove selection caused by scroll, unless it's a focused input
31+
// (we use doc.defaultView in case we're in an iframe)
32+
const selection = (doc.defaultView || window).getSelection();
33+
if (selection && selection.type !== 'Caret') {
34+
selection.removeAllRanges();
35+
}
3036
}
3137
} catch (e) {
3238
// probably IE

src/utils.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22

33
export function renderElement(renderer, props) {
44
if (React.isValidElement(renderer)) {
5+
if (!props) return renderer;
56
return React.cloneElement(renderer, props);
67
} else if (typeof renderer === 'function') {
78
if (renderer.prototype && renderer.prototype.isReactComponent) {
@@ -26,7 +27,7 @@ export function normalizeColumns(elements) {
2627
return columns;
2728
}
2829

29-
export function isObjectEqual(objA, objB) {
30+
export function isObjectEqual(objA, objB, ignoreFunction = true) {
3031
if (objA === objB) return true;
3132
if (objA === null && objB === null) return true;
3233
if (objA === null || objB === null) return false;
@@ -38,13 +39,22 @@ export function isObjectEqual(objA, objB) {
3839

3940
for (let i = 0; i < keysA.length; i++) {
4041
const key = keysA[i];
42+
43+
if (key === '_owner' && objA.$$typeof) {
44+
// React-specific: avoid traversing React elements' _owner.
45+
// _owner contains circular references
46+
// and is not needed when comparing the actual elements (and not their owners)
47+
continue;
48+
}
49+
4150
const valueA = objA[key];
4251
const valueB = objB[key];
52+
const valueAType = typeof valueA;
4353

44-
if (typeof valueA !== typeof valueB) return false;
45-
if (typeof valueA === 'function') continue;
46-
if (typeof valueA === 'object') {
47-
if (!isObjectEqual(valueA, valueB)) return false;
54+
if (valueAType !== typeof valueB) return false;
55+
if (valueAType === 'function' && ignoreFunction) continue;
56+
if (valueAType === 'object') {
57+
if (!isObjectEqual(valueA, valueB, ignoreFunction)) return false;
4858
else continue;
4959
}
5060
if (valueA !== valueB) return false;

website/siteConfig.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ module.exports = {
5252
title: 'JSX Column',
5353
path: '/examples/jsx-column',
5454
},
55+
{
56+
title: 'Flex Column(column width in ratio)',
57+
path: '/examples/flex-column',
58+
},
5559
{
5660
title: 'Custom Cell',
5761
path: '/examples/custom-cell',

website/src/examples/custom-cell.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ export default class App extends React.Component {
175175
return (
176176
<Table
177177
fixed
178-
selectable
179178
columns={this.columns}
180179
data={data}
181180
sortBy={sortBy}

website/src/examples/dynamic-row-heights.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ export default class App extends React.Component {
193193
</button>
194194
<Table
195195
fixed
196-
selectable
197196
columns={
198197
!!this.state.toggle ? this.columns.slice(0, 4) : this.columns
199198
}

0 commit comments

Comments
 (0)