UNPKG

@fluentui/react-northstar

Version:
162 lines (159 loc) 6.36 kB
import _get from "lodash/get"; import _map from "lodash/map"; import _findIndex from "lodash/findIndex"; import _filter from "lodash/filter"; import { toolbarRadioGroupBehavior, toolbarRadioGroupItemBehavior } from '@fluentui/accessibility'; import { compose, getElementType, mergeVariablesOverrides, useUnhandledProps, useAccessibility, useStyles, useFluentContext, useTelemetry } from '@fluentui/react-bindings'; import { Ref } from '@fluentui/react-component-ref'; import * as customPropTypes from '@fluentui/react-proptypes'; import * as PropTypes from 'prop-types'; import * as React from 'react'; import { createShorthand, childrenExist, commonPropTypes } from '../../utils'; import { ToolbarDivider } from './ToolbarDivider'; import { ToolbarItem } from './ToolbarItem'; import { ToolbarVariablesContext, ToolbarVariablesProvider } from './toolbarVariablesContext'; export var toolbarRadioGroupClassName = 'ui-toolbars'; // FIXME: required by getComponentInfo/isConformant. But this is group inside a toolbar not a group of toolbars /** * A ToolbarRadioGroup renders Toolbar item as a group of mutually exclusive options. * Component doesn't implement mutual exclusiveness, it just serves accessibility purposes. * * @accessibility * Implements [ARIA RadioGroup](https://www.w3.org/TR/wai-aria-practices/#radiobutton) design pattern. */ export var ToolbarRadioGroup = /*#__PURE__*/function () { var ToolbarRadioGroup = compose(function (props, ref, composeOptions) { var context = useFluentContext(); var _useTelemetry = useTelemetry(composeOptions.displayName, context.telemetry), setStart = _useTelemetry.setStart, setEnd = _useTelemetry.setEnd; setStart(); var accessibility = props.accessibility, activeIndex = props.activeIndex, children = props.children, className = props.className, design = props.design, items = props.items, variables = props.variables, styles = props.styles; var itemRefs = []; var slotProps = composeOptions.resolveSlotProps(props); var parentVariables = React.useContext(ToolbarVariablesContext); var mergedVariables = mergeVariablesOverrides(parentVariables, variables); var getA11yProps = useAccessibility(accessibility, { debugName: composeOptions.displayName, actionHandlers: { nextItem: function nextItem(event) { return setFocusedItem(event, 1); }, prevItem: function prevItem(event) { return setFocusedItem(event, -1); } }, rtl: context.rtl }); var _useStyles = useStyles(composeOptions.displayName, { className: composeOptions.className, composeOptions: composeOptions, mapPropsToInlineStyles: function mapPropsToInlineStyles() { return { className: className, design: design, styles: styles, variables: mergedVariables }; }, rtl: context.rtl, unstable_props: props }), classes = _useStyles.classes; var setFocusedItem = function setFocusedItem(event, direction) { // filter items which are not disabled var filteredRadioItems = _filter(itemRefs, function (item, index) { var currentItem = items[index]; return currentItem && !currentItem.disabled; }); // get the index of currently focused element (w/ tabindex = 0) or the first one as default var currentFocusedIndex = _findIndex(filteredRadioItems, function (item) { return item.current.tabIndex === 0; }) || 0; var itemsLength = filteredRadioItems.length; var nextIndex = currentFocusedIndex + direction; if (nextIndex >= itemsLength) { nextIndex = 0; } if (nextIndex < 0) { nextIndex = itemsLength - 1; } var nextItemToFocus = filteredRadioItems[nextIndex].current; if (nextItemToFocus) { nextItemToFocus.focus(); } if (context.target.activeElement === nextItemToFocus) { event.stopPropagation(); } event.preventDefault(); }; var renderItems = function renderItems() { return _map(items, function (item, index) { var kind = _get(item, 'kind', 'item'); var ref = /*#__PURE__*/React.createRef(); itemRefs[index] = ref; if (kind === 'divider') { return createShorthand(composeOptions.slots.divider, item, { defaultProps: function defaultProps() { return slotProps.divider; } }); } var toolbarItem = createShorthand(composeOptions.slots.item, item, { defaultProps: function defaultProps() { return Object.assign({}, slotProps.item, { active: activeIndex === index }); } }); return /*#__PURE__*/React.createElement(Ref, { innerRef: ref, key: toolbarItem.key }, toolbarItem); }); }; var ElementType = getElementType(props); var unhandledProps = useUnhandledProps(composeOptions.handledProps, props); var element = /*#__PURE__*/React.createElement(ElementType, getA11yProps('root', Object.assign({}, unhandledProps, { className: classes.root, ref: ref })), /*#__PURE__*/React.createElement(ToolbarVariablesProvider, { value: mergedVariables }, childrenExist(children) ? children : renderItems())); setEnd(); return element; }, { className: toolbarRadioGroupClassName, displayName: 'ToolbarRadioGroup', slots: { item: ToolbarItem, divider: ToolbarDivider }, slotProps: function slotProps() { return { item: { accessibility: toolbarRadioGroupItemBehavior } }; }, shorthandConfig: { mappedProp: 'content' }, handledProps: ['accessibility', 'as', 'children', 'className', 'content', 'design', 'styles', 'variables', 'activeIndex', 'items'] }); ToolbarRadioGroup.propTypes = Object.assign({}, commonPropTypes.createCommon(), { activeIndex: PropTypes.number, items: customPropTypes.collectionShorthandWithKindProp(['divider', 'item']) }); ToolbarRadioGroup.defaultProps = { accessibility: toolbarRadioGroupBehavior }; return ToolbarRadioGroup; }(); //# sourceMappingURL=ToolbarRadioGroup.js.map