|
1 | 1 | import cx from 'clsx'
|
| 2 | +import _ from 'lodash' |
2 | 3 | import PropTypes from 'prop-types'
|
3 |
| -import React, { Component } from 'react' |
| 4 | +import React from 'react' |
4 | 5 |
|
5 | 6 | import {
|
6 | 7 | createHTMLImage,
|
@@ -30,32 +31,29 @@ const defaultRenderer = ({ image, price, title, description }) => [
|
30 | 31 | </div>,
|
31 | 32 | ]
|
32 | 33 |
|
33 |
| -export default class SearchResult extends Component { |
34 |
| - handleClick = (e) => { |
35 |
| - const { onClick } = this.props |
| 34 | +const SearchResult = React.forwardRef(function (props, ref) { |
| 35 | + const { active, className, renderer } = props |
36 | 36 |
|
37 |
| - if (onClick) onClick(e, this.props) |
| 37 | + const handleClick = (e) => { |
| 38 | + _.invoke(props, 'onClick', e, props) |
38 | 39 | }
|
39 | 40 |
|
40 |
| - render() { |
41 |
| - const { active, className, renderer } = this.props |
42 |
| - |
43 |
| - const classes = cx(useKeyOnly(active, 'active'), 'result', className) |
44 |
| - const rest = getUnhandledProps(SearchResult, this.props) |
45 |
| - const ElementType = getElementType(SearchResult, this.props) |
46 |
| - |
47 |
| - // Note: You technically only need the 'content' wrapper when there's an |
48 |
| - // image. However, optionally wrapping it makes this function a lot more |
49 |
| - // complicated and harder to read. Since always wrapping it doesn't affect |
50 |
| - // the style in any way let's just do that. |
51 |
| - return ( |
52 |
| - <ElementType {...rest} className={classes} onClick={this.handleClick}> |
53 |
| - {renderer(this.props)} |
54 |
| - </ElementType> |
55 |
| - ) |
56 |
| - } |
57 |
| -} |
58 |
| - |
| 41 | + const classes = cx(useKeyOnly(active, 'active'), 'result', className) |
| 42 | + const rest = getUnhandledProps(SearchResult, props) |
| 43 | + const ElementType = getElementType(SearchResult, props) |
| 44 | + |
| 45 | + // Note: You technically only need the 'content' wrapper when there's an |
| 46 | + // image. However, optionally wrapping it makes this function a lot more |
| 47 | + // complicated and harder to read. Since always wrapping it doesn't affect |
| 48 | + // the style in any way let's just do that. |
| 49 | + return ( |
| 50 | + <ElementType {...rest} className={classes} onClick={handleClick} ref={ref}> |
| 51 | + {renderer(props)} |
| 52 | + </ElementType> |
| 53 | + ) |
| 54 | +}) |
| 55 | + |
| 56 | +SearchResult.displayName = 'SearchResult' |
59 | 57 | SearchResult.propTypes = {
|
60 | 58 | /** An element type to render as (string or function). */
|
61 | 59 | as: PropTypes.elementType,
|
@@ -104,3 +102,5 @@ SearchResult.propTypes = {
|
104 | 102 | SearchResult.defaultProps = {
|
105 | 103 | renderer: defaultRenderer,
|
106 | 104 | }
|
| 105 | + |
| 106 | +export default SearchResult |
0 commit comments