UNPKG

semantic-ui-react

Version:
154 lines (127 loc) 4.76 kB
import _extends from 'babel-runtime/helpers/extends'; import _classCallCheck from 'babel-runtime/helpers/classCallCheck'; import _createClass from 'babel-runtime/helpers/createClass'; import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn'; import _inherits from 'babel-runtime/helpers/inherits'; import cx from 'classnames'; import PropTypes from 'prop-types'; import React, { Component } from 'react'; import { createHTMLImage, customPropTypes, getElementType, getUnhandledProps, META, useKeyOnly } from '../../lib'; // Note: You technically only need the 'content' wrapper when there's an // image. However, optionally wrapping it makes this function a lot more // complicated and harder to read. Since always wrapping it doesn't affect // the style in any way let's just do that. // // Note: To avoid requiring a wrapping div, we return an array here so to // prevent rendering issues each node needs a unique key. var defaultRenderer = function defaultRenderer(_ref) { var image = _ref.image, price = _ref.price, title = _ref.title, description = _ref.description; return [image && React.createElement( 'div', { key: 'image', className: 'image' }, createHTMLImage(image) ), React.createElement( 'div', { key: 'content', className: 'content' }, price && React.createElement( 'div', { className: 'price' }, price ), title && React.createElement( 'div', { className: 'title' }, title ), description && React.createElement( 'div', { className: 'description' }, description ) )]; }; defaultRenderer.handledProps = []; var SearchResult = function (_Component) { _inherits(SearchResult, _Component); function SearchResult() { var _ref2; var _temp, _this, _ret; _classCallCheck(this, SearchResult); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref2 = SearchResult.__proto__ || Object.getPrototypeOf(SearchResult)).call.apply(_ref2, [this].concat(args))), _this), _this.handleClick = function (e) { var onClick = _this.props.onClick; if (onClick) onClick(e, _this.props); }, _temp), _possibleConstructorReturn(_this, _ret); } _createClass(SearchResult, [{ key: 'render', value: function render() { var _props = this.props, active = _props.active, className = _props.className, renderer = _props.renderer; var classes = cx(useKeyOnly(active, 'active'), 'result', className); var rest = getUnhandledProps(SearchResult, this.props); var ElementType = getElementType(SearchResult, this.props); // Note: You technically only need the 'content' wrapper when there's an // image. However, optionally wrapping it makes this function a lot more // complicated and harder to read. Since always wrapping it doesn't affect // the style in any way let's just do that. return React.createElement( ElementType, _extends({}, rest, { className: classes, onClick: this.handleClick }), renderer(this.props) ); } }]); return SearchResult; }(Component); SearchResult.defaultProps = { renderer: defaultRenderer }; SearchResult._meta = { name: 'SearchResult', parent: 'Search', type: META.TYPES.MODULE }; SearchResult.handledProps = ['active', 'as', 'className', 'content', 'description', 'id', 'image', 'onClick', 'price', 'renderer', 'title']; export default SearchResult; SearchResult.propTypes = process.env.NODE_ENV !== "production" ? { /** An element type to render as (string or function). */ as: customPropTypes.as, /** The item currently selected by keyboard shortcut. */ active: PropTypes.bool, /** Additional classes. */ className: PropTypes.string, /** Shorthand for primary content. */ content: customPropTypes.contentShorthand, /** Additional text with less emphasis. */ description: PropTypes.string, /** A unique identifier. */ id: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), /** Add an image to the item. */ image: PropTypes.string, /** * Called on click. * * @param {SyntheticEvent} event - React's original SyntheticEvent. * @param {object} data - All props. */ onClick: PropTypes.func, /** Customized text for price. */ price: PropTypes.string, /** * Renders the result contents. * * @param {object} props - The SearchResult props object. * @returns {*} - Renderable result contents. */ renderer: PropTypes.func, /** Display title. */ title: PropTypes.string } : {};