fixed-data-table-2 icon indicating copy to clipboard operation
fixed-data-table-2 copied to clipboard

Need a way to keep the selected row from unmounting.

Open davidhaimson opened this issue 8 years ago • 15 comments

Current Behavior

In our app, some cells, when active, are rendered using our ListInput component:

grid-list-input-active

On small form factors, when the ListInput is open, the list renders full-width (with a vertical scrollbar, if necessary, and with a search input for finding particular values):

grid-list-input-open

On Android, if I focus the search input:

  1. The virtual keyboard comes up. This makes the viewport small enough that the ListInput (in the bottom row) is hidden.
  2. Our GridDimensions component detects that the viewport's height was reduced. It sets the scrollToRow prop on the Table component to the index of the selected row, so that the selected row, with the ListInput visible, will remain visible in the smaller viewport.
  3. However, fixed-data-table-2 unmounts the selected row.
  4. In the next render, the selected row appears again, at the bottom of the viewport. But because it was unmounted, it has forgotten that it was open.

grid-list-input-keyboard


So, can we fix this in our code, or does it require changes to fixed-data-table-2?

If you can suggest a workaround that requires no changes to fixed-data-table-2, that would be excellent.

Alternatively, if fixed-data-table-2 gave us a way to say "Don't unmount this row!", that would take care of it. I think the simple rule "Never unmount the selected row" would be a reasonable choice. A boolean prop allowSelectedRowUnmount, which we could set to false, or similar, would also be satisfactory.

I tried setting the bufferRowCount prop to a large value, but it had no observable effect in this case.

I added some debug logging to our code to show in what order things happen. Here's the current status:

User clicks the "Status" field in the last row of the grid. selectedRecordIndex changed, so the GridDimensions component sets scrollToRow:

DataGrid.GridDimensions.render:
  @_lastHeight: 519
  containerHeight: 519
  scrollToRow: 11

The ListInput component is rendered:

ListInput.componentDidMount:
  editKey: 3acc32c0:Status
  label: Status

User clicks the ListInput's toggle button. The list displays. Then user clicks the search input. The input gets focus, and the virtual keyboard pops up. The GridDimensions component detects that the viewport size changed, and sets scrollToRow on the Table component, so that the selected row will be visible (and hopefully will not be unmounted):

DataGrid.GridDimensions.render:
  @_lastHeight: 519
  containerHeight: 225
  scrollToRow: 11

However, the ListInput is unmounted:

ListInput.componentWillUnmount:
  editKey: 3acc32c0:Status
  label: Status

As a consequence, the displayed list is also unmounted. Then, in the very next render, the selected row containing the ListInput is mounted again:

ListInput.componentDidMount:
  editKey: 3acc32c0:Status
  label: Status

But of course it doesn't remember that it was open.

Possible Solution

A new prop for the Table component: allowSelectedRowUnmount, default true (so existing code behaves the same as before). If set to false, the Table component never unmounts the selected row.

Context

The existing code works fine everywhere except on Android. On Android, it is impossible to do keyboard input to controls in certain grid rows, depending on where they appear on the screen.

Your Environment

  • Version used: 0.8.8
  • Browser Name and version: Chrome on Android, 55.0.2883.40
  • Operating System and version (desktop or mobile): Android 7.1.1; Android SDK built for x86 Build/NYC (Android Simulator)
  • Link to your project: (private)

davidhaimson avatar Jan 30 '18 22:01 davidhaimson

I'm not sure of a great way to fix this. 2 things I recommend trying are

  1. Instead of putting your input component in the grid, render it separately on the page and position it based on the cells position. Not sure how best to do this but you could explore using Portals or having cells store their position into global state when opened.
  2. Set a large bufferRowCount prop so things have to go way off screen before they're evicted from the rendered rows

wcjordan avatar Jan 30 '18 22:01 wcjordan

Instead of putting your input component in the grid, render it separately on the page and position it based on the cells position.

At the moment, we detect that the cell is active (row is selected, and column is selected), and render the input component instead of just its value. What you suggest instead sounds very painful! I'd have to maintain a separate copy of the grid's geometry. I'm dubious.

Set a large bufferRowCount prop so things have to go way off screen before they're evicted from the rendered rows

This seemed like it would work. In the example I screen-captured above, the rowsCount prop is 13. I set bufferRowCount to 100, but nevertheless it behaves just as described above — the row unmounts. bufferRowCount appears to have no effect. Maybe what I'm asking for is for bufferRowCount to be fixed?

davidhaimson avatar Jan 30 '18 23:01 davidhaimson

I'm not sure I follow your bufferRowCount comment. I expect if you set it to 100, you'd keep around 50 rows ahead of the viewport and 50 behind it.

wcjordan avatar Jan 30 '18 23:01 wcjordan

I believe I am saying that bufferRowCount doesn't work as expected.

davidhaimson avatar Jan 31 '18 00:01 davidhaimson

Could you provide a JsFiddle which demonstrates the issue?

wcjordan avatar Jan 31 '18 01:01 wcjordan

I looked much deeper into this. bufferRowCount has no effect on the behavior in this case.

Here is the problem. In FixedDataTableBufferedRows.componentWillReceiveProps:

    if (nextProps.rowsCount !== this.props.rowsCount ||
        nextProps.defaultRowHeight !== this.props.defaultRowHeight ||
        nextProps.height !== this.props.height) {
      this._rowBuffer =
        new FixedDataTableRowBuffer(
          ...

In other words, whenever the height changes, it throws away the row buffer and starts over!

In order to solve my problem, instead we'd have to preserve the row buffer and keep it up to date, using bufferRowCount to define how big the row buffer is on either end of the viewport.

My management has asked me to back off here, so I'm afraid I won't be able to fix this for you. If you fix it, I would be delighted to test it.

davidhaimson avatar Jan 31 '18 19:01 davidhaimson

Now that I understand the root cause, I'll see if I can come with a JsFiddle. Probably tomorrow.

davidhaimson avatar Jan 31 '18 19:01 davidhaimson

I'm not sure how that would alter the bufferRowCount as it still uses it for props. I'm guessing this may have to do with the cell renderers you're using and you may need to make them controlled components, but without a reproduction it's hard to tell.

wcjordan avatar Jan 31 '18 19:01 wcjordan

If I understand correctly, when the height changes we throw away the old buffer, regardless of its buffer count.

I think the correct fix is to have the input trigger some global state and render the dialog based on that state. If you provide a fiddle we can try and resolve this; but without we cannot do much.

KamranAsif avatar Jan 31 '18 19:01 KamranAsif

If I understand correctly, when the height changes we throw away the old buffer, regardless of its buffer count.

That is correct.

I'm not sure how that would alter the bufferRowCount as it still uses it for props.

bufferRowCount is unchanged. But if you've thrown away the row buffer, who cares what bufferRowCount is? bufferRowCount is only used to decide how many rows belong in the row buffer. But FixedDataTableBufferedRows has explicitly reinitialized the row buffer, so there are exactly 0 rows in it for the next render.

Here's how this causes the problem I am experiencing: my cell renderer for the active cell has internal state. When the row buffer is reinitialized, my cell renderer is unmounted, and its internal state is gone forever.

I think the correct fix is to have the input trigger some global state and render the dialog based on that state.

The ListInput is a widget. Its UI state (in this case, whether the list is open or not) is private to itself, and beneath the notice of the app's global state. It could be made part of global state, but that would be a lot of trouble, it would add complexity, and in any case it would have to wait for some other integration work that's going on in our project at the moment.

I guess you guys have to decide whether fixed-data-table-2 supports its cell renderers having internal state or not. At the moment, I assert that it does not.

davidhaimson avatar Jan 31 '18 19:01 davidhaimson

Nope it does not. The react-y way to do this is with controlled components and passing in a value field for controlling your widget state. React's motivating principles aren't geared towards encapsulated components that own their own state.

One workaround you could try is setting the rowKeyGetter so the React diff algorithm reconciles row's onto themselves, but no guarantee this will solve the issue in every case.

wcjordan avatar Jan 31 '18 19:01 wcjordan

Might work, I'll give it a try. Thanks.

davidhaimson avatar Jan 31 '18 20:01 davidhaimson

@wcjordan We could try and have BufferedRows update RowBuffer so it doesn't throw everything out. It can have an update method that tries to do reconciling and keep as many rows as possible

KamranAsif avatar Jan 31 '18 21:01 KamranAsif

I'm not opposed to that, but not eager to do too much mucking around there without a major version bump. It might be worth seeing if this is fixed on v1 beta since we've refactored and removed the RowBuffer

wcjordan avatar Jan 31 '18 21:01 wcjordan

Closing due to inactivity and my preference for the suggested alternatives.

wcjordan avatar May 04 '20 18:05 wcjordan