@talend/react-bootstrap
Version:
Bootstrap 3 components built with React
127 lines • 3.28 kB
JavaScript
import classNames from 'classnames';
import React from 'react';
import PropTypes from 'prop-types';
import all from 'prop-types-extra/lib/all';
import SafeAnchor from './SafeAnchor';
import { bsClass, prefix, splitBsPropsAndOmit } from './utils/bootstrapUtils';
import createChainedFunction from './utils/createChainedFunction';
import { jsx as _jsx } from "react/jsx-runtime";
const propTypes = {
/**
* Highlight the menu item as active.
*/
active: PropTypes.bool,
/**
* Disable the menu item, making it unselectable.
*/
disabled: PropTypes.bool,
/**
* Styles the menu item as a horizontal rule, providing visual separation between
* groups of menu items.
*/
divider: all(PropTypes.bool, ({
divider,
children
}) => divider && children ? new Error('Children will not be rendered for dividers') : null),
/**
* Value passed to the `onSelect` handler, useful for identifying the selected menu item.
*/
eventKey: PropTypes.any,
/**
* Styles the menu item as a header label, useful for describing a group of menu items.
*/
header: PropTypes.bool,
/**
* HTML `href` attribute corresponding to `a.href`.
*/
href: PropTypes.string,
/**
* Callback fired when the menu item is clicked.
*/
onClick: PropTypes.func,
/**
* Callback fired when the menu item is selected.
*
* ```js
* (eventKey: any, event: Object) => any
* ```
*/
onSelect: PropTypes.func
};
const defaultProps = {
divider: false,
disabled: false,
header: false
};
class MenuItem extends React.Component {
constructor(props, context) {
super(props, context);
this.handleClick = this.handleClick.bind(this);
}
handleClick(event) {
const {
href,
disabled,
onSelect,
eventKey
} = this.props;
if (!href || disabled) {
event.preventDefault();
}
if (disabled) {
return;
}
if (onSelect) {
onSelect(eventKey, event);
}
}
render() {
const {
active,
disabled,
divider,
header,
onClick,
className,
style,
...props
} = this.props;
const [bsProps, elementProps] = splitBsPropsAndOmit(props, ['eventKey', 'onSelect']);
if (divider) {
// Forcibly blank out the children; separators shouldn't render any.
elementProps.children = undefined;
return /*#__PURE__*/_jsx("li", {
...elementProps,
role: "separator",
className: classNames(className, 'divider'),
style: style
});
}
if (header) {
return /*#__PURE__*/_jsx("li", {
...elementProps,
role: "heading",
className: classNames(className, prefix(bsProps, 'header')),
style: style
});
}
return /*#__PURE__*/_jsx("li", {
role: "presentation",
className: classNames(className, {
active,
disabled
}),
style: style,
children: /*#__PURE__*/_jsx(SafeAnchor, {
...elementProps,
role: "menuitem",
tabIndex: "-1",
onClick: createChainedFunction(onClick, this.handleClick)
})
});
}
}
MenuItem.propTypes = propTypes;
MenuItem.defaultProps = defaultProps;
export default bsClass('dropdown', MenuItem);
//# sourceMappingURL=MenuItem.js.map