react-autocomplete icon indicating copy to clipboard operation
react-autocomplete copied to clipboard

Cannot select via click if rendered item is an imported Component.

Open agonsalves opened this issue 7 years ago • 7 comments

If I set the "renderItem" prop to be a component that has been imported, then clicking on the items in the menu does not trigger the onSelect method. The items also do not get isHighlighted on mouse over. Navigating by keyboard does work.

<Autocomplete
                autoHighlight={false}
                className={className}
                getItemValue={item => item.id}
                items={options}
                name={name}
                onChange={this.handleChange}
                onSelect={this.handleSelection}
                renderItem={(item, isHighlighted) =>
                    <DatalistItem
                        key={Math.random()}
                        item={item}
                        isHighlighted={isHighlighted}
                    />
                }
            />
class DatalistItem extends PureComponent {
    render() {
        const {item, isHighlighted} = this.props
        return (
            <DivContainer
                className={isHighlighted ? 'active' : ''}
                children={item.title}
            />
        )
    }
}

agonsalves avatar Nov 29 '18 19:11 agonsalves

Autocomplete uses lots of on* events. Your component has to accept them and pass them through. It means renderInput, renderItem and renderMenu props has to return components aware of this on* events. For example, events used for menu are onClick and onMouseEnter: https://github.com/reactjs/react-autocomplete/blob/46300e999f304dd51b03aa6b8988979e5c92e552/lib/Autocomplete.js#L466

cinan avatar Dec 14 '18 14:12 cinan

@cinan Thanks. How do I make them aware? The renderItem method only passes two values to the component.

agonsalves avatar Dec 14 '18 16:12 agonsalves

The on* props are injected by Autocomplete library. Example for renderItem (note ...events):

<Autocomplete
  /// ...
  renderItem={(item, highlight) => <Item item={item} highlight={highlight} />}
/>

class Item extends React.Component {
  render() {
    const { item, highlight, ...events } = this.props;
    return (
      <SomeComponent key={item} highlight={highlight} {...events}>{item}</SomeComponent>
    );
  }
}

cinan avatar Dec 14 '18 16:12 cinan

@cinan I just tried exactly that, but there are no event handlers being passed to the component at all.

agonsalves avatar Dec 14 '18 16:12 agonsalves

I'm also facing the same issue. It works when I comment out my onBlur function. @agonsalves were you able to solve this?

glocore avatar Feb 17 '19 13:02 glocore

For me, this was happening because I was programmatically toggling the open prop, and due to an implementation detail, open would toggle to false before onSelect. I gave a 100 ms delay to onBlur using setTimeout and that seems to have solved the issue.

glocore avatar Feb 17 '19 14:02 glocore

Hello all. I am facing the same issue, not being able to select and item. I don't have any blur events but my console does lights up with:

**Warning: Stateless function components cannot be given refs. Attempts to access this ref will fail.

Check the render method of Autocomplete.**

Any thoughts or experience with this?


So my issue was two fold: one using function components to return other components and multiple nesting divs inside. In summary I simplified it as much as I could without disrupting the prop flow down

Example : renderItem={Content} where as before it was this convoluted (a,c,v)=>

<Content {...a}/>

makis-spy avatar Feb 18 '19 06:02 makis-spy