ssc-refer
Version:
React refer component for SSC 3.0
97 lines (84 loc) • 2.48 kB
JavaScript
;
import { pick } from 'lodash';
import Highlight from 'react-highlighter';
import React, { PropTypes } from 'react';
import Menu from './Menu.react';
import MenuItem from './MenuItem.react';
import getOptionLabel from './utils/getOptionLabel';
var MATCH_CLASS = 'bootstrap-typeahead-highlight';
var TypeaheadMenu = React.createClass({
displayName: 'TypeaheadMenu',
/**
* In addition to the propTypes below, the following props are automatically
* passed down by `Typeahead`:
*
* - labelKey
* - onPaginate
* - options
* - paginate
* - text
*/
propTypes: {
/**
* Provides the ability to specify a prefix before the user-entered text to
* indicate that the selection will be new. No-op unless `allowNew={true}`.
*/
newSelectionPrefix: PropTypes.string,
/**
* Provides a hook for customized rendering of menu item contents.
*/
renderMenuItemChildren: PropTypes.func
},
getDefaultProps: function getDefaultProps() {
return {
newSelectionPrefix: 'New selection: '
};
},
render: function render() {
var menuProps = pick(this.props, ['align', 'className', 'dropup', 'emptyLabel', 'maxHeight', 'onPaginate', 'paginate', 'paginationText', 'style']);
return React.createElement(
Menu,
menuProps,
this.props.options.map(this._renderMenuItem)
);
},
_renderMenuItem: function _renderMenuItem(option, idx) {
var _props = this.props,
labelKey = _props.labelKey,
newSelectionPrefix = _props.newSelectionPrefix,
renderMenuItemChildren = _props.renderMenuItemChildren,
text = _props.text;
var menuItemProps = {
disabled: option.disabled,
key: idx,
option: option,
position: idx
};
if (option.customOption) {
return React.createElement(
MenuItem,
menuItemProps,
newSelectionPrefix,
React.createElement(
Highlight,
{ matchClass: MATCH_CLASS, search: text },
option[labelKey]
)
);
}
return renderMenuItemChildren ? React.createElement(
MenuItem,
menuItemProps,
renderMenuItemChildren(option, this.props, idx)
) : React.createElement(
MenuItem,
menuItemProps,
React.createElement(
Highlight,
{ matchClass: MATCH_CLASS, search: text },
getOptionLabel(option, labelKey)
)
);
}
});
export default TypeaheadMenu;