ssc-refer
Version:
React refer component for SSC 3.0
143 lines (126 loc) • 3.82 kB
JavaScript
'use strict';
import _extends from 'babel-runtime/helpers/extends';
import cx from 'classnames';
import React, { Children } from 'react';
import PropTypes from 'prop-types';
import { BaseMenuItem } from './MenuItem.react';
var BaseMenu = function BaseMenu(props) {
return React.createElement(
'ul',
_extends({}, props, {
className: cx('dropdown-menu', props.className) }),
props.children
);
};
/**
* Menu component that automatically handles pagination and empty state when
* passed a set of filtered and truncated results.
*/
var Menu = React.createClass({
displayName: 'Menu',
propTypes: {
/**
* Specify menu alignment. The default value is `justify`, which makes the
* menu as wide as the input and truncates long values. Specifying `left`
* or `right` will align the menu to that side and the width will be
* determined by the length of menu item values.
*/
align: PropTypes.oneOf(['justify', 'left', 'right']),
/**
* Message to display in the menu if there are no valid results.
*/
emptyLabel: PropTypes.string,
/**
* Maximum height of the dropdown menu, in px.
*/
maxHeight: PropTypes.number,
/**
* Prompt displayed when large data sets are paginated.
*/
paginationText: PropTypes.string
},
getDefaultProps: function getDefaultProps() {
return {
align: 'justify',
emptyLabel: '加载中...',
maxHeight: 300,
paginate: true,
paginationText: '显示更多...'
};
},
render: function render() {
var _props = this.props,
align = _props.align,
children = _props.children,
className = _props.className,
emptyLabel = _props.emptyLabel;
var noResults = Children.count(children) === 0;
// If an empty string is passed, suppress menu when there are no results.
if (noResults && emptyLabel === '') {
return null;
}
var contents = noResults ? React.createElement(
BaseMenuItem,
{ disabled: true },
emptyLabel
) : children;
return React.createElement(
BaseMenu,
{
className: cx('bootstrap-typeahead-menu', {
'dropdown-menu-justify': align === 'justify',
'dropdown-menu-right': align === 'right'
}, className),
style: this._getMenuStyle() },
contents,
this._renderPaginationMenuItem()
);
},
/**
* Allow user to see more results, if available.
*/
_renderPaginationMenuItem: function _renderPaginationMenuItem() {
var _props2 = this.props,
children = _props2.children,
onPaginate = _props2.onPaginate,
paginate = _props2.paginate,
paginationText = _props2.paginationText;
if (paginate && Children.count(children)) {
return [React.createElement('li', {
className: 'divider',
key: 'pagination-item-divider',
role: 'separator'
}), React.createElement(
BaseMenuItem,
{
className: 'bootstrap-typeahead-menu-paginator',
key: 'pagination-item',
onClick: onPaginate },
paginationText
)];
}
},
_getMenuStyle: function _getMenuStyle() {
var _props3 = this.props,
align = _props3.align,
dropup = _props3.dropup,
maxHeight = _props3.maxHeight,
style = _props3.style;
var menuStyle = _extends({}, style, {
display: 'block',
maxHeight: maxHeight + 'px',
overflow: 'auto'
});
if (style) {
if (dropup) {
menuStyle.top = 'auto';
} else {
delete menuStyle.bottom;
}
menuStyle.left = align === 'right' ? 'auto' : style.left;
menuStyle.right = align === 'left' ? 'auto' : style.right;
}
return menuStyle;
}
});
export default Menu;