52 lines
1.9 KiB
JavaScript
52 lines
1.9 KiB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
|
|
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
|
|
import classNames from 'classnames';
|
|
import React, { useContext } from 'react';
|
|
import useEventCallback from '@restart/hooks/useEventCallback';
|
|
import NavContext from './NavContext';
|
|
import SelectableContext, { makeEventKey } from './SelectableContext';
|
|
var defaultProps = {
|
|
disabled: false
|
|
};
|
|
var AbstractNavItem = React.forwardRef(function (_ref, ref) {
|
|
var active = _ref.active,
|
|
className = _ref.className,
|
|
tabIndex = _ref.tabIndex,
|
|
eventKey = _ref.eventKey,
|
|
onSelect = _ref.onSelect,
|
|
onClick = _ref.onClick,
|
|
Component = _ref.as,
|
|
props = _objectWithoutPropertiesLoose(_ref, ["active", "className", "tabIndex", "eventKey", "onSelect", "onClick", "as"]);
|
|
|
|
var navKey = makeEventKey(eventKey, props.href);
|
|
var parentOnSelect = useContext(SelectableContext);
|
|
var navContext = useContext(NavContext);
|
|
var isActive = active;
|
|
|
|
if (navContext) {
|
|
if (!props.role && navContext.role === 'tablist') props.role = 'tab';
|
|
props['data-rb-event-key'] = navKey;
|
|
props.id = navContext.getControllerId(navKey);
|
|
props['aria-controls'] = navContext.getControlledId(navKey);
|
|
isActive = active == null && navKey != null ? navContext.activeKey === navKey : active;
|
|
}
|
|
|
|
if (props.role === 'tab') {
|
|
props.tabIndex = isActive ? tabIndex : -1;
|
|
props['aria-selected'] = isActive;
|
|
}
|
|
|
|
var handleOnclick = useEventCallback(function (e) {
|
|
if (onClick) onClick(e);
|
|
if (navKey == null) return;
|
|
if (onSelect) onSelect(navKey, e);
|
|
if (parentOnSelect) parentOnSelect(navKey, e);
|
|
});
|
|
return React.createElement(Component, _extends({}, props, {
|
|
ref: ref,
|
|
onClick: handleOnclick,
|
|
className: classNames(className, isActive && 'active')
|
|
}));
|
|
});
|
|
AbstractNavItem.defaultProps = defaultProps;
|
|
export default AbstractNavItem; |