React-admin: AutocompleteInput suggestions list location in filters

Created on 31 Aug 2018  路  9Comments  路  Source: marmelab/react-admin

General description
In filters of a List module, if a ReferenceInput with AutocompleteInput is moved after adding/removing another filter, the suggestions list stays at the old location.

What you were expecting:

When I add a new filter, I want the suggestions list of the previous AutocompleteInput to be moved to the right place.

What happened instead:

When a new filter is added/removed to a List, the AutocompleteInput is moved to the right place, but the suggestions list opens at the old location of the filter.

Steps to reproduce:

When adding a filter:
ezgif-3-e5904e8e53

When removing a filter:
ezgif-3-933ddc5008

Related code:

const OrderFilters = props => (
  <Filter {...props}>
    <TextInput source="company" />
    <ReferenceInput
      source="customer_id"
      reference="customer"
      allowEmpty
    >
      <AutocompleteInput optionText="name" />
    </ReferenceInput>
    <ReferenceInput
      source="carrier_id"
      reference="carrier"
      allowEmpty
    >
      <AutocompleteInput optionText="name" />
    </ReferenceInput>
  </Filter>
);

const OrderList = props => (
<List {...props} filters={<OrderFilters />}>
 [...]
</List>

Other information:

No error stacktrace.

Environment

  • React-admin version: 2.2.3
  • Last version that did not exhibit the issue (if applicable): unknown
  • React version: 16.4.13
  • Browser: Chrome 68.0.3440.106
bug

Most helpful comment

Can you please also add this solution on AutocompleteInput?
With this commit, now it seems only the AutocompleteArrayInput has fixed.

All 9 comments

wow, such a weird bug! Thanks for reporting it.

Could this be related to #2256?

possibly

I just saw the new component AutocompleteArrayInput introduced with v2.3.0 suffers the same effect.

(See when adding tags _Rock_ and _Crossfit_)

AutocompleteArrayInput

Not only in filters; if the suggestion list opens upward, when you search something, the list sticks to the top as it narrows.

screen shot 2019-02-06 at 10 37 58

I had the same error here and found a possible solution.

The problem happens because the Popper component used to render the choices is a stateless component.

I noticed that when the original input element moves, React is keeping the old Popper without forcing it to redraw.

My solution is to create a new object that mimics the original input element. Doing this, we force the component to be redrawn, since the anchor element will always be new.

const anchorEl = !this.inputEl
            ? null
            : {
                  getBoundingClientRect: () =>
                      this.inputEl.getBoundingClientRect(),
              };

Then I'm using this anchorEl in the Popper component.

Here is my implementation, let me know if is fine for a merge request:
https://github.com/tiagoschenkel/react-admin/commit/3e58c94f03fbd10857a6819521a05079d4b71e70

I noticed that I was forcing the Popper element to redraw on every call to the renderSuggestionsContainer.

So now I'm keeping a reference to the old position and only forcing the redraw when the inputEl moves to a new location:
https://github.com/tiagoschenkel/react-admin/commit/33fafcf1e3f2c9bb681849e10185b09977210a9c

I still don't know if this is the proper way to solve this issue, maybe we can store the inputEl and anchorEl elements in the state of the component.

My solution is based on this example from Material-UI docs:
https://v1-5-0.material-ui.com/utils/popper/#faked-reference-object

Can you please also add this solution on AutocompleteInput?
With this commit, now it seems only the AutocompleteArrayInput has fixed.

@selenyillar, #2928 solves the problem also for AutocompleteInput.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Dragomir-Ivanov picture Dragomir-Ivanov  路  3Comments

fzaninotto picture fzaninotto  路  3Comments

kdabir picture kdabir  路  3Comments

kopax picture kopax  路  3Comments

yangjiamu picture yangjiamu  路  3Comments