UNPKG

@fluentui/react-northstar

Version:
153 lines (152 loc) 6.63 kB
import { treeBehavior } from '@fluentui/accessibility'; import { useTelemetry, useUnhandledProps, getElementType, useAccessibility, useStyles, useFluentContext } from '@fluentui/react-bindings'; import * as customPropTypes from '@fluentui/react-proptypes'; import * as PropTypes from 'prop-types'; import * as React from 'react'; import { TreeItem } from './TreeItem'; import { TreeTitle } from './TreeTitle'; import { childrenExist, commonPropTypes, createShorthandFactory, rtlTextContainer } from '../../utils'; import { useTree } from './hooks/useTree'; import { TreeContext } from './context'; export var treeClassName = 'ui-tree'; /** * A Tree displays data organised in tree hierarchy. * * @accessibility * Implements [ARIA TreeView](https://www.w3.org/TR/wai-aria-practices-1.1/#TreeView) design pattern. * @accessibilityIssues * [Treeview - JAWS doesn't narrate position for each tree item](https://github.com/FreedomScientific/VFO-standards-support/issues/338) * [Aria-selected and aria-checked are not output correctly for trees #432](https://github.com/FreedomScientific/VFO-standards-support/issues/432) * [Aria compliant trees are read as empty tables](https://bugs.chromium.org/p/chromium/issues/detail?id=1048770) * [VoiceOver narrates "selected false" for DOM with role=option and no aria-selected attribute](http://www.openradar.me/FB8050959) * [VoiceOver does not support Aria 1.2 listbox role owning unselectable group role](http://www.openradar.me/FB8050958) * [Tree as table in Mac > VoiceOver narrates " no selection " when user navigates to tree/table](https://bugs.chromium.org/p/chromium/issues/detail?id=1273538) * [Tree as table in Mac > VoiceOver narrates " 0 items enclosed " when user navigates to expaded treeitem](https://bugs.chromium.org/p/chromium/issues/detail?id=1273540) * [Tree as table in Mac > VoiceOver doesn't narrate aria-labelledby element on treeitem](https://bugs.chromium.org/p/chromium/issues/detail?id=1273544) */ export var Tree = /*#__PURE__*/function () { var Tree = /*#__PURE__*/React.forwardRef(function (props, ref) { var context = useFluentContext(); var _useTelemetry = useTelemetry(Tree.displayName, context.telemetry), setStart = _useTelemetry.setStart, setEnd = _useTelemetry.setEnd; setStart(); var selectable = props.selectable, children = props.children, renderedItems = props.renderedItems, className = props.className, design = props.design, styles = props.styles, variables = props.variables; var ElementType = getElementType(props); var unhandledProps = useUnhandledProps(Tree.handledProps, props); var getA11yProps = useAccessibility(props.accessibility, { debugName: Tree.displayName, rtl: context.rtl, mapPropsToBehavior: function mapPropsToBehavior() { return { selectable: selectable }; } }); var _useStyles = useStyles(Tree.displayName, { className: treeClassName, mapPropsToInlineStyles: function mapPropsToInlineStyles() { return { className: className, design: design, styles: styles, variables: variables }; }, rtl: context.rtl, unstyled: props.unstyled }), classes = _useStyles.classes; var _useTree = useTree(props), visibleItemIds = _useTree.visibleItemIds, getItemById = _useTree.getItemById, registerItemRef = _useTree.registerItemRef, toggleItemActive = _useTree.toggleItemActive, focusItemById = _useTree.focusItemById, expandSiblings = _useTree.expandSiblings, toggleItemSelect = _useTree.toggleItemSelect, getToFocusIDByFirstCharacter = _useTree.getToFocusIDByFirstCharacter; var contextValue = React.useMemo(function () { return { getItemById: getItemById, registerItemRef: registerItemRef, toggleItemActive: toggleItemActive, expandSiblings: expandSiblings, focusItemById: focusItemById, toggleItemSelect: toggleItemSelect, getToFocusIDByFirstCharacter: getToFocusIDByFirstCharacter }; }, [getItemById, registerItemRef, toggleItemActive, focusItemById, expandSiblings, toggleItemSelect, getToFocusIDByFirstCharacter]); var renderContent = function renderContent() { return visibleItemIds.map(function (id) { var item = getItemById(id); var expanded = item.expanded, parent = item.parent, level = item.level, index = item.index, treeSize = item.treeSize; return TreeItem.create(item.item, { defaultProps: function defaultProps() { return getA11yProps('item', { renderItemTitle: props.renderItemTitle }); }, overrideProps: { expanded: expanded, parent: parent, key: id, level: level, index: index, treeSize: treeSize, selectable: selectable ? item.item.selectable : false } }); }); }; var element = /*#__PURE__*/React.createElement(TreeContext.Provider, { value: contextValue }, getA11yProps.unstable_wrapWithFocusZone( /*#__PURE__*/React.createElement(ElementType, getA11yProps('root', Object.assign({ className: classes.root, ref: ref }, rtlTextContainer.getAttributes({ forElements: [children] }), unhandledProps)), childrenExist(children) ? children : renderedItems ? renderedItems(renderContent()) : renderContent()))); setEnd(); return element; }); Tree.displayName = 'Tree'; Tree.propTypes = Object.assign({}, commonPropTypes.createCommon({ content: false }), { activeItemIds: customPropTypes.collectionShorthand, selectedItemIds: customPropTypes.collectionShorthand, defaultActiveItemIds: customPropTypes.collectionShorthand, defaultSelectedItemIds: customPropTypes.collectionShorthand, exclusive: PropTypes.bool, selectable: PropTypes.bool, unstyled: PropTypes.bool, items: customPropTypes.collectionObjectShorthand, onActiveItemIdsChange: PropTypes.func, onSelectedItemIdsChange: PropTypes.func, renderItemTitle: PropTypes.func, renderedItems: PropTypes.func }); Tree.Item = TreeItem; Tree.Title = TreeTitle; Tree.defaultProps = { accessibility: treeBehavior }; Tree.handledProps = Object.keys(Tree.propTypes); Tree.create = createShorthandFactory({ Component: Tree, mappedArrayProp: 'items' }); return Tree; }(); //# sourceMappingURL=Tree.js.map