react-accessible-treeview
Advanced tools
Comparing version 2.1.0 to 2.1.1
@@ -1,1 +0,1 @@ | ||
"use strict";function _interopDefault(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var React=require("react"),React__default=_interopDefault(React),cx=_interopDefault(require("classnames")),PropTypes=_interopDefault(require("prop-types"));function _defineProperty(e,t,a){return t in e?Object.defineProperty(e,t,{value:a,enumerable:!0,configurable:!0,writable:!0}):e[t]=a,e}function _extends(){return(_extends=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var a=arguments[t];for(var r in a)Object.prototype.hasOwnProperty.call(a,r)&&(e[r]=a[r])}return e}).apply(this,arguments)}function ownKeys(e,t){var a=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),a.push.apply(a,r)}return a}function _objectSpread2(e){for(var t=1;t<arguments.length;t++){var a=null!=arguments[t]?arguments[t]:{};t%2?ownKeys(a,!0).forEach(function(t){_defineProperty(e,t,a[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(a)):ownKeys(a).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(a,t))})}return e}function _objectWithoutPropertiesLoose(e,t){if(null==e)return{};var a,r,n={},l=Object.keys(e);for(r=0;r<l.length;r++)a=l[r],t.indexOf(a)>=0||(n[a]=e[a]);return n}function _objectWithoutProperties(e,t){if(null==e)return{};var a,r,n=_objectWithoutPropertiesLoose(e,t);if(Object.getOwnPropertySymbols){var l=Object.getOwnPropertySymbols(e);for(r=0;r<l.length;r++)a=l[r],t.indexOf(a)>=0||Object.prototype.propertyIsEnumerable.call(e,a)&&(n[a]=e[a])}return n}function _slicedToArray(e,t){return _arrayWithHoles(e)||_iterableToArrayLimit(e,t)||_nonIterableRest()}function _toConsumableArray(e){return _arrayWithoutHoles(e)||_iterableToArray(e)||_nonIterableSpread()}function _arrayWithoutHoles(e){if(Array.isArray(e)){for(var t=0,a=new Array(e.length);t<e.length;t++)a[t]=e[t];return a}}function _arrayWithHoles(e){if(Array.isArray(e))return e}function _iterableToArray(e){if(Symbol.iterator in Object(e)||"[object Arguments]"===Object.prototype.toString.call(e))return Array.from(e)}function _iterableToArrayLimit(e,t){var a=[],r=!0,n=!1,l=void 0;try{for(var s,d=e[Symbol.iterator]();!(r=(s=d.next()).done)&&(a.push(s.value),!t||a.length!==t);r=!0);}catch(e){n=!0,l=e}finally{try{r||null==d.return||d.return()}finally{if(n)throw l}}return a}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance")}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}var composeHandlers=function(){for(var e=arguments.length,t=new Array(e),a=0;a<e;a++)t[a]=arguments[a];return function(e){for(var a=0,r=t;a<r.length;a++){var n=r[a];if(n&&n(e),e.defaultPrevented)break}}},difference=function(e,t){var a=new Set,r=!0,n=!1,l=void 0;try{for(var s,d=e[Symbol.iterator]();!(r=(s=d.next()).done);r=!0){var i=s.value;t.has(i)||a.add(i)}}catch(e){n=!0,l=e}finally{try{r||null==d.return||d.return()}finally{if(n)throw l}}return a},symmetricDifference=function(e,t){return new Set([].concat(_toConsumableArray(difference(e,t)),_toConsumableArray(difference(t,e))))},usePrevious=function(e){var t=React.useRef();return React.useEffect(function(){t.current=e},[e]),t.current},isBranchNode=function(e,t){return null!=e[t].children&&e[t].children.length>0},focusRef=function(e){null!=e&&e.focus&&e.focus()},getParent=function(e,t){return e[t].parent},getDescendants=function(e,t,a){var r=[];return function e(t,n){var l=t[n];if(null!=l.children){var s=!0,d=!1,i=void 0;try{for(var c,o=l.children.filter(function(e){return!a.has(e)})[Symbol.iterator]();!(s=(c=o.next()).done);s=!0){var u=c.value;r.push(u),e(t,u)}}catch(e){d=!0,i=e}finally{try{s||null==o.return||o.return()}finally{if(d)throw i}}}}(e,t),r},getSibling=function(e,t,a){var r=getParent(e,t);if(null!=r){var n=e[r],l=n.children.indexOf(t)+a;if(n.children[l])return n.children[l]}return null},getLastAccessible=function(e,t,a){var r=e[t];for(e[0].id===t&&(r=e[e[t].children[e[t].children.length-1]]);a.has(r.id)&&isBranchNode(e,r.id);)r=e[r.children[r.children.length-1]];return r.id},getPreviousAccessible=function(e,t,a){if(t===e[0].children[0])return null;var r=getSibling(e,t,-1);return null==r?getParent(e,t):getLastAccessible(e,r,a)},getNextAccessible=function(e,t,a){var r=e[t].id;if(isBranchNode(e,r)&&a.has(r))return e[r].children[0];for(;;){var n=getSibling(e,r,1);if(null!=n)return n;if(null==(r=getParent(e,r)))return null}},propagateSelectChange=function(e,t,a,r){var n={every:new Set,some:new Set,none:new Set},l=!0,s=!1,d=void 0;try{for(var i,c=t[Symbol.iterator]();!(l=(i=c.next()).done);l=!0)for(var o=i.value;;){var u=getParent(e,o);if(0===u||r.has(u))break;var p=e[u].children.filter(function(e){return!r.has(e)});if(0===p.length)break;p.some(function(e){return a.has(e)})?p.every(function(e){return a.has(e)})?n.every.add(u):n.some.add(u):n.none.add(u),o=u}}catch(e){s=!0,d=e}finally{try{l||null==c.return||c.return()}finally{if(s)throw d}}return n},getAccessibleRange=function(e){var t=e.data,a=e.expandedIds,r=e.from,n=e.to,l=[],s=Object.keys(t).length,d=0,i=r;if(l.push(r),r<n)for(;d<s&&(i=getNextAccessible(t,i,a),l.push(i),null!=i&&i!==n);)d+=1;else if(r>n)for(;d<s&&(i=getPreviousAccessible(t,i,a),l.push(i),null!=i&&i!==n);)d+=1;return l},flattenTree=function(e){var t=0,a=[];return function e(r,n){if(r.id=t,r.parent=n,a[t]=r,t+=1,null!=r.children&&0!==r.children.length){var l=!0,s=!1,d=void 0;try{for(var i,c=r.children[Symbol.iterator]();!(l=(i=c.next()).done);l=!0){e(i.value,r.id)}}catch(e){s=!0,d=e}finally{try{l||null==c.return||c.return()}finally{if(s)throw d}}r.children=r.children.map(function(e){return e.id})}}(e,null),a},getAriaSelected=function(e,t,a){if(!t)return a?e:!!e||void 0},propagatedIds=function(e,t,a){return t.concat.apply(t,_toConsumableArray(t.filter(function(t){return isBranchNode(e,t)}).map(function(t){return getDescendants(e,t,a)})))},isIE=function(){return window.navigator.userAgent.match(/Trident/)},onComponentBlur=function(e,t,a){isIE()?setTimeout(function(){return!t.contains(document.activeElement)&&a()},0):!t.contains(e.nativeEvent.relatedTarget)&&a()},baseClassNames={root:"tree",node:"tree-node",branch:"tree-node__branch",branchWrapper:"tree-branch-wrapper",leafListItem:"tree-leaf-list-item",leaf:"tree-node__leaf",nodeGroup:"tree-node-group"},treeTypes={collapse:"COLLAPSE",collapseMany:"COLLAPSE_MANY",expand:"EXPAND",expandMany:"EXPAND_MANY",halfSelect:"HALF_SELECT",select:"SELECT",deselect:"DESELECT",toggle:"TOGGLE",toggleSelect:"TOGGLE_SELECT",changeSelectMany:"SELECT_MANY",exclusiveSelectMany:"EXCLUSIVE_SELECT_MANY",exclusiveChangeSelectMany:"EXCLUSIVE_CHANGE_SELECT_MANY",focus:"FOCUS",blur:"BLUR",disable:"DISABLE",enable:"ENABLE"},treeReducer=function(e,t){switch(t.type){case treeTypes.collapse:var a=new Set(e.expandedIds);return a.delete(t.id),_objectSpread2({},e,{expandedIds:a,tabbableId:t.id,isFocused:!0,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case treeTypes.collapseMany:var r=new Set(e.expandedIds),n=!0,l=!1,s=void 0;try{for(var d,i=t.ids[Symbol.iterator]();!(n=(d=i.next()).done);n=!0){var c=d.value;r.delete(c)}}catch(e){l=!0,s=e}finally{try{n||null==i.return||i.return()}finally{if(l)throw s}}return _objectSpread2({},e,{expandedIds:r,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case treeTypes.expand:var o=new Set(e.expandedIds);return o.add(t.id),_objectSpread2({},e,{expandedIds:o,tabbableId:t.id,isFocused:!0,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case treeTypes.expandMany:var u=new Set([].concat(_toConsumableArray(e.expandedIds),_toConsumableArray(t.ids)));return _objectSpread2({},e,{expandedIds:u,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case treeTypes.toggle:var p=new Set(e.expandedIds);return e.expandedIds.has(t.id)?p.delete(t.id):p.add(t.id),_objectSpread2({},e,{expandedIds:p,tabbableId:t.id,isFocused:!0,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case treeTypes.halfSelect:if(e.disabledIds.has(t.id))return e;var f=new Set(e.halfSelectedIds),h=new Set(e.selectedIds);return f.add(t.id),h.delete(t.id),_objectSpread2({},e,{selectedIds:h,halfSelectedIds:f,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case treeTypes.select:if(e.disabledIds.has(t.id))return e;var y;t.multiSelect?(y=new Set(e.selectedIds)).add(t.id):(y=new Set).add(t.id);var b=new Set(e.halfSelectedIds);return b.delete(t.id),_objectSpread2({},e,{selectedIds:y,halfSelectedIds:b,tabbableId:t.keepFocus?e.tabbableId:t.id,isFocused:!0,lastUserSelect:t.NotUserAction?e.lastUserSelect:t.id,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case treeTypes.deselect:if(e.disabledIds.has(t.id))return e;var S;t.multiSelect?(S=new Set(e.selectedIds)).delete(t.id):S=new Set;var I=new Set(e.halfSelectedIds);return I.delete(t.id),_objectSpread2({},e,{selectedIds:S,halfSelectedIds:I,tabbableId:t.keepFocus?e.tabbableId:t.id,isFocused:!0,lastUserSelect:t.NotUserAction?e.lastUserSelect:t.id,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case treeTypes.toggleSelect:if(e.disabledIds.has(t.id))return e;var v,g=e.selectedIds.has(t.id);t.multiSelect?(v=new Set(e.selectedIds),g?v.delete(t.id):v.add(t.id)):(v=new Set,g||v.add(t.id));var m=new Set(e.halfSelectedIds);return m.delete(t.id),_objectSpread2({},e,{selectedIds:v,halfSelectedIds:m,tabbableId:t.id,isFocused:!0,lastUserSelect:t.NotUserAction?e.lastUserSelect:t.id,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case treeTypes.changeSelectMany:var T,A=t.ids.filter(function(t){return!e.disabledIds.has(t)});if(t.multiSelect){T=t.select?new Set([].concat(_toConsumableArray(e.selectedIds),_toConsumableArray(A))):difference(e.selectedIds,new Set(A));var w=difference(e.halfSelectedIds,T);return _objectSpread2({},e,{selectedIds:T,halfSelectedIds:w,lastAction:t.type,lastInteractedWith:t.lastInteractedWith})}return e;case treeTypes.exclusiveChangeSelectMany:var x,_=t.ids.filter(function(t){return!e.disabledIds.has(t)});if(t.multiSelect){x=t.select?new Set(_):difference(e.selectedIds,new Set(_));var E=difference(e.halfSelectedIds,x);return _objectSpread2({},e,{selectedIds:x,halfSelectedIds:E,lastAction:t.type,lastInteractedWith:t.lastInteractedWith})}return e;case treeTypes.focus:return _objectSpread2({},e,{tabbableId:t.id,isFocused:!0,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case treeTypes.blur:return _objectSpread2({},e,{isFocused:!1});case treeTypes.disable:var W=new Set(e.disabledIds);return W.add(t.id),_objectSpread2({},e,{disabledIds:W});case treeTypes.enable:var P=new Set(e.disabledIds);return P.delete(t.id),_objectSpread2({},e,{disabledIds:P});default:throw new Error("Invalid action passed to the reducer")}},useTree=function(e){var t=e.data,a=e.defaultExpandedIds,r=e.defaultSelectedIds,n=e.defaultDisabledIds,l=e.nodeRefs,s=e.onSelect,d=e.onExpand,i=e.multiSelect,c=e.propagateSelectUpwards,o=_slicedToArray(React.useReducer(treeReducer,{selectedIds:new Set(r),tabbableId:t[0].children[0],isFocused:!1,expandedIds:new Set(a),halfSelectedIds:new Set,lastUserSelect:t[0].children[0],lastInteractedWith:null,disabledIds:new Set(n)}),2),u=o[0],p=o[1],f=u.selectedIds,h=u.expandedIds,y=u.disabledIds,b=u.tabbableId,S=u.halfSelectedIds,I=u.lastAction,v=u.lastInteractedWith,g=usePrevious(f)||new Set,m=symmetricDifference(f,g);React.useEffect(function(){if(s!==noop){var e=!0,a=!1,r=void 0;try{for(var n,l=m[Symbol.iterator]();!(e=(n=l.next()).done);e=!0){var d=n.value,i=isBranchNode(t,d);s({element:t[d],isBranch:i,isExpanded:i?h.has(d):void 0,isSelected:f.has(d),isDisabled:y.has(d),isHalfSelected:i?S.has(d):void 0,treeState:u})}}catch(e){a=!0,r=e}finally{try{e||null==l.return||l.return()}finally{if(a)throw r}}}},[t,f,h,y,S,m,s,u]);var T=usePrevious(h)||new Set;return React.useEffect(function(){var e=symmetricDifference(h,T);if(d!==noop){var a=!0,r=!1,n=void 0;try{for(var l,s=e[Symbol.iterator]();!(a=(l=s.next()).done);a=!0){var i=l.value;d({element:t[i],isExpanded:h.has(i),isSelected:f.has(i),isDisabled:y.has(i),isHalfSelected:S.has(i),treeState:u})}}catch(e){r=!0,n=e}finally{try{a||null==s.return||s.return()}finally{if(r)throw n}}}},[t,f,h,y,S,T,d,u]),React.useEffect(function(){if(c&&i){var e=new Set(m);v&&e.add(v);var a=propagateSelectChange(t,e,f,y),r=a.every,n=a.some,l=a.none,s=!0,d=!1,o=void 0;try{for(var u,h=r[Symbol.iterator]();!(s=(u=h.next()).done);s=!0){var b=u.value;f.has(b)||p({type:treeTypes.select,id:b,multiSelect:i,keepFocus:!0,NotUserAction:!0,lastInteractedWith:v})}}catch(e){d=!0,o=e}finally{try{s||null==h.return||h.return()}finally{if(d)throw o}}var I=!0,g=!1,T=void 0;try{for(var A,w=n[Symbol.iterator]();!(I=(A=w.next()).done);I=!0){var x=A.value;S.has(x)||p({type:treeTypes.halfSelect,id:x,multiSelect:i,keepFocus:!0,NotUserAction:!0,lastInteractedWith:v})}}catch(e){g=!0,T=e}finally{try{I||null==w.return||w.return()}finally{if(g)throw T}}var _=!0,E=!1,W=void 0;try{for(var P,C=l[Symbol.iterator]();!(_=(P=C.next()).done);_=!0){var N=P.value;(f.has(N)||S.has(N))&&p({type:treeTypes.deselect,id:N,multiSelect:i,keepFocus:!0,NotUserAction:!0,lastInteractedWith:v})}}catch(e){E=!0,W=e}finally{try{_||null==C.return||C.return()}finally{if(E)throw W}}}},[t,i,c,f,h,y,S,I,g,m,v]),React.useEffect(function(){if(null!=v&&null!=b){var e=l.current[b];focusRef(e)}},[b,l,v]),[u,p]},clickActions={select:"SELECT",focus:"FOCUS",exclusiveSelect:"EXCLUSIVE_SELECT"},noop=function(){},TreeView=React__default.forwardRef(function(e,t){var a=e.data,r=e.nodeRenderer,n=e.onSelect,l=void 0===n?noop:n,s=e.onExpand,d=void 0===s?noop:s,i=e.className,c=void 0===i?"":i,o=e.multiSelect,u=void 0!==o&&o,p=e.propagateSelect,f=void 0!==p&&p,h=e.propagateSelectUpwards,y=void 0!==h&&h,b=e.propagateCollapse,S=void 0!==b&&b,I=e.expandOnKeyboardSelect,v=void 0!==I&&I,g=e.togglableSelect,m=void 0!==g&&g,T=e.defaultExpandedIds,A=void 0===T?[]:T,w=e.defaultSelectedIds,x=void 0===w?[]:w,_=e.defaultDisabledIds,E=void 0===_?[]:_,W=e.clickAction,P=void 0===W?clickActions.select:W,C=e.onBlur,N=_objectWithoutProperties(e,["data","nodeRenderer","onSelect","onExpand","className","multiSelect","propagateSelect","propagateSelectUpwards","propagateCollapse","expandOnKeyboardSelect","togglableSelect","defaultExpandedIds","defaultSelectedIds","defaultDisabledIds","clickAction","onBlur"]),R=React.useRef({}),O=_slicedToArray(useTree({data:a,defaultExpandedIds:A,defaultSelectedIds:x,defaultDisabledIds:E,nodeRefs:R,onSelect:l,onExpand:d,multiSelect:u,propagateSelect:f,propagateSelectUpwards:y}),2),j=O[0],D=O[1];f=f&&u;var k=React.useRef();return null!=t&&(k=t),React__default.createElement("ul",_extends({className:cx(baseClassNames.root,c),role:"tree","aria-multiselectable":u,ref:k,onBlur:function(e){return onComponentBlur(e,k.current,function(){C&&C({treeState:j,dispatch:D}),D({type:treeTypes.blur})})},onKeyDown:handleKeyDown({data:a,tabbableId:j.tabbableId,expandedIds:j.expandedIds,selectedIds:j.selectedIds,disabledIds:j.disabledIds,halfSelectedIds:j.halfSelectedIds,dispatch:D,propagateCollapse:S,propagateSelect:f,multiSelect:u,expandOnKeyboardSelect:v,togglableSelect:m})},N),a[0].children.map(function(e,t){return React__default.createElement(Node,{key:e,selectedIds:j.selectedIds,tabbableId:j.tabbableId,isFocused:j.isFocused,expandedIds:j.expandedIds,disabledIds:j.disabledIds,halfSelectedIds:j.halfSelectedIds,lastUserSelect:j.lastUserSelect,lastInteractedWith:j.lastInteractedWith,dispatch:D,data:a,element:a[e],nodeRefs:R,baseClassNames:baseClassNames,nodeRenderer:r,setsize:a[0].children.length,posinset:t+1,level:1,propagateCollapse:S,propagateSelect:f,multiSelect:u,togglableSelect:m,clickAction:P,state:j})}))}),Node=function e(t){var a=t.element,r=t.dispatch,n=t.data,l=t.selectedIds,s=t.tabbableId,d=t.isFocused,i=t.expandedIds,c=t.disabledIds,o=t.halfSelectedIds,u=t.lastUserSelect,p=t.nodeRefs,f=t.baseClassNames,h=t.nodeRenderer,y=t.setsize,b=t.posinset,S=t.level,I=t.propagateCollapse,v=t.propagateSelect,g=t.multiSelect,m=t.togglableSelect,T=t.clickAction,A=t.lastInteractedWith,w=t.state,x=function(e){if(!(e.ctrlKey||e.altKey||e.shiftKey))if(i.has(a.id)&&I){var t=[a.id].concat(_toConsumableArray(getDescendants(n,a.id,new Set)));r({type:treeTypes.collapseMany,ids:t,lastInteractedWith:a.id})}else r({type:treeTypes.toggle,id:a.id,lastInteractedWith:a.id})},_=function(){r({type:treeTypes.focus,id:a.id,lastInteractedWith:a.id})},E=function(e){if(e.shiftKey){var t=getAccessibleRange({data:n,expandedIds:i,from:u,to:a.id,lastInteractedWith:a.id}).filter(function(e){return!c.has(e)});t=v?propagatedIds(n,t,c):t,r({type:treeTypes.exclusiveChangeSelectMany,select:!0,multiSelect:g,ids:t,lastInteractedWith:a.id})}else e.ctrlKey||T===clickActions.select?(r({type:m?treeTypes.toggleSelect:treeTypes.select,id:a.id,multiSelect:g,lastInteractedWith:a.id}),v&&!c.has(a.id)&&r({type:treeTypes.changeSelectMany,ids:propagatedIds(n,[a.id],c),select:!m||!l.has(a.id),multiSelect:g,lastInteractedWith:a.id})):T===clickActions.exclusiveSelect?r({type:m?treeTypes.toggleSelect:treeTypes.select,id:a.id,multiSelect:!1,lastInteractedWith:a.id}):T===clickActions.focus&&r({type:treeTypes.focus,id:a.id,lastInteractedWith:a.id})},W=function(e){var t;return cx(e,(_defineProperty(t={},"".concat(e,"--expanded"),i.has(a.id)),_defineProperty(t,"".concat(e,"--selected"),l.has(a.id)),_defineProperty(t,"".concat(e,"--focused"),s===a.id&&d),t))};return isBranchNode(n,a.id)?React__default.createElement("li",{role:"treeitem","aria-expanded":i.has(a.id),"aria-selected":getAriaSelected(l.has(a.id),c.has(a.id),g),"aria-setsize":y,"aria-posinset":b,"aria-level":S,disabled:c.has(a.id),tabIndex:s===a.id?0:-1,ref:function(e){return p.current[a.id]=e},className:f.branchWrapper},h({element:a,isBranch:!0,isSelected:l.has(a.id),isHalfSelected:o.has(a.id),isExpanded:i.has(a.id),isDisabled:c.has(a.id),dispatch:r,getNodeProps:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=e.onClick;return{onClick:null==t?composeHandlers(E,x,_):composeHandlers(t,_),className:cx(W(f.node),f.branch)}},setsize:y,posinset:b,level:S,handleSelect:E,handleExpand:x,treeState:w}),i.has(a.id)&&React__default.createElement("ul",{role:"group",className:W(f.nodeGroup)},a.children.map(function(t,y){return React__default.createElement(e,{key:t,selectedIds:l,tabbableId:s,isFocused:d,expandedIds:i,halfSelectedIds:o,disabledIds:c,lastUserSelect:u,dispatch:r,data:n,element:n[t],nodeRefs:p,baseClassNames:f,nodeRenderer:h,setsize:a.children.length,posinset:y+1,level:S+1,propagateCollapse:I,propagateSelect:v,multiSelect:g,togglableSelect:m,clickAction:T,lastInteractedWith:A,state:w})}))):React__default.createElement("li",{role:"none",className:W(f.leafListItem)},h({element:a,isBranch:!1,isSelected:l.has(a.id),isHalfSelected:void 0,isExpanded:!1,isDisabled:c.has(a.id),dispatch:r,getNodeProps:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=e.onClick;return{role:"treeitem",tabIndex:s===a.id?0:-1,onClick:composeHandlers(null==t?E:t,_),ref:function(e){return p.current[a.id]=e},className:cx(W(f.node),f.leaf),"aria-setsize":y,"aria-posinset":b,"aria-level":S,"aria-selected":getAriaSelected(l.has(a.id),c.has(a.id),g),disabled:c.has(a.id)}},setsize:y,posinset:b,level:S,handleSelect:E,handleExpand:noop,treeState:w}))},handleKeyDown=function(e){var t=e.data,a=e.expandedIds,r=e.selectedIds,n=e.disabledIds,l=e.tabbableId,s=e.dispatch,d=e.propagateCollapse,i=e.propagateSelect,c=e.multiSelect,o=e.expandOnKeyboardSelect,u=e.togglableSelect;return function(e){var p=t[l],f=p.id;if(e.ctrlKey){if("a"===e.key){e.preventDefault();t[0];var h=_objectWithoutProperties(t,["0"]),y=Object.values(h).map(function(e){return e.id}).filter(function(e){return!n.has(e)});s({type:treeTypes.changeSelectMany,multiSelect:c,select:Array.from(r).filter(function(e){return!n.has(e)}).length!==y.length,ids:y,lastInteractedWith:p.id})}else if(e.shiftKey&&("Home"===e.key||"End"===e.key)){var b="Home"===e.key?t[0].children[0]:getLastAccessible(t,f,a),S=getAccessibleRange({data:t,expandedIds:a,from:f,to:b}).filter(function(e){return!n.has(e)});s({type:treeTypes.changeSelectMany,multiSelect:c,select:!0,ids:i?propagatedIds(t,S,n):S}),s({type:treeTypes.focus,id:b,lastInteractedWith:b})}}else{if(e.shiftKey)switch(e.key){case"ArrowUp":e.preventDefault();var I=getPreviousAccessible(t,f,a);return void(null==I&&n.has(I)||(s({type:treeTypes.changeSelectMany,ids:i?propagatedIds(t,[I],n):[I],select:!0,multiSelect:c,lastInteractedWith:I}),s({type:treeTypes.focus,id:I,lastInteractedWith:I})));case"ArrowDown":e.preventDefault();var v=getNextAccessible(t,f,a);return void(null==v&&n.has(v)||(s({type:treeTypes.changeSelectMany,ids:i?propagatedIds(t,[v],n):[v],multiSelect:c,select:!0,lastInteractedWith:v}),s({type:treeTypes.focus,id:v,lastInteractedWith:v})))}switch(e.key){case"ArrowDown":e.preventDefault();var g=getNextAccessible(t,f,a);return void(null!=g&&s({type:treeTypes.focus,id:g,lastInteractedWith:g}));case"ArrowUp":e.preventDefault();var m=getPreviousAccessible(t,f,a);return void(null!=m&&s({type:treeTypes.focus,id:m,lastInteractedWith:m}));case"ArrowLeft":if(e.preventDefault(),isBranchNode(t,f)&&a.has(l))if(d){var T=[f].concat(_toConsumableArray(getDescendants(t,f,new Set)));s({type:treeTypes.collapseMany,ids:T,lastInteractedWith:p.id})}else s({type:treeTypes.collapse,id:f,lastInteractedWith:f});else if(!t[0].children.includes(f)){var A=getParent(t,f);s({type:treeTypes.focus,id:A,lastInteractedWith:A})}return;case"ArrowRight":return e.preventDefault(),void(isBranchNode(t,f)&&(a.has(l)?s({type:treeTypes.focus,id:p.children[0],lastInteractedWith:p.children[0]}):s({type:treeTypes.expand,id:f,lastInteractedWith:f})));case"Home":e.preventDefault(),s({type:treeTypes.focus,id:t[0].children[0],lastInteractedWith:t[0].children[0]});break;case"End":e.preventDefault();var w=getLastAccessible(t,t[0].id,a);return void s({type:treeTypes.focus,id:w,lastInteractedWith:w});case"*":e.preventDefault();var x=t[getParent(t,f)].children.filter(function(e){return isBranchNode(t,e)});return void s({type:treeTypes.expandMany,ids:x,lastInteractedWith:f});case"Enter":case" ":case"Spacebar":return e.preventDefault(),!n.has(f)&&s({type:treeTypes.changeSelectMany,ids:i?propagatedIds(t,[f],n):[f],select:!u||!r.has(f),multiSelect:c,lastInteractedWith:f}),void(o&&s({type:treeTypes.toggle,id:f,lastInteractedWith:f}));default:if(1===e.key.length)for(var _=getNextAccessible(t,f,a);_!==f;)if(null!=_){if(t[_].name[0].toLowerCase()===e.key.toLowerCase())return void s({type:treeTypes.focus,id:_,lastInteractedWith:f});_=getNextAccessible(t,_,a)}else _=t[0].children[0];return}}}};TreeView.propTypes={data:PropTypes.array.isRequired,onSelect:PropTypes.func,onExpand:PropTypes.func,className:PropTypes.string,nodeRenderer:PropTypes.func.isRequired,defaultExpandedIds:PropTypes.array,defaultSelectedIds:PropTypes.array,defaultDisabledIds:PropTypes.array,propagateCollapse:PropTypes.bool,propagateSelect:PropTypes.bool,propagateSelectUpwards:PropTypes.bool,multiSelect:PropTypes.bool,expandOnKeyboardSelect:PropTypes.bool,togglableSelect:PropTypes.bool,clickAction:PropTypes.oneOf(Object.values(clickActions))},exports.default=TreeView,exports.flattenTree=flattenTree; | ||
"use strict";function _interopDefault(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var React=require("react"),React__default=_interopDefault(React),cx=_interopDefault(require("classnames")),PropTypes=_interopDefault(require("prop-types"));function _defineProperty(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function _extends(){return(_extends=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(e[a]=r[a])}return e}).apply(this,arguments)}function ownKeys(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);t&&(a=a.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),r.push.apply(r,a)}return r}function _objectSpread2(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?ownKeys(r,!0).forEach(function(t){_defineProperty(e,t,r[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):ownKeys(r).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))})}return e}function _objectWithoutPropertiesLoose(e,t){if(null==e)return{};var r,a,n={},l=Object.keys(e);for(a=0;a<l.length;a++)r=l[a],t.indexOf(r)>=0||(n[r]=e[r]);return n}function _objectWithoutProperties(e,t){if(null==e)return{};var r,a,n=_objectWithoutPropertiesLoose(e,t);if(Object.getOwnPropertySymbols){var l=Object.getOwnPropertySymbols(e);for(a=0;a<l.length;a++)r=l[a],t.indexOf(r)>=0||Object.prototype.propertyIsEnumerable.call(e,r)&&(n[r]=e[r])}return n}function _slicedToArray(e,t){return _arrayWithHoles(e)||_iterableToArrayLimit(e,t)||_nonIterableRest()}function _toConsumableArray(e){return _arrayWithoutHoles(e)||_iterableToArray(e)||_nonIterableSpread()}function _arrayWithoutHoles(e){if(Array.isArray(e)){for(var t=0,r=new Array(e.length);t<e.length;t++)r[t]=e[t];return r}}function _arrayWithHoles(e){if(Array.isArray(e))return e}function _iterableToArray(e){if(Symbol.iterator in Object(e)||"[object Arguments]"===Object.prototype.toString.call(e))return Array.from(e)}function _iterableToArrayLimit(e,t){var r=[],a=!0,n=!1,l=void 0;try{for(var s,d=e[Symbol.iterator]();!(a=(s=d.next()).done)&&(r.push(s.value),!t||r.length!==t);a=!0);}catch(e){n=!0,l=e}finally{try{a||null==d.return||d.return()}finally{if(n)throw l}}return r}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance")}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}var composeHandlers=function(){for(var e=arguments.length,t=new Array(e),r=0;r<e;r++)t[r]=arguments[r];return function(e){for(var r=0,a=t;r<a.length;r++){var n=a[r];if(n&&n(e),e.defaultPrevented)break}}},difference=function(e,t){var r=new Set,a=!0,n=!1,l=void 0;try{for(var s,d=e[Symbol.iterator]();!(a=(s=d.next()).done);a=!0){var i=s.value;t.has(i)||r.add(i)}}catch(e){n=!0,l=e}finally{try{a||null==d.return||d.return()}finally{if(n)throw l}}return r},symmetricDifference=function(e,t){return new Set([].concat(_toConsumableArray(difference(e,t)),_toConsumableArray(difference(t,e))))},usePrevious=function(e){var t=React.useRef();return React.useEffect(function(){t.current=e},[e]),t.current},isBranchNode=function(e,t){return null!=e[t].children&&e[t].children.length>0},focusRef=function(e){null!=e&&e.focus&&e.focus()},getParent=function(e,t){return e[t].parent},getDescendants=function(e,t,r){var a=[];return function e(t,n){var l=t[n];if(null!=l.children){var s=!0,d=!1,i=void 0;try{for(var c,o=l.children.filter(function(e){return!r.has(e)})[Symbol.iterator]();!(s=(c=o.next()).done);s=!0){var u=c.value;a.push(u),e(t,u)}}catch(e){d=!0,i=e}finally{try{s||null==o.return||o.return()}finally{if(d)throw i}}}}(e,t),a},getSibling=function(e,t,r){var a=getParent(e,t);if(null!=a){var n=e[a],l=n.children.indexOf(t)+r;if(n.children[l])return n.children[l]}return null},getLastAccessible=function(e,t,r){var a=e[t];for(e[0].id===t&&(a=e[e[t].children[e[t].children.length-1]]);r.has(a.id)&&isBranchNode(e,a.id);)a=e[a.children[a.children.length-1]];return a.id},getPreviousAccessible=function(e,t,r){if(t===e[0].children[0])return null;var a=getSibling(e,t,-1);return null==a?getParent(e,t):getLastAccessible(e,a,r)},getNextAccessible=function(e,t,r){var a=e[t].id;if(isBranchNode(e,a)&&r.has(a))return e[a].children[0];for(;;){var n=getSibling(e,a,1);if(null!=n)return n;if(null==(a=getParent(e,a)))return null}},propagateSelectChange=function(e,t,r,a){var n={every:new Set,some:new Set,none:new Set},l=!0,s=!1,d=void 0;try{for(var i,c=t[Symbol.iterator]();!(l=(i=c.next()).done);l=!0)for(var o=i.value;;){var u=getParent(e,o);if(0===u||a.has(u))break;var p=e[u].children.filter(function(e){return!a.has(e)});if(0===p.length)break;p.some(function(e){return r.has(e)})?p.every(function(e){return r.has(e)})?n.every.add(u):n.some.add(u):n.none.add(u),o=u}}catch(e){s=!0,d=e}finally{try{l||null==c.return||c.return()}finally{if(s)throw d}}return n},getAccessibleRange=function(e){var t=e.data,r=e.expandedIds,a=e.from,n=e.to,l=[],s=Object.keys(t).length,d=0,i=a;if(l.push(a),a<n)for(;d<s&&(i=getNextAccessible(t,i,r),l.push(i),null!=i&&i!==n);)d+=1;else if(a>n)for(;d<s&&(i=getPreviousAccessible(t,i,r),l.push(i),null!=i&&i!==n);)d+=1;return l},flattenTree=function(e){var t=0,r=[];return function e(a,n){if(a.id=t,a.parent=n,r[t]=a,t+=1,null!=a.children&&0!==a.children.length){var l=!0,s=!1,d=void 0;try{for(var i,c=a.children[Symbol.iterator]();!(l=(i=c.next()).done);l=!0){e(i.value,a.id)}}catch(e){s=!0,d=e}finally{try{l||null==c.return||c.return()}finally{if(s)throw d}}a.children=a.children.map(function(e){return e.id})}}(e,null),r},getAriaSelected=function(e,t,r){if(!t)return r?e:!!e||void 0},propagatedIds=function(e,t,r){return t.concat.apply(t,_toConsumableArray(t.filter(function(t){return isBranchNode(e,t)}).map(function(t){return getDescendants(e,t,r)})))},isIE=function(){return window.navigator.userAgent.match(/Trident/)},onComponentBlur=function(e,t,r){isIE()?setTimeout(function(){return!t.contains(document.activeElement)&&r()},0):!t.contains(e.nativeEvent.relatedTarget)&&r()},baseClassNames={root:"tree",node:"tree-node",branch:"tree-node__branch",branchWrapper:"tree-branch-wrapper",leafListItem:"tree-leaf-list-item",leaf:"tree-node__leaf",nodeGroup:"tree-node-group"},treeTypes={collapse:"COLLAPSE",collapseMany:"COLLAPSE_MANY",expand:"EXPAND",expandMany:"EXPAND_MANY",halfSelect:"HALF_SELECT",select:"SELECT",deselect:"DESELECT",toggle:"TOGGLE",toggleSelect:"TOGGLE_SELECT",changeSelectMany:"SELECT_MANY",exclusiveSelectMany:"EXCLUSIVE_SELECT_MANY",exclusiveChangeSelectMany:"EXCLUSIVE_CHANGE_SELECT_MANY",focus:"FOCUS",blur:"BLUR",disable:"DISABLE",enable:"ENABLE"},treeReducer=function(e,t){switch(t.type){case treeTypes.collapse:var r=new Set(e.expandedIds);return r.delete(t.id),_objectSpread2({},e,{expandedIds:r,tabbableId:t.id,isFocused:!0,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case treeTypes.collapseMany:var a=new Set(e.expandedIds),n=!0,l=!1,s=void 0;try{for(var d,i=t.ids[Symbol.iterator]();!(n=(d=i.next()).done);n=!0){var c=d.value;a.delete(c)}}catch(e){l=!0,s=e}finally{try{n||null==i.return||i.return()}finally{if(l)throw s}}return _objectSpread2({},e,{expandedIds:a,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case treeTypes.expand:var o=new Set(e.expandedIds);return o.add(t.id),_objectSpread2({},e,{expandedIds:o,tabbableId:t.id,isFocused:!0,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case treeTypes.expandMany:var u=new Set([].concat(_toConsumableArray(e.expandedIds),_toConsumableArray(t.ids)));return _objectSpread2({},e,{expandedIds:u,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case treeTypes.toggle:var p=new Set(e.expandedIds);return e.expandedIds.has(t.id)?p.delete(t.id):p.add(t.id),_objectSpread2({},e,{expandedIds:p,tabbableId:t.id,isFocused:!0,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case treeTypes.halfSelect:if(e.disabledIds.has(t.id))return e;var f=new Set(e.halfSelectedIds),h=new Set(e.selectedIds);return f.add(t.id),h.delete(t.id),_objectSpread2({},e,{selectedIds:h,halfSelectedIds:f,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case treeTypes.select:if(e.disabledIds.has(t.id))return e;var y;t.multiSelect?(y=new Set(e.selectedIds)).add(t.id):(y=new Set).add(t.id);var b=new Set(e.halfSelectedIds);return b.delete(t.id),_objectSpread2({},e,{selectedIds:y,halfSelectedIds:b,tabbableId:t.keepFocus?e.tabbableId:t.id,isFocused:!0,lastUserSelect:t.NotUserAction?e.lastUserSelect:t.id,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case treeTypes.deselect:if(e.disabledIds.has(t.id))return e;var S;t.multiSelect?(S=new Set(e.selectedIds)).delete(t.id):S=new Set;var I=new Set(e.halfSelectedIds);return I.delete(t.id),_objectSpread2({},e,{selectedIds:S,halfSelectedIds:I,tabbableId:t.keepFocus?e.tabbableId:t.id,isFocused:!0,lastUserSelect:t.NotUserAction?e.lastUserSelect:t.id,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case treeTypes.toggleSelect:if(e.disabledIds.has(t.id))return e;var v,g=e.selectedIds.has(t.id);t.multiSelect?(v=new Set(e.selectedIds),g?v.delete(t.id):v.add(t.id)):(v=new Set,g||v.add(t.id));var m=new Set(e.halfSelectedIds);return m.delete(t.id),_objectSpread2({},e,{selectedIds:v,halfSelectedIds:m,tabbableId:t.id,isFocused:!0,lastUserSelect:t.NotUserAction?e.lastUserSelect:t.id,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case treeTypes.changeSelectMany:var T,A=t.ids.filter(function(t){return!e.disabledIds.has(t)});if(t.multiSelect){T=t.select?new Set([].concat(_toConsumableArray(e.selectedIds),_toConsumableArray(A))):difference(e.selectedIds,new Set(A));var w=difference(e.halfSelectedIds,T);return _objectSpread2({},e,{selectedIds:T,halfSelectedIds:w,lastAction:t.type,lastInteractedWith:t.lastInteractedWith})}return e;case treeTypes.exclusiveChangeSelectMany:var x,_=t.ids.filter(function(t){return!e.disabledIds.has(t)});if(t.multiSelect){x=t.select?new Set(_):difference(e.selectedIds,new Set(_));var E=difference(e.halfSelectedIds,x);return _objectSpread2({},e,{selectedIds:x,halfSelectedIds:E,lastAction:t.type,lastInteractedWith:t.lastInteractedWith})}return e;case treeTypes.focus:return _objectSpread2({},e,{tabbableId:t.id,isFocused:!0,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case treeTypes.blur:return _objectSpread2({},e,{isFocused:!1});case treeTypes.disable:var W=new Set(e.disabledIds);return W.add(t.id),_objectSpread2({},e,{disabledIds:W});case treeTypes.enable:var P=new Set(e.disabledIds);return P.delete(t.id),_objectSpread2({},e,{disabledIds:P});default:throw new Error("Invalid action passed to the reducer")}},useTree=function(e){var t=e.data,r=e.defaultExpandedIds,a=e.defaultSelectedIds,n=e.defaultDisabledIds,l=e.nodeRefs,s=e.onSelect,d=e.onExpand,i=e.multiSelect,c=e.propagateSelectUpwards,o=_slicedToArray(React.useReducer(treeReducer,{selectedIds:new Set(a),tabbableId:t[0].children[0],isFocused:!1,expandedIds:new Set(r),halfSelectedIds:new Set,lastUserSelect:t[0].children[0],lastInteractedWith:null,disabledIds:new Set(n)}),2),u=o[0],p=o[1],f=u.selectedIds,h=u.expandedIds,y=u.disabledIds,b=u.tabbableId,S=u.halfSelectedIds,I=u.lastAction,v=u.lastInteractedWith,g=usePrevious(f)||new Set,m=symmetricDifference(f,g);React.useEffect(function(){if(s!==noop){var e=!0,r=!1,a=void 0;try{for(var n,l=m[Symbol.iterator]();!(e=(n=l.next()).done);e=!0){var d=n.value,i=isBranchNode(t,d);s({element:t[d],isBranch:i,isExpanded:i?h.has(d):void 0,isSelected:f.has(d),isDisabled:y.has(d),isHalfSelected:i?S.has(d):void 0,treeState:u})}}catch(e){r=!0,a=e}finally{try{e||null==l.return||l.return()}finally{if(r)throw a}}}},[t,f,h,y,S,m,s,u]);var T=usePrevious(h)||new Set;return React.useEffect(function(){var e=symmetricDifference(h,T);if(d!==noop){var r=!0,a=!1,n=void 0;try{for(var l,s=e[Symbol.iterator]();!(r=(l=s.next()).done);r=!0){var i=l.value;d({element:t[i],isExpanded:h.has(i),isSelected:f.has(i),isDisabled:y.has(i),isHalfSelected:S.has(i),treeState:u})}}catch(e){a=!0,n=e}finally{try{r||null==s.return||s.return()}finally{if(a)throw n}}}},[t,f,h,y,S,T,d,u]),React.useEffect(function(){if(c&&i){var e=new Set(m);v&&e.add(v);var r=propagateSelectChange(t,e,f,y),a=r.every,n=r.some,l=r.none,s=!0,d=!1,o=void 0;try{for(var u,h=a[Symbol.iterator]();!(s=(u=h.next()).done);s=!0){var b=u.value;f.has(b)||p({type:treeTypes.select,id:b,multiSelect:i,keepFocus:!0,NotUserAction:!0,lastInteractedWith:v})}}catch(e){d=!0,o=e}finally{try{s||null==h.return||h.return()}finally{if(d)throw o}}var I=!0,g=!1,T=void 0;try{for(var A,w=n[Symbol.iterator]();!(I=(A=w.next()).done);I=!0){var x=A.value;S.has(x)||p({type:treeTypes.halfSelect,id:x,multiSelect:i,keepFocus:!0,NotUserAction:!0,lastInteractedWith:v})}}catch(e){g=!0,T=e}finally{try{I||null==w.return||w.return()}finally{if(g)throw T}}var _=!0,E=!1,W=void 0;try{for(var P,C=l[Symbol.iterator]();!(_=(P=C.next()).done);_=!0){var N=P.value;(f.has(N)||S.has(N))&&p({type:treeTypes.deselect,id:N,multiSelect:i,keepFocus:!0,NotUserAction:!0,lastInteractedWith:v})}}catch(e){E=!0,W=e}finally{try{_||null==C.return||C.return()}finally{if(E)throw W}}}},[t,i,c,f,h,y,S,I,g,m,v]),React.useEffect(function(){if(null!=v&&null!=b){var e=l.current[b];focusRef(e)}},[b,l,v]),[u,p]},clickActions={select:"SELECT",focus:"FOCUS",exclusiveSelect:"EXCLUSIVE_SELECT"},noop=function(){},TreeView=React__default.forwardRef(function(e,t){var r=e.data,a=e.nodeRenderer,n=e.onSelect,l=void 0===n?noop:n,s=e.onExpand,d=void 0===s?noop:s,i=e.className,c=void 0===i?"":i,o=e.multiSelect,u=void 0!==o&&o,p=e.propagateSelect,f=void 0!==p&&p,h=e.propagateSelectUpwards,y=void 0!==h&&h,b=e.propagateCollapse,S=void 0!==b&&b,I=e.expandOnKeyboardSelect,v=void 0!==I&&I,g=e.togglableSelect,m=void 0!==g&&g,T=e.defaultExpandedIds,A=void 0===T?[]:T,w=e.defaultSelectedIds,x=void 0===w?[]:w,_=e.defaultDisabledIds,E=void 0===_?[]:_,W=e.clickAction,P=void 0===W?clickActions.select:W,C=e.onBlur,N=_objectWithoutProperties(e,["data","nodeRenderer","onSelect","onExpand","className","multiSelect","propagateSelect","propagateSelectUpwards","propagateCollapse","expandOnKeyboardSelect","togglableSelect","defaultExpandedIds","defaultSelectedIds","defaultDisabledIds","clickAction","onBlur"]),R=React.useRef({}),O=_slicedToArray(useTree({data:r,defaultExpandedIds:A,defaultSelectedIds:x,defaultDisabledIds:E,nodeRefs:R,onSelect:l,onExpand:d,multiSelect:u,propagateSelect:f,propagateSelectUpwards:y}),2),j=O[0],D=O[1];f=f&&u;var k=React.useRef();return null!=t&&(k=t),React__default.createElement("ul",_extends({className:cx(baseClassNames.root,c),role:"tree","aria-multiselectable":u,ref:k,onBlur:function(e){return onComponentBlur(e,k.current,function(){C&&C({treeState:j,dispatch:D}),D({type:treeTypes.blur})})},onKeyDown:handleKeyDown({data:r,tabbableId:j.tabbableId,expandedIds:j.expandedIds,selectedIds:j.selectedIds,disabledIds:j.disabledIds,halfSelectedIds:j.halfSelectedIds,dispatch:D,propagateCollapse:S,propagateSelect:f,multiSelect:u,expandOnKeyboardSelect:v,togglableSelect:m})},N),r[0].children.map(function(e,t){return React__default.createElement(Node,{key:e,selectedIds:j.selectedIds,tabbableId:j.tabbableId,isFocused:j.isFocused,expandedIds:j.expandedIds,disabledIds:j.disabledIds,halfSelectedIds:j.halfSelectedIds,lastUserSelect:j.lastUserSelect,lastInteractedWith:j.lastInteractedWith,dispatch:D,data:r,element:r[e],nodeRefs:R,baseClassNames:baseClassNames,nodeRenderer:a,setsize:r[0].children.length,posinset:t+1,level:1,propagateCollapse:S,propagateSelect:f,multiSelect:u,togglableSelect:m,clickAction:P,state:j})}))}),Node=function e(t){var r=t.element,a=t.dispatch,n=t.data,l=t.selectedIds,s=t.tabbableId,d=t.isFocused,i=t.expandedIds,c=t.disabledIds,o=t.halfSelectedIds,u=t.lastUserSelect,p=t.nodeRefs,f=t.baseClassNames,h=t.nodeRenderer,y=t.setsize,b=t.posinset,S=t.level,I=t.propagateCollapse,v=t.propagateSelect,g=t.multiSelect,m=t.togglableSelect,T=t.clickAction,A=t.lastInteractedWith,w=t.state,x=function(e){if(!(e.ctrlKey||e.altKey||e.shiftKey))if(i.has(r.id)&&I){var t=[r.id].concat(_toConsumableArray(getDescendants(n,r.id,new Set)));a({type:treeTypes.collapseMany,ids:t,lastInteractedWith:r.id})}else a({type:treeTypes.toggle,id:r.id,lastInteractedWith:r.id})},_=function(){a({type:treeTypes.focus,id:r.id,lastInteractedWith:r.id})},E=function(e){if(e.shiftKey){var t=getAccessibleRange({data:n,expandedIds:i,from:u,to:r.id,lastInteractedWith:r.id}).filter(function(e){return!c.has(e)});t=v?propagatedIds(n,t,c):t,a({type:treeTypes.exclusiveChangeSelectMany,select:!0,multiSelect:g,ids:t,lastInteractedWith:r.id})}else e.ctrlKey||T===clickActions.select?(a({type:m?treeTypes.toggleSelect:treeTypes.select,id:r.id,multiSelect:g,lastInteractedWith:r.id}),v&&!c.has(r.id)&&a({type:treeTypes.changeSelectMany,ids:propagatedIds(n,[r.id],c),select:!m||!l.has(r.id),multiSelect:g,lastInteractedWith:r.id})):T===clickActions.exclusiveSelect?a({type:m?treeTypes.toggleSelect:treeTypes.select,id:r.id,multiSelect:!1,lastInteractedWith:r.id}):T===clickActions.focus&&a({type:treeTypes.focus,id:r.id,lastInteractedWith:r.id})},W=function(e){var t;return cx(e,(_defineProperty(t={},"".concat(e,"--expanded"),i.has(r.id)),_defineProperty(t,"".concat(e,"--selected"),l.has(r.id)),_defineProperty(t,"".concat(e,"--focused"),s===r.id&&d),t))};return isBranchNode(n,r.id)?React__default.createElement("li",{role:"treeitem","aria-expanded":i.has(r.id),"aria-selected":getAriaSelected(l.has(r.id),c.has(r.id),g),"aria-setsize":y,"aria-posinset":b,"aria-level":S,disabled:c.has(r.id),tabIndex:s===r.id?0:-1,ref:function(e){return p.current[r.id]=e},className:f.branchWrapper},h({element:r,isBranch:!0,isSelected:l.has(r.id),isHalfSelected:o.has(r.id),isExpanded:i.has(r.id),isDisabled:c.has(r.id),dispatch:a,getNodeProps:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=e.onClick;return{onClick:null==t?composeHandlers(E,x,_):composeHandlers(t,_),className:cx(W(f.node),f.branch)}},setsize:y,posinset:b,level:S,handleSelect:E,handleExpand:x,treeState:w}),i.has(r.id)&&React__default.createElement("ul",{role:"group",className:W(f.nodeGroup)},r.children.map(function(t,y){return React__default.createElement(e,{key:t,selectedIds:l,tabbableId:s,isFocused:d,expandedIds:i,halfSelectedIds:o,disabledIds:c,lastUserSelect:u,dispatch:a,data:n,element:n[t],nodeRefs:p,baseClassNames:f,nodeRenderer:h,setsize:r.children.length,posinset:y+1,level:S+1,propagateCollapse:I,propagateSelect:v,multiSelect:g,togglableSelect:m,clickAction:T,lastInteractedWith:A,state:w})}))):React__default.createElement("li",{role:"none",className:W(f.leafListItem)},h({element:r,isBranch:!1,isSelected:l.has(r.id),isHalfSelected:void 0,isExpanded:!1,isDisabled:c.has(r.id),dispatch:a,getNodeProps:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=e.onClick;return{role:"treeitem",tabIndex:s===r.id?0:-1,onClick:composeHandlers(null==t?E:t,_),ref:function(e){return p.current[r.id]=e},className:cx(W(f.node),f.leaf),"aria-setsize":y,"aria-posinset":b,"aria-level":S,"aria-selected":getAriaSelected(l.has(r.id),c.has(r.id),g),disabled:c.has(r.id)}},setsize:y,posinset:b,level:S,handleSelect:E,handleExpand:noop,treeState:w}))},handleKeyDown=function(e){var t=e.data,r=e.expandedIds,a=e.selectedIds,n=e.disabledIds,l=e.tabbableId,s=e.dispatch,d=e.propagateCollapse,i=e.propagateSelect,c=e.multiSelect,o=e.expandOnKeyboardSelect,u=e.togglableSelect;return function(e){var p=t[l],f=p.id;if(e.ctrlKey){if("a"===e.key){e.preventDefault();t[0];var h=_objectWithoutProperties(t,["0"]),y=Object.values(h).map(function(e){return e.id}).filter(function(e){return!n.has(e)});s({type:treeTypes.changeSelectMany,multiSelect:c,select:Array.from(a).filter(function(e){return!n.has(e)}).length!==y.length,ids:y,lastInteractedWith:p.id})}else if(e.shiftKey&&("Home"===e.key||"End"===e.key)){var b="Home"===e.key?t[0].children[0]:getLastAccessible(t,f,r),S=getAccessibleRange({data:t,expandedIds:r,from:f,to:b}).filter(function(e){return!n.has(e)});s({type:treeTypes.changeSelectMany,multiSelect:c,select:!0,ids:i?propagatedIds(t,S,n):S}),s({type:treeTypes.focus,id:b,lastInteractedWith:b})}}else{if(e.shiftKey)switch(e.key){case"ArrowUp":e.preventDefault();var I=getPreviousAccessible(t,f,r);return void(null==I&&n.has(I)||(s({type:treeTypes.changeSelectMany,ids:i?propagatedIds(t,[I],n):[I],select:!0,multiSelect:c,lastInteractedWith:I}),s({type:treeTypes.focus,id:I,lastInteractedWith:I})));case"ArrowDown":e.preventDefault();var v=getNextAccessible(t,f,r);return void(null==v&&n.has(v)||(s({type:treeTypes.changeSelectMany,ids:i?propagatedIds(t,[v],n):[v],multiSelect:c,select:!0,lastInteractedWith:v}),s({type:treeTypes.focus,id:v,lastInteractedWith:v})))}switch(e.key){case"ArrowDown":e.preventDefault();var g=getNextAccessible(t,f,r);return void(null!=g&&s({type:treeTypes.focus,id:g,lastInteractedWith:g}));case"ArrowUp":e.preventDefault();var m=getPreviousAccessible(t,f,r);return void(null!=m&&s({type:treeTypes.focus,id:m,lastInteractedWith:m}));case"ArrowLeft":if(e.preventDefault(),isBranchNode(t,f)&&r.has(l))if(d){var T=[f].concat(_toConsumableArray(getDescendants(t,f,new Set)));s({type:treeTypes.collapseMany,ids:T,lastInteractedWith:p.id})}else s({type:treeTypes.collapse,id:f,lastInteractedWith:f});else if(!t[0].children.includes(f)){var A=getParent(t,f);s({type:treeTypes.focus,id:A,lastInteractedWith:A})}return;case"ArrowRight":return e.preventDefault(),void(isBranchNode(t,f)&&(r.has(l)?s({type:treeTypes.focus,id:p.children[0],lastInteractedWith:p.children[0]}):s({type:treeTypes.expand,id:f,lastInteractedWith:f})));case"Home":e.preventDefault(),s({type:treeTypes.focus,id:t[0].children[0],lastInteractedWith:t[0].children[0]});break;case"End":e.preventDefault();var w=getLastAccessible(t,t[0].id,r);return void s({type:treeTypes.focus,id:w,lastInteractedWith:w});case"*":e.preventDefault();var x=t[getParent(t,f)].children.filter(function(e){return isBranchNode(t,e)});return void s({type:treeTypes.expandMany,ids:x,lastInteractedWith:f});case"Enter":case" ":case"Spacebar":return e.preventDefault(),!n.has(f)&&s({type:treeTypes.toggleSelect,id:p.id,select:!u||!a.has(f),multiSelect:c,lastInteractedWith:f}),void(o&&s({type:treeTypes.toggle,id:f,lastInteractedWith:f}));default:if(1===e.key.length)for(var _=getNextAccessible(t,f,r);_!==f;)if(null!=_){if(t[_].name[0].toLowerCase()===e.key.toLowerCase())return void s({type:treeTypes.focus,id:_,lastInteractedWith:f});_=getNextAccessible(t,_,r)}else _=t[0].children[0];return}}}};TreeView.propTypes={data:PropTypes.array.isRequired,onSelect:PropTypes.func,onExpand:PropTypes.func,className:PropTypes.string,nodeRenderer:PropTypes.func.isRequired,defaultExpandedIds:PropTypes.array,defaultSelectedIds:PropTypes.array,defaultDisabledIds:PropTypes.array,propagateCollapse:PropTypes.bool,propagateSelect:PropTypes.bool,propagateSelectUpwards:PropTypes.bool,multiSelect:PropTypes.bool,expandOnKeyboardSelect:PropTypes.bool,togglableSelect:PropTypes.bool,clickAction:PropTypes.oneOf(Object.values(clickActions))},exports.default=TreeView,exports.flattenTree=flattenTree; |
@@ -1,1 +0,1 @@ | ||
import e,{useRef as t,useEffect as a,useReducer as r}from"react";import n from"classnames";import l from"prop-types";function d(e,t,a){return t in e?Object.defineProperty(e,t,{value:a,enumerable:!0,configurable:!0,writable:!0}):e[t]=a,e}function i(){return(i=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var a=arguments[t];for(var r in a)Object.prototype.hasOwnProperty.call(a,r)&&(e[r]=a[r])}return e}).apply(this,arguments)}function s(e,t){var a=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),a.push.apply(a,r)}return a}function c(e){for(var t=1;t<arguments.length;t++){var a=null!=arguments[t]?arguments[t]:{};t%2?s(a,!0).forEach(function(t){d(e,t,a[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(a)):s(a).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(a,t))})}return e}function o(e,t){if(null==e)return{};var a,r,n=function(e,t){if(null==e)return{};var a,r,n={},l=Object.keys(e);for(r=0;r<l.length;r++)a=l[r],t.indexOf(a)>=0||(n[a]=e[a]);return n}(e,t);if(Object.getOwnPropertySymbols){var l=Object.getOwnPropertySymbols(e);for(r=0;r<l.length;r++)a=l[r],t.indexOf(a)>=0||Object.prototype.propertyIsEnumerable.call(e,a)&&(n[a]=e[a])}return n}function u(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var a=[],r=!0,n=!1,l=void 0;try{for(var d,i=e[Symbol.iterator]();!(r=(d=i.next()).done)&&(a.push(d.value),!t||a.length!==t);r=!0);}catch(e){n=!0,l=e}finally{try{r||null==i.return||i.return()}finally{if(n)throw l}}return a}(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}()}function f(e){return function(e){if(Array.isArray(e)){for(var t=0,a=new Array(e.length);t<e.length;t++)a[t]=e[t];return a}}(e)||function(e){if(Symbol.iterator in Object(e)||"[object Arguments]"===Object.prototype.toString.call(e))return Array.from(e)}(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance")}()}var h=function(){for(var e=arguments.length,t=new Array(e),a=0;a<e;a++)t[a]=arguments[a];return function(e){for(var a=0,r=t;a<r.length;a++){var n=r[a];if(n&&n(e),e.defaultPrevented)break}}},p=function(e,t){var a=new Set,r=!0,n=!1,l=void 0;try{for(var d,i=e[Symbol.iterator]();!(r=(d=i.next()).done);r=!0){var s=d.value;t.has(s)||a.add(s)}}catch(e){n=!0,l=e}finally{try{r||null==i.return||i.return()}finally{if(n)throw l}}return a},I=function(e,t){return new Set([].concat(f(p(e,t)),f(p(t,e))))},v=function(e){var r=t();return a(function(){r.current=e},[e]),r.current},S=function(e,t){return null!=e[t].children&&e[t].children.length>0},y=function(e,t){return e[t].parent},b=function(e,t,a){var r=[];return function e(t,n){var l=t[n];if(null!=l.children){var d=!0,i=!1,s=void 0;try{for(var c,o=l.children.filter(function(e){return!a.has(e)})[Symbol.iterator]();!(d=(c=o.next()).done);d=!0){var u=c.value;r.push(u),e(t,u)}}catch(e){i=!0,s=e}finally{try{d||null==o.return||o.return()}finally{if(i)throw s}}}}(e,t),r},m=function(e,t,a){var r=y(e,t);if(null!=r){var n=e[r],l=n.children.indexOf(t)+a;if(n.children[l])return n.children[l]}return null},w=function(e,t,a){var r=e[t];for(e[0].id===t&&(r=e[e[t].children[e[t].children.length-1]]);a.has(r.id)&&S(e,r.id);)r=e[r.children[r.children.length-1]];return r.id},g=function(e,t,a){if(t===e[0].children[0])return null;var r=m(e,t,-1);return null==r?y(e,t):w(e,r,a)},x=function(e,t,a){var r=e[t].id;if(S(e,r)&&a.has(r))return e[r].children[0];for(;;){var n=m(e,r,1);if(null!=n)return n;if(null==(r=y(e,r)))return null}},E=function(e){var t=e.data,a=e.expandedIds,r=e.from,n=e.to,l=[],d=Object.keys(t).length,i=0,s=r;if(l.push(r),r<n)for(;i<d&&(s=x(t,s,a),l.push(s),null!=s&&s!==n);)i+=1;else if(r>n)for(;i<d&&(s=g(t,s,a),l.push(s),null!=s&&s!==n);)i+=1;return l},W=function(e){var t=0,a=[];return function e(r,n){if(r.id=t,r.parent=n,a[t]=r,t+=1,null!=r.children&&0!==r.children.length){var l=!0,d=!1,i=void 0;try{for(var s,c=r.children[Symbol.iterator]();!(l=(s=c.next()).done);l=!0){e(s.value,r.id)}}catch(e){d=!0,i=e}finally{try{l||null==c.return||c.return()}finally{if(d)throw i}}r.children=r.children.map(function(e){return e.id})}}(e,null),a},A=function(e,t,a){if(!t)return a?e:!!e||void 0},O=function(e,t,a){return t.concat.apply(t,f(t.filter(function(t){return S(e,t)}).map(function(t){return b(e,t,a)})))},k=function(e,t,a){window.navigator.userAgent.match(/Trident/)?setTimeout(function(){return!t.contains(document.activeElement)&&a()},0):!t.contains(e.nativeEvent.relatedTarget)&&a()},C={root:"tree",node:"tree-node",branch:"tree-node__branch",branchWrapper:"tree-branch-wrapper",leafListItem:"tree-leaf-list-item",leaf:"tree-node__leaf",nodeGroup:"tree-node-group"},D="COLLAPSE",U="COLLAPSE_MANY",N="EXPAND",L="EXPAND_MANY",j="HALF_SELECT",F="SELECT",P="DESELECT",R="TOGGLE",T="TOGGLE_SELECT",K="SELECT_MANY",_="EXCLUSIVE_CHANGE_SELECT_MANY",B="FOCUS",H="BLUR",z="DISABLE",G="ENABLE",M=function(e,t){switch(t.type){case D:var a=new Set(e.expandedIds);return a.delete(t.id),c({},e,{expandedIds:a,tabbableId:t.id,isFocused:!0,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case U:var r=new Set(e.expandedIds),n=!0,l=!1,d=void 0;try{for(var i,s=t.ids[Symbol.iterator]();!(n=(i=s.next()).done);n=!0){var o=i.value;r.delete(o)}}catch(e){l=!0,d=e}finally{try{n||null==s.return||s.return()}finally{if(l)throw d}}return c({},e,{expandedIds:r,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case N:var u=new Set(e.expandedIds);return u.add(t.id),c({},e,{expandedIds:u,tabbableId:t.id,isFocused:!0,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case L:var h=new Set([].concat(f(e.expandedIds),f(t.ids)));return c({},e,{expandedIds:h,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case R:var I=new Set(e.expandedIds);return e.expandedIds.has(t.id)?I.delete(t.id):I.add(t.id),c({},e,{expandedIds:I,tabbableId:t.id,isFocused:!0,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case j:if(e.disabledIds.has(t.id))return e;var v=new Set(e.halfSelectedIds),S=new Set(e.selectedIds);return v.add(t.id),S.delete(t.id),c({},e,{selectedIds:S,halfSelectedIds:v,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case F:if(e.disabledIds.has(t.id))return e;var y;t.multiSelect?(y=new Set(e.selectedIds)).add(t.id):(y=new Set).add(t.id);var b=new Set(e.halfSelectedIds);return b.delete(t.id),c({},e,{selectedIds:y,halfSelectedIds:b,tabbableId:t.keepFocus?e.tabbableId:t.id,isFocused:!0,lastUserSelect:t.NotUserAction?e.lastUserSelect:t.id,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case P:if(e.disabledIds.has(t.id))return e;var m;t.multiSelect?(m=new Set(e.selectedIds)).delete(t.id):m=new Set;var w=new Set(e.halfSelectedIds);return w.delete(t.id),c({},e,{selectedIds:m,halfSelectedIds:w,tabbableId:t.keepFocus?e.tabbableId:t.id,isFocused:!0,lastUserSelect:t.NotUserAction?e.lastUserSelect:t.id,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case T:if(e.disabledIds.has(t.id))return e;var g,x=e.selectedIds.has(t.id);t.multiSelect?(g=new Set(e.selectedIds),x?g.delete(t.id):g.add(t.id)):(g=new Set,x||g.add(t.id));var E=new Set(e.halfSelectedIds);return E.delete(t.id),c({},e,{selectedIds:g,halfSelectedIds:E,tabbableId:t.id,isFocused:!0,lastUserSelect:t.NotUserAction?e.lastUserSelect:t.id,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case K:var W,A=t.ids.filter(function(t){return!e.disabledIds.has(t)});if(t.multiSelect){W=t.select?new Set([].concat(f(e.selectedIds),f(A))):p(e.selectedIds,new Set(A));var O=p(e.halfSelectedIds,W);return c({},e,{selectedIds:W,halfSelectedIds:O,lastAction:t.type,lastInteractedWith:t.lastInteractedWith})}return e;case _:var k,C=t.ids.filter(function(t){return!e.disabledIds.has(t)});if(t.multiSelect){k=t.select?new Set(C):p(e.selectedIds,new Set(C));var M=p(e.halfSelectedIds,k);return c({},e,{selectedIds:k,halfSelectedIds:M,lastAction:t.type,lastInteractedWith:t.lastInteractedWith})}return e;case B:return c({},e,{tabbableId:t.id,isFocused:!0,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case H:return c({},e,{isFocused:!1});case z:var X=new Set(e.disabledIds);return X.add(t.id),c({},e,{disabledIds:X});case G:var Y=new Set(e.disabledIds);return Y.delete(t.id),c({},e,{disabledIds:Y});default:throw new Error("Invalid action passed to the reducer")}},X=function(e){var t=e.data,n=e.defaultExpandedIds,l=e.defaultSelectedIds,d=e.defaultDisabledIds,i=e.nodeRefs,s=e.onSelect,c=e.onExpand,o=e.multiSelect,f=e.propagateSelectUpwards,h=u(r(M,{selectedIds:new Set(l),tabbableId:t[0].children[0],isFocused:!1,expandedIds:new Set(n),halfSelectedIds:new Set,lastUserSelect:t[0].children[0],lastInteractedWith:null,disabledIds:new Set(d)}),2),p=h[0],b=h[1],m=p.selectedIds,w=p.expandedIds,g=p.disabledIds,x=p.tabbableId,E=p.halfSelectedIds,W=p.lastAction,A=p.lastInteractedWith,O=v(m)||new Set,k=I(m,O);a(function(){if(s!==q){var e=!0,a=!1,r=void 0;try{for(var n,l=k[Symbol.iterator]();!(e=(n=l.next()).done);e=!0){var d=n.value,i=S(t,d);s({element:t[d],isBranch:i,isExpanded:i?w.has(d):void 0,isSelected:m.has(d),isDisabled:g.has(d),isHalfSelected:i?E.has(d):void 0,treeState:p})}}catch(e){a=!0,r=e}finally{try{e||null==l.return||l.return()}finally{if(a)throw r}}}},[t,m,w,g,E,k,s,p]);var C=v(w)||new Set;return a(function(){var e=I(w,C);if(c!==q){var a=!0,r=!1,n=void 0;try{for(var l,d=e[Symbol.iterator]();!(a=(l=d.next()).done);a=!0){var i=l.value;c({element:t[i],isExpanded:w.has(i),isSelected:m.has(i),isDisabled:g.has(i),isHalfSelected:E.has(i),treeState:p})}}catch(e){r=!0,n=e}finally{try{a||null==d.return||d.return()}finally{if(r)throw n}}}},[t,m,w,g,E,C,c,p]),a(function(){if(f&&o){var e=new Set(k);A&&e.add(A);var a=function(e,t,a,r){var n={every:new Set,some:new Set,none:new Set},l=!0,d=!1,i=void 0;try{for(var s,c=t[Symbol.iterator]();!(l=(s=c.next()).done);l=!0)for(var o=s.value;;){var u=y(e,o);if(0===u||r.has(u))break;var f=e[u].children.filter(function(e){return!r.has(e)});if(0===f.length)break;f.some(function(e){return a.has(e)})?f.every(function(e){return a.has(e)})?n.every.add(u):n.some.add(u):n.none.add(u),o=u}}catch(e){d=!0,i=e}finally{try{l||null==c.return||c.return()}finally{if(d)throw i}}return n}(t,e,m,g),r=a.every,n=a.some,l=a.none,d=!0,i=!1,s=void 0;try{for(var c,u=r[Symbol.iterator]();!(d=(c=u.next()).done);d=!0){var h=c.value;m.has(h)||b({type:F,id:h,multiSelect:o,keepFocus:!0,NotUserAction:!0,lastInteractedWith:A})}}catch(e){i=!0,s=e}finally{try{d||null==u.return||u.return()}finally{if(i)throw s}}var p=!0,I=!1,v=void 0;try{for(var S,w=n[Symbol.iterator]();!(p=(S=w.next()).done);p=!0){var x=S.value;E.has(x)||b({type:j,id:x,multiSelect:o,keepFocus:!0,NotUserAction:!0,lastInteractedWith:A})}}catch(e){I=!0,v=e}finally{try{p||null==w.return||w.return()}finally{if(I)throw v}}var W=!0,O=!1,C=void 0;try{for(var D,U=l[Symbol.iterator]();!(W=(D=U.next()).done);W=!0){var N=D.value;(m.has(N)||E.has(N))&&b({type:P,id:N,multiSelect:o,keepFocus:!0,NotUserAction:!0,lastInteractedWith:A})}}catch(e){O=!0,C=e}finally{try{W||null==U.return||U.return()}finally{if(O)throw C}}}},[t,o,f,m,w,g,E,W,O,k,A]),a(function(){var e;if(null!=A&&null!=x){var t=i.current[x];null!=(e=t)&&e.focus&&e.focus()}},[x,i,A]),[p,b]},Y={select:"SELECT",focus:"FOCUS",exclusiveSelect:"EXCLUSIVE_SELECT"},q=function(){},V=e.forwardRef(function(a,r){var l=a.data,d=a.nodeRenderer,s=a.onSelect,c=void 0===s?q:s,f=a.onExpand,h=void 0===f?q:f,p=a.className,I=void 0===p?"":p,v=a.multiSelect,S=void 0!==v&&v,y=a.propagateSelect,b=void 0!==y&&y,m=a.propagateSelectUpwards,w=void 0!==m&&m,g=a.propagateCollapse,x=void 0!==g&&g,E=a.expandOnKeyboardSelect,W=void 0!==E&&E,A=a.togglableSelect,O=void 0!==A&&A,D=a.defaultExpandedIds,U=void 0===D?[]:D,N=a.defaultSelectedIds,L=void 0===N?[]:N,j=a.defaultDisabledIds,F=void 0===j?[]:j,P=a.clickAction,R=void 0===P?Y.select:P,T=a.onBlur,K=o(a,["data","nodeRenderer","onSelect","onExpand","className","multiSelect","propagateSelect","propagateSelectUpwards","propagateCollapse","expandOnKeyboardSelect","togglableSelect","defaultExpandedIds","defaultSelectedIds","defaultDisabledIds","clickAction","onBlur"]),_=t({}),B=u(X({data:l,defaultExpandedIds:U,defaultSelectedIds:L,defaultDisabledIds:F,nodeRefs:_,onSelect:c,onExpand:h,multiSelect:S,propagateSelect:b,propagateSelectUpwards:w}),2),z=B[0],G=B[1];b=b&&S;var M=t();return null!=r&&(M=r),e.createElement("ul",i({className:n(C.root,I),role:"tree","aria-multiselectable":S,ref:M,onBlur:function(e){return k(e,M.current,function(){T&&T({treeState:z,dispatch:G}),G({type:H})})},onKeyDown:Q({data:l,tabbableId:z.tabbableId,expandedIds:z.expandedIds,selectedIds:z.selectedIds,disabledIds:z.disabledIds,halfSelectedIds:z.halfSelectedIds,dispatch:G,propagateCollapse:x,propagateSelect:b,multiSelect:S,expandOnKeyboardSelect:W,togglableSelect:O})},K),l[0].children.map(function(t,a){return e.createElement(J,{key:t,selectedIds:z.selectedIds,tabbableId:z.tabbableId,isFocused:z.isFocused,expandedIds:z.expandedIds,disabledIds:z.disabledIds,halfSelectedIds:z.halfSelectedIds,lastUserSelect:z.lastUserSelect,lastInteractedWith:z.lastInteractedWith,dispatch:G,data:l,element:l[t],nodeRefs:_,baseClassNames:C,nodeRenderer:d,setsize:l[0].children.length,posinset:a+1,level:1,propagateCollapse:x,propagateSelect:b,multiSelect:S,togglableSelect:O,clickAction:R,state:z})}))}),J=function t(a){var r=a.element,l=a.dispatch,i=a.data,s=a.selectedIds,c=a.tabbableId,o=a.isFocused,u=a.expandedIds,p=a.disabledIds,I=a.halfSelectedIds,v=a.lastUserSelect,y=a.nodeRefs,m=a.baseClassNames,w=a.nodeRenderer,g=a.setsize,x=a.posinset,W=a.level,k=a.propagateCollapse,C=a.propagateSelect,D=a.multiSelect,N=a.togglableSelect,L=a.clickAction,j=a.lastInteractedWith,P=a.state,H=function(e){if(!(e.ctrlKey||e.altKey||e.shiftKey))if(u.has(r.id)&&k){var t=[r.id].concat(f(b(i,r.id,new Set)));l({type:U,ids:t,lastInteractedWith:r.id})}else l({type:R,id:r.id,lastInteractedWith:r.id})},z=function(){l({type:B,id:r.id,lastInteractedWith:r.id})},G=function(e){if(e.shiftKey){var t=E({data:i,expandedIds:u,from:v,to:r.id,lastInteractedWith:r.id}).filter(function(e){return!p.has(e)});t=C?O(i,t,p):t,l({type:_,select:!0,multiSelect:D,ids:t,lastInteractedWith:r.id})}else e.ctrlKey||L===Y.select?(l({type:N?T:F,id:r.id,multiSelect:D,lastInteractedWith:r.id}),C&&!p.has(r.id)&&l({type:K,ids:O(i,[r.id],p),select:!N||!s.has(r.id),multiSelect:D,lastInteractedWith:r.id})):L===Y.exclusiveSelect?l({type:N?T:F,id:r.id,multiSelect:!1,lastInteractedWith:r.id}):L===Y.focus&&l({type:B,id:r.id,lastInteractedWith:r.id})},M=function(e){var t;return n(e,(d(t={},"".concat(e,"--expanded"),u.has(r.id)),d(t,"".concat(e,"--selected"),s.has(r.id)),d(t,"".concat(e,"--focused"),c===r.id&&o),t))};return S(i,r.id)?e.createElement("li",{role:"treeitem","aria-expanded":u.has(r.id),"aria-selected":A(s.has(r.id),p.has(r.id),D),"aria-setsize":g,"aria-posinset":x,"aria-level":W,disabled:p.has(r.id),tabIndex:c===r.id?0:-1,ref:function(e){return y.current[r.id]=e},className:m.branchWrapper},w({element:r,isBranch:!0,isSelected:s.has(r.id),isHalfSelected:I.has(r.id),isExpanded:u.has(r.id),isDisabled:p.has(r.id),dispatch:l,getNodeProps:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=e.onClick;return{onClick:null==t?h(G,H,z):h(t,z),className:n(M(m.node),m.branch)}},setsize:g,posinset:x,level:W,handleSelect:G,handleExpand:H,treeState:P}),u.has(r.id)&&e.createElement("ul",{role:"group",className:M(m.nodeGroup)},r.children.map(function(a,n){return e.createElement(t,{key:a,selectedIds:s,tabbableId:c,isFocused:o,expandedIds:u,halfSelectedIds:I,disabledIds:p,lastUserSelect:v,dispatch:l,data:i,element:i[a],nodeRefs:y,baseClassNames:m,nodeRenderer:w,setsize:r.children.length,posinset:n+1,level:W+1,propagateCollapse:k,propagateSelect:C,multiSelect:D,togglableSelect:N,clickAction:L,lastInteractedWith:j,state:P})}))):e.createElement("li",{role:"none",className:M(m.leafListItem)},w({element:r,isBranch:!1,isSelected:s.has(r.id),isHalfSelected:void 0,isExpanded:!1,isDisabled:p.has(r.id),dispatch:l,getNodeProps:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=e.onClick;return{role:"treeitem",tabIndex:c===r.id?0:-1,onClick:h(null==t?G:t,z),ref:function(e){return y.current[r.id]=e},className:n(M(m.node),m.leaf),"aria-setsize":g,"aria-posinset":x,"aria-level":W,"aria-selected":A(s.has(r.id),p.has(r.id),D),disabled:p.has(r.id)}},setsize:g,posinset:x,level:W,handleSelect:G,handleExpand:q,treeState:P}))},Q=function(e){var t=e.data,a=e.expandedIds,r=e.selectedIds,n=e.disabledIds,l=e.tabbableId,d=e.dispatch,i=e.propagateCollapse,s=e.propagateSelect,c=e.multiSelect,u=e.expandOnKeyboardSelect,h=e.togglableSelect;return function(e){var p=t[l],I=p.id;if(e.ctrlKey){if("a"===e.key){e.preventDefault();t[0];var v=o(t,["0"]),m=Object.values(v).map(function(e){return e.id}).filter(function(e){return!n.has(e)});d({type:K,multiSelect:c,select:Array.from(r).filter(function(e){return!n.has(e)}).length!==m.length,ids:m,lastInteractedWith:p.id})}else if(e.shiftKey&&("Home"===e.key||"End"===e.key)){var W="Home"===e.key?t[0].children[0]:w(t,I,a),A=E({data:t,expandedIds:a,from:I,to:W}).filter(function(e){return!n.has(e)});d({type:K,multiSelect:c,select:!0,ids:s?O(t,A,n):A}),d({type:B,id:W,lastInteractedWith:W})}}else{if(e.shiftKey)switch(e.key){case"ArrowUp":e.preventDefault();var k=g(t,I,a);return void(null==k&&n.has(k)||(d({type:K,ids:s?O(t,[k],n):[k],select:!0,multiSelect:c,lastInteractedWith:k}),d({type:B,id:k,lastInteractedWith:k})));case"ArrowDown":e.preventDefault();var C=x(t,I,a);return void(null==C&&n.has(C)||(d({type:K,ids:s?O(t,[C],n):[C],multiSelect:c,select:!0,lastInteractedWith:C}),d({type:B,id:C,lastInteractedWith:C})))}switch(e.key){case"ArrowDown":e.preventDefault();var j=x(t,I,a);return void(null!=j&&d({type:B,id:j,lastInteractedWith:j}));case"ArrowUp":e.preventDefault();var F=g(t,I,a);return void(null!=F&&d({type:B,id:F,lastInteractedWith:F}));case"ArrowLeft":if(e.preventDefault(),S(t,I)&&a.has(l))if(i){var P=[I].concat(f(b(t,I,new Set)));d({type:U,ids:P,lastInteractedWith:p.id})}else d({type:D,id:I,lastInteractedWith:I});else if(!t[0].children.includes(I)){var T=y(t,I);d({type:B,id:T,lastInteractedWith:T})}return;case"ArrowRight":return e.preventDefault(),void(S(t,I)&&(a.has(l)?d({type:B,id:p.children[0],lastInteractedWith:p.children[0]}):d({type:N,id:I,lastInteractedWith:I})));case"Home":e.preventDefault(),d({type:B,id:t[0].children[0],lastInteractedWith:t[0].children[0]});break;case"End":e.preventDefault();var _=w(t,t[0].id,a);return void d({type:B,id:_,lastInteractedWith:_});case"*":e.preventDefault();var H=t[y(t,I)].children.filter(function(e){return S(t,e)});return void d({type:L,ids:H,lastInteractedWith:I});case"Enter":case" ":case"Spacebar":return e.preventDefault(),!n.has(I)&&d({type:K,ids:s?O(t,[I],n):[I],select:!h||!r.has(I),multiSelect:c,lastInteractedWith:I}),void(u&&d({type:R,id:I,lastInteractedWith:I}));default:if(1===e.key.length)for(var z=x(t,I,a);z!==I;)if(null!=z){if(t[z].name[0].toLowerCase()===e.key.toLowerCase())return void d({type:B,id:z,lastInteractedWith:I});z=x(t,z,a)}else z=t[0].children[0];return}}}};V.propTypes={data:l.array.isRequired,onSelect:l.func,onExpand:l.func,className:l.string,nodeRenderer:l.func.isRequired,defaultExpandedIds:l.array,defaultSelectedIds:l.array,defaultDisabledIds:l.array,propagateCollapse:l.bool,propagateSelect:l.bool,propagateSelectUpwards:l.bool,multiSelect:l.bool,expandOnKeyboardSelect:l.bool,togglableSelect:l.bool,clickAction:l.oneOf(Object.values(Y))};export default V;export{W as flattenTree}; | ||
import e,{useRef as t,useEffect as a,useReducer as r}from"react";import n from"classnames";import l from"prop-types";function d(e,t,a){return t in e?Object.defineProperty(e,t,{value:a,enumerable:!0,configurable:!0,writable:!0}):e[t]=a,e}function i(){return(i=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var a=arguments[t];for(var r in a)Object.prototype.hasOwnProperty.call(a,r)&&(e[r]=a[r])}return e}).apply(this,arguments)}function s(e,t){var a=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),a.push.apply(a,r)}return a}function c(e){for(var t=1;t<arguments.length;t++){var a=null!=arguments[t]?arguments[t]:{};t%2?s(a,!0).forEach(function(t){d(e,t,a[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(a)):s(a).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(a,t))})}return e}function o(e,t){if(null==e)return{};var a,r,n=function(e,t){if(null==e)return{};var a,r,n={},l=Object.keys(e);for(r=0;r<l.length;r++)a=l[r],t.indexOf(a)>=0||(n[a]=e[a]);return n}(e,t);if(Object.getOwnPropertySymbols){var l=Object.getOwnPropertySymbols(e);for(r=0;r<l.length;r++)a=l[r],t.indexOf(a)>=0||Object.prototype.propertyIsEnumerable.call(e,a)&&(n[a]=e[a])}return n}function u(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var a=[],r=!0,n=!1,l=void 0;try{for(var d,i=e[Symbol.iterator]();!(r=(d=i.next()).done)&&(a.push(d.value),!t||a.length!==t);r=!0);}catch(e){n=!0,l=e}finally{try{r||null==i.return||i.return()}finally{if(n)throw l}}return a}(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}()}function f(e){return function(e){if(Array.isArray(e)){for(var t=0,a=new Array(e.length);t<e.length;t++)a[t]=e[t];return a}}(e)||function(e){if(Symbol.iterator in Object(e)||"[object Arguments]"===Object.prototype.toString.call(e))return Array.from(e)}(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance")}()}var h=function(){for(var e=arguments.length,t=new Array(e),a=0;a<e;a++)t[a]=arguments[a];return function(e){for(var a=0,r=t;a<r.length;a++){var n=r[a];if(n&&n(e),e.defaultPrevented)break}}},p=function(e,t){var a=new Set,r=!0,n=!1,l=void 0;try{for(var d,i=e[Symbol.iterator]();!(r=(d=i.next()).done);r=!0){var s=d.value;t.has(s)||a.add(s)}}catch(e){n=!0,l=e}finally{try{r||null==i.return||i.return()}finally{if(n)throw l}}return a},I=function(e,t){return new Set([].concat(f(p(e,t)),f(p(t,e))))},v=function(e){var r=t();return a(function(){r.current=e},[e]),r.current},S=function(e,t){return null!=e[t].children&&e[t].children.length>0},y=function(e,t){return e[t].parent},b=function(e,t,a){var r=[];return function e(t,n){var l=t[n];if(null!=l.children){var d=!0,i=!1,s=void 0;try{for(var c,o=l.children.filter(function(e){return!a.has(e)})[Symbol.iterator]();!(d=(c=o.next()).done);d=!0){var u=c.value;r.push(u),e(t,u)}}catch(e){i=!0,s=e}finally{try{d||null==o.return||o.return()}finally{if(i)throw s}}}}(e,t),r},m=function(e,t,a){var r=y(e,t);if(null!=r){var n=e[r],l=n.children.indexOf(t)+a;if(n.children[l])return n.children[l]}return null},w=function(e,t,a){var r=e[t];for(e[0].id===t&&(r=e[e[t].children[e[t].children.length-1]]);a.has(r.id)&&S(e,r.id);)r=e[r.children[r.children.length-1]];return r.id},g=function(e,t,a){if(t===e[0].children[0])return null;var r=m(e,t,-1);return null==r?y(e,t):w(e,r,a)},x=function(e,t,a){var r=e[t].id;if(S(e,r)&&a.has(r))return e[r].children[0];for(;;){var n=m(e,r,1);if(null!=n)return n;if(null==(r=y(e,r)))return null}},E=function(e){var t=e.data,a=e.expandedIds,r=e.from,n=e.to,l=[],d=Object.keys(t).length,i=0,s=r;if(l.push(r),r<n)for(;i<d&&(s=x(t,s,a),l.push(s),null!=s&&s!==n);)i+=1;else if(r>n)for(;i<d&&(s=g(t,s,a),l.push(s),null!=s&&s!==n);)i+=1;return l},W=function(e){var t=0,a=[];return function e(r,n){if(r.id=t,r.parent=n,a[t]=r,t+=1,null!=r.children&&0!==r.children.length){var l=!0,d=!1,i=void 0;try{for(var s,c=r.children[Symbol.iterator]();!(l=(s=c.next()).done);l=!0){e(s.value,r.id)}}catch(e){d=!0,i=e}finally{try{l||null==c.return||c.return()}finally{if(d)throw i}}r.children=r.children.map(function(e){return e.id})}}(e,null),a},A=function(e,t,a){if(!t)return a?e:!!e||void 0},O=function(e,t,a){return t.concat.apply(t,f(t.filter(function(t){return S(e,t)}).map(function(t){return b(e,t,a)})))},k=function(e,t,a){window.navigator.userAgent.match(/Trident/)?setTimeout(function(){return!t.contains(document.activeElement)&&a()},0):!t.contains(e.nativeEvent.relatedTarget)&&a()},C={root:"tree",node:"tree-node",branch:"tree-node__branch",branchWrapper:"tree-branch-wrapper",leafListItem:"tree-leaf-list-item",leaf:"tree-node__leaf",nodeGroup:"tree-node-group"},D="COLLAPSE",U="COLLAPSE_MANY",N="EXPAND",L="EXPAND_MANY",j="HALF_SELECT",F="SELECT",P="DESELECT",R="TOGGLE",T="TOGGLE_SELECT",K="SELECT_MANY",_="EXCLUSIVE_CHANGE_SELECT_MANY",B="FOCUS",H="BLUR",z="DISABLE",G="ENABLE",M=function(e,t){switch(t.type){case D:var a=new Set(e.expandedIds);return a.delete(t.id),c({},e,{expandedIds:a,tabbableId:t.id,isFocused:!0,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case U:var r=new Set(e.expandedIds),n=!0,l=!1,d=void 0;try{for(var i,s=t.ids[Symbol.iterator]();!(n=(i=s.next()).done);n=!0){var o=i.value;r.delete(o)}}catch(e){l=!0,d=e}finally{try{n||null==s.return||s.return()}finally{if(l)throw d}}return c({},e,{expandedIds:r,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case N:var u=new Set(e.expandedIds);return u.add(t.id),c({},e,{expandedIds:u,tabbableId:t.id,isFocused:!0,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case L:var h=new Set([].concat(f(e.expandedIds),f(t.ids)));return c({},e,{expandedIds:h,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case R:var I=new Set(e.expandedIds);return e.expandedIds.has(t.id)?I.delete(t.id):I.add(t.id),c({},e,{expandedIds:I,tabbableId:t.id,isFocused:!0,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case j:if(e.disabledIds.has(t.id))return e;var v=new Set(e.halfSelectedIds),S=new Set(e.selectedIds);return v.add(t.id),S.delete(t.id),c({},e,{selectedIds:S,halfSelectedIds:v,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case F:if(e.disabledIds.has(t.id))return e;var y;t.multiSelect?(y=new Set(e.selectedIds)).add(t.id):(y=new Set).add(t.id);var b=new Set(e.halfSelectedIds);return b.delete(t.id),c({},e,{selectedIds:y,halfSelectedIds:b,tabbableId:t.keepFocus?e.tabbableId:t.id,isFocused:!0,lastUserSelect:t.NotUserAction?e.lastUserSelect:t.id,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case P:if(e.disabledIds.has(t.id))return e;var m;t.multiSelect?(m=new Set(e.selectedIds)).delete(t.id):m=new Set;var w=new Set(e.halfSelectedIds);return w.delete(t.id),c({},e,{selectedIds:m,halfSelectedIds:w,tabbableId:t.keepFocus?e.tabbableId:t.id,isFocused:!0,lastUserSelect:t.NotUserAction?e.lastUserSelect:t.id,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case T:if(e.disabledIds.has(t.id))return e;var g,x=e.selectedIds.has(t.id);t.multiSelect?(g=new Set(e.selectedIds),x?g.delete(t.id):g.add(t.id)):(g=new Set,x||g.add(t.id));var E=new Set(e.halfSelectedIds);return E.delete(t.id),c({},e,{selectedIds:g,halfSelectedIds:E,tabbableId:t.id,isFocused:!0,lastUserSelect:t.NotUserAction?e.lastUserSelect:t.id,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case K:var W,A=t.ids.filter(function(t){return!e.disabledIds.has(t)});if(t.multiSelect){W=t.select?new Set([].concat(f(e.selectedIds),f(A))):p(e.selectedIds,new Set(A));var O=p(e.halfSelectedIds,W);return c({},e,{selectedIds:W,halfSelectedIds:O,lastAction:t.type,lastInteractedWith:t.lastInteractedWith})}return e;case _:var k,C=t.ids.filter(function(t){return!e.disabledIds.has(t)});if(t.multiSelect){k=t.select?new Set(C):p(e.selectedIds,new Set(C));var M=p(e.halfSelectedIds,k);return c({},e,{selectedIds:k,halfSelectedIds:M,lastAction:t.type,lastInteractedWith:t.lastInteractedWith})}return e;case B:return c({},e,{tabbableId:t.id,isFocused:!0,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case H:return c({},e,{isFocused:!1});case z:var X=new Set(e.disabledIds);return X.add(t.id),c({},e,{disabledIds:X});case G:var Y=new Set(e.disabledIds);return Y.delete(t.id),c({},e,{disabledIds:Y});default:throw new Error("Invalid action passed to the reducer")}},X=function(e){var t=e.data,n=e.defaultExpandedIds,l=e.defaultSelectedIds,d=e.defaultDisabledIds,i=e.nodeRefs,s=e.onSelect,c=e.onExpand,o=e.multiSelect,f=e.propagateSelectUpwards,h=u(r(M,{selectedIds:new Set(l),tabbableId:t[0].children[0],isFocused:!1,expandedIds:new Set(n),halfSelectedIds:new Set,lastUserSelect:t[0].children[0],lastInteractedWith:null,disabledIds:new Set(d)}),2),p=h[0],b=h[1],m=p.selectedIds,w=p.expandedIds,g=p.disabledIds,x=p.tabbableId,E=p.halfSelectedIds,W=p.lastAction,A=p.lastInteractedWith,O=v(m)||new Set,k=I(m,O);a(function(){if(s!==q){var e=!0,a=!1,r=void 0;try{for(var n,l=k[Symbol.iterator]();!(e=(n=l.next()).done);e=!0){var d=n.value,i=S(t,d);s({element:t[d],isBranch:i,isExpanded:i?w.has(d):void 0,isSelected:m.has(d),isDisabled:g.has(d),isHalfSelected:i?E.has(d):void 0,treeState:p})}}catch(e){a=!0,r=e}finally{try{e||null==l.return||l.return()}finally{if(a)throw r}}}},[t,m,w,g,E,k,s,p]);var C=v(w)||new Set;return a(function(){var e=I(w,C);if(c!==q){var a=!0,r=!1,n=void 0;try{for(var l,d=e[Symbol.iterator]();!(a=(l=d.next()).done);a=!0){var i=l.value;c({element:t[i],isExpanded:w.has(i),isSelected:m.has(i),isDisabled:g.has(i),isHalfSelected:E.has(i),treeState:p})}}catch(e){r=!0,n=e}finally{try{a||null==d.return||d.return()}finally{if(r)throw n}}}},[t,m,w,g,E,C,c,p]),a(function(){if(f&&o){var e=new Set(k);A&&e.add(A);var a=function(e,t,a,r){var n={every:new Set,some:new Set,none:new Set},l=!0,d=!1,i=void 0;try{for(var s,c=t[Symbol.iterator]();!(l=(s=c.next()).done);l=!0)for(var o=s.value;;){var u=y(e,o);if(0===u||r.has(u))break;var f=e[u].children.filter(function(e){return!r.has(e)});if(0===f.length)break;f.some(function(e){return a.has(e)})?f.every(function(e){return a.has(e)})?n.every.add(u):n.some.add(u):n.none.add(u),o=u}}catch(e){d=!0,i=e}finally{try{l||null==c.return||c.return()}finally{if(d)throw i}}return n}(t,e,m,g),r=a.every,n=a.some,l=a.none,d=!0,i=!1,s=void 0;try{for(var c,u=r[Symbol.iterator]();!(d=(c=u.next()).done);d=!0){var h=c.value;m.has(h)||b({type:F,id:h,multiSelect:o,keepFocus:!0,NotUserAction:!0,lastInteractedWith:A})}}catch(e){i=!0,s=e}finally{try{d||null==u.return||u.return()}finally{if(i)throw s}}var p=!0,I=!1,v=void 0;try{for(var S,w=n[Symbol.iterator]();!(p=(S=w.next()).done);p=!0){var x=S.value;E.has(x)||b({type:j,id:x,multiSelect:o,keepFocus:!0,NotUserAction:!0,lastInteractedWith:A})}}catch(e){I=!0,v=e}finally{try{p||null==w.return||w.return()}finally{if(I)throw v}}var W=!0,O=!1,C=void 0;try{for(var D,U=l[Symbol.iterator]();!(W=(D=U.next()).done);W=!0){var N=D.value;(m.has(N)||E.has(N))&&b({type:P,id:N,multiSelect:o,keepFocus:!0,NotUserAction:!0,lastInteractedWith:A})}}catch(e){O=!0,C=e}finally{try{W||null==U.return||U.return()}finally{if(O)throw C}}}},[t,o,f,m,w,g,E,W,O,k,A]),a(function(){var e;if(null!=A&&null!=x){var t=i.current[x];null!=(e=t)&&e.focus&&e.focus()}},[x,i,A]),[p,b]},Y={select:"SELECT",focus:"FOCUS",exclusiveSelect:"EXCLUSIVE_SELECT"},q=function(){},V=e.forwardRef(function(a,r){var l=a.data,d=a.nodeRenderer,s=a.onSelect,c=void 0===s?q:s,f=a.onExpand,h=void 0===f?q:f,p=a.className,I=void 0===p?"":p,v=a.multiSelect,S=void 0!==v&&v,y=a.propagateSelect,b=void 0!==y&&y,m=a.propagateSelectUpwards,w=void 0!==m&&m,g=a.propagateCollapse,x=void 0!==g&&g,E=a.expandOnKeyboardSelect,W=void 0!==E&&E,A=a.togglableSelect,O=void 0!==A&&A,D=a.defaultExpandedIds,U=void 0===D?[]:D,N=a.defaultSelectedIds,L=void 0===N?[]:N,j=a.defaultDisabledIds,F=void 0===j?[]:j,P=a.clickAction,R=void 0===P?Y.select:P,T=a.onBlur,K=o(a,["data","nodeRenderer","onSelect","onExpand","className","multiSelect","propagateSelect","propagateSelectUpwards","propagateCollapse","expandOnKeyboardSelect","togglableSelect","defaultExpandedIds","defaultSelectedIds","defaultDisabledIds","clickAction","onBlur"]),_=t({}),B=u(X({data:l,defaultExpandedIds:U,defaultSelectedIds:L,defaultDisabledIds:F,nodeRefs:_,onSelect:c,onExpand:h,multiSelect:S,propagateSelect:b,propagateSelectUpwards:w}),2),z=B[0],G=B[1];b=b&&S;var M=t();return null!=r&&(M=r),e.createElement("ul",i({className:n(C.root,I),role:"tree","aria-multiselectable":S,ref:M,onBlur:function(e){return k(e,M.current,function(){T&&T({treeState:z,dispatch:G}),G({type:H})})},onKeyDown:Q({data:l,tabbableId:z.tabbableId,expandedIds:z.expandedIds,selectedIds:z.selectedIds,disabledIds:z.disabledIds,halfSelectedIds:z.halfSelectedIds,dispatch:G,propagateCollapse:x,propagateSelect:b,multiSelect:S,expandOnKeyboardSelect:W,togglableSelect:O})},K),l[0].children.map(function(t,a){return e.createElement(J,{key:t,selectedIds:z.selectedIds,tabbableId:z.tabbableId,isFocused:z.isFocused,expandedIds:z.expandedIds,disabledIds:z.disabledIds,halfSelectedIds:z.halfSelectedIds,lastUserSelect:z.lastUserSelect,lastInteractedWith:z.lastInteractedWith,dispatch:G,data:l,element:l[t],nodeRefs:_,baseClassNames:C,nodeRenderer:d,setsize:l[0].children.length,posinset:a+1,level:1,propagateCollapse:x,propagateSelect:b,multiSelect:S,togglableSelect:O,clickAction:R,state:z})}))}),J=function t(a){var r=a.element,l=a.dispatch,i=a.data,s=a.selectedIds,c=a.tabbableId,o=a.isFocused,u=a.expandedIds,p=a.disabledIds,I=a.halfSelectedIds,v=a.lastUserSelect,y=a.nodeRefs,m=a.baseClassNames,w=a.nodeRenderer,g=a.setsize,x=a.posinset,W=a.level,k=a.propagateCollapse,C=a.propagateSelect,D=a.multiSelect,N=a.togglableSelect,L=a.clickAction,j=a.lastInteractedWith,P=a.state,H=function(e){if(!(e.ctrlKey||e.altKey||e.shiftKey))if(u.has(r.id)&&k){var t=[r.id].concat(f(b(i,r.id,new Set)));l({type:U,ids:t,lastInteractedWith:r.id})}else l({type:R,id:r.id,lastInteractedWith:r.id})},z=function(){l({type:B,id:r.id,lastInteractedWith:r.id})},G=function(e){if(e.shiftKey){var t=E({data:i,expandedIds:u,from:v,to:r.id,lastInteractedWith:r.id}).filter(function(e){return!p.has(e)});t=C?O(i,t,p):t,l({type:_,select:!0,multiSelect:D,ids:t,lastInteractedWith:r.id})}else e.ctrlKey||L===Y.select?(l({type:N?T:F,id:r.id,multiSelect:D,lastInteractedWith:r.id}),C&&!p.has(r.id)&&l({type:K,ids:O(i,[r.id],p),select:!N||!s.has(r.id),multiSelect:D,lastInteractedWith:r.id})):L===Y.exclusiveSelect?l({type:N?T:F,id:r.id,multiSelect:!1,lastInteractedWith:r.id}):L===Y.focus&&l({type:B,id:r.id,lastInteractedWith:r.id})},M=function(e){var t;return n(e,(d(t={},"".concat(e,"--expanded"),u.has(r.id)),d(t,"".concat(e,"--selected"),s.has(r.id)),d(t,"".concat(e,"--focused"),c===r.id&&o),t))};return S(i,r.id)?e.createElement("li",{role:"treeitem","aria-expanded":u.has(r.id),"aria-selected":A(s.has(r.id),p.has(r.id),D),"aria-setsize":g,"aria-posinset":x,"aria-level":W,disabled:p.has(r.id),tabIndex:c===r.id?0:-1,ref:function(e){return y.current[r.id]=e},className:m.branchWrapper},w({element:r,isBranch:!0,isSelected:s.has(r.id),isHalfSelected:I.has(r.id),isExpanded:u.has(r.id),isDisabled:p.has(r.id),dispatch:l,getNodeProps:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=e.onClick;return{onClick:null==t?h(G,H,z):h(t,z),className:n(M(m.node),m.branch)}},setsize:g,posinset:x,level:W,handleSelect:G,handleExpand:H,treeState:P}),u.has(r.id)&&e.createElement("ul",{role:"group",className:M(m.nodeGroup)},r.children.map(function(a,n){return e.createElement(t,{key:a,selectedIds:s,tabbableId:c,isFocused:o,expandedIds:u,halfSelectedIds:I,disabledIds:p,lastUserSelect:v,dispatch:l,data:i,element:i[a],nodeRefs:y,baseClassNames:m,nodeRenderer:w,setsize:r.children.length,posinset:n+1,level:W+1,propagateCollapse:k,propagateSelect:C,multiSelect:D,togglableSelect:N,clickAction:L,lastInteractedWith:j,state:P})}))):e.createElement("li",{role:"none",className:M(m.leafListItem)},w({element:r,isBranch:!1,isSelected:s.has(r.id),isHalfSelected:void 0,isExpanded:!1,isDisabled:p.has(r.id),dispatch:l,getNodeProps:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=e.onClick;return{role:"treeitem",tabIndex:c===r.id?0:-1,onClick:h(null==t?G:t,z),ref:function(e){return y.current[r.id]=e},className:n(M(m.node),m.leaf),"aria-setsize":g,"aria-posinset":x,"aria-level":W,"aria-selected":A(s.has(r.id),p.has(r.id),D),disabled:p.has(r.id)}},setsize:g,posinset:x,level:W,handleSelect:G,handleExpand:q,treeState:P}))},Q=function(e){var t=e.data,a=e.expandedIds,r=e.selectedIds,n=e.disabledIds,l=e.tabbableId,d=e.dispatch,i=e.propagateCollapse,s=e.propagateSelect,c=e.multiSelect,u=e.expandOnKeyboardSelect,h=e.togglableSelect;return function(e){var p=t[l],I=p.id;if(e.ctrlKey){if("a"===e.key){e.preventDefault();t[0];var v=o(t,["0"]),m=Object.values(v).map(function(e){return e.id}).filter(function(e){return!n.has(e)});d({type:K,multiSelect:c,select:Array.from(r).filter(function(e){return!n.has(e)}).length!==m.length,ids:m,lastInteractedWith:p.id})}else if(e.shiftKey&&("Home"===e.key||"End"===e.key)){var W="Home"===e.key?t[0].children[0]:w(t,I,a),A=E({data:t,expandedIds:a,from:I,to:W}).filter(function(e){return!n.has(e)});d({type:K,multiSelect:c,select:!0,ids:s?O(t,A,n):A}),d({type:B,id:W,lastInteractedWith:W})}}else{if(e.shiftKey)switch(e.key){case"ArrowUp":e.preventDefault();var k=g(t,I,a);return void(null==k&&n.has(k)||(d({type:K,ids:s?O(t,[k],n):[k],select:!0,multiSelect:c,lastInteractedWith:k}),d({type:B,id:k,lastInteractedWith:k})));case"ArrowDown":e.preventDefault();var C=x(t,I,a);return void(null==C&&n.has(C)||(d({type:K,ids:s?O(t,[C],n):[C],multiSelect:c,select:!0,lastInteractedWith:C}),d({type:B,id:C,lastInteractedWith:C})))}switch(e.key){case"ArrowDown":e.preventDefault();var j=x(t,I,a);return void(null!=j&&d({type:B,id:j,lastInteractedWith:j}));case"ArrowUp":e.preventDefault();var F=g(t,I,a);return void(null!=F&&d({type:B,id:F,lastInteractedWith:F}));case"ArrowLeft":if(e.preventDefault(),S(t,I)&&a.has(l))if(i){var P=[I].concat(f(b(t,I,new Set)));d({type:U,ids:P,lastInteractedWith:p.id})}else d({type:D,id:I,lastInteractedWith:I});else if(!t[0].children.includes(I)){var _=y(t,I);d({type:B,id:_,lastInteractedWith:_})}return;case"ArrowRight":return e.preventDefault(),void(S(t,I)&&(a.has(l)?d({type:B,id:p.children[0],lastInteractedWith:p.children[0]}):d({type:N,id:I,lastInteractedWith:I})));case"Home":e.preventDefault(),d({type:B,id:t[0].children[0],lastInteractedWith:t[0].children[0]});break;case"End":e.preventDefault();var H=w(t,t[0].id,a);return void d({type:B,id:H,lastInteractedWith:H});case"*":e.preventDefault();var z=t[y(t,I)].children.filter(function(e){return S(t,e)});return void d({type:L,ids:z,lastInteractedWith:I});case"Enter":case" ":case"Spacebar":return e.preventDefault(),!n.has(I)&&d({type:T,id:p.id,select:!h||!r.has(I),multiSelect:c,lastInteractedWith:I}),void(u&&d({type:R,id:I,lastInteractedWith:I}));default:if(1===e.key.length)for(var G=x(t,I,a);G!==I;)if(null!=G){if(t[G].name[0].toLowerCase()===e.key.toLowerCase())return void d({type:B,id:G,lastInteractedWith:I});G=x(t,G,a)}else G=t[0].children[0];return}}}};V.propTypes={data:l.array.isRequired,onSelect:l.func,onExpand:l.func,className:l.string,nodeRenderer:l.func.isRequired,defaultExpandedIds:l.array,defaultSelectedIds:l.array,defaultDisabledIds:l.array,propagateCollapse:l.bool,propagateSelect:l.bool,propagateSelectUpwards:l.bool,multiSelect:l.bool,expandOnKeyboardSelect:l.bool,togglableSelect:l.bool,clickAction:l.oneOf(Object.values(Y))};export default V;export{W as flattenTree}; |
{ | ||
"name": "react-accessible-treeview", | ||
"description": "A react component that implements the treeview pattern as described by the WAI-ARIA Authoring Practices.", | ||
"version": "2.1.0", | ||
"version": "2.1.1", | ||
"author": "lissitz (https://github.com/lissitz)", | ||
@@ -22,2 +22,3 @@ "main": "dist/react-accessible-treeview.cjs.js", | ||
"@storybook/react": "^5.1.11", | ||
"@testing-library/react": "^9.1.3", | ||
"babel-loader": "^8.0.6", | ||
@@ -32,2 +33,3 @@ "classnames": "^2.2.6", | ||
"eslint-plugin-react-hooks": "^1.5.0", | ||
"jest": "^24.9.0", | ||
"lint-staged": "^9.2.2", | ||
@@ -34,0 +36,0 @@ "prettier": "^1.18.2", |
@@ -987,4 +987,4 @@ import React, { useReducer, useRef, useEffect } from "react"; | ||
dispatch({ | ||
type: treeTypes.changeSelectMany, | ||
ids: propagateSelect ? propagatedIds(data, [id], disabledIds) : [id], | ||
type: treeTypes.toggleSelect, | ||
id: element.id, | ||
select: togglableSelect ? !selectedIds.has(id) : true, | ||
@@ -991,0 +991,0 @@ multiSelect, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
113886
28
1386