react-accessible-treeview
Advanced tools
Comparing version 2.1.3 to 2.1.4
@@ -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){var t=e.isSelected,a=e.isDisabled,r=e.multiSelect;if(!a)return r?t:!!t||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,w=t.ids.filter(function(t){return!e.disabledIds.has(t)});if(t.multiSelect){T=t.select?new Set([].concat(_toConsumableArray(e.selectedIds),_toConsumableArray(w))):difference(e.selectedIds,new Set(w));var A=difference(e.halfSelectedIds,T);return _objectSpread2({},e,{selectedIds:T,halfSelectedIds:A,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 w,A=n[Symbol.iterator]();!(I=(w=A.next()).done);I=!0){var x=w.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==A.return||A.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,w=void 0===T?[]:T,A=e.defaultSelectedIds,x=void 0===A?[]:A,_=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({}),D=_slicedToArray(useTree({data:a,defaultExpandedIds:w,defaultSelectedIds:x,defaultDisabledIds:E,nodeRefs:R,onSelect:l,onExpand:d,multiSelect:u,propagateSelect:f,propagateSelectUpwards:y}),2),O=D[0],j=D[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){onComponentBlur(e,k.current,function(){C&&C({treeState:O,dispatch:j}),j({type:treeTypes.blur})})},onKeyDown:handleKeyDown({data:a,tabbableId:O.tabbableId,expandedIds:O.expandedIds,selectedIds:O.selectedIds,disabledIds:O.disabledIds,halfSelectedIds:O.halfSelectedIds,dispatch:j,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:O.selectedIds,tabbableId:O.tabbableId,isFocused:O.isFocused,expandedIds:O.expandedIds,disabledIds:O.disabledIds,halfSelectedIds:O.halfSelectedIds,lastUserSelect:O.lastUserSelect,lastInteractedWith:O.lastInteractedWith,dispatch:j,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,propagateSelectUpwards:y,multiSelect:u,togglableSelect:m,clickAction:P,state:O})}))}),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.propagateSelectUpwards,t.multiSelect),m=t.togglableSelect,T=t.clickAction,w=t.lastInteractedWith,A=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({isSelected:l.has(a.id),isDisabled:c.has(a.id),multiSelect: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:A}),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,propagateSelectUpwards:v,multiSelect:g,togglableSelect:m,clickAction:T,lastInteractedWith:w,state:A})}))):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({isSelected:l.has(a.id),isDisabled:c.has(a.id),multiSelect:g}),disabled:c.has(a.id)}},setsize:y,posinset:b,level:S,handleSelect:E,handleExpand:noop,treeState:A}))},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 w=getParent(t,f);s({type:treeTypes.focus,id:w,lastInteractedWith:w})}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 A=getLastAccessible(t,t[0].id,a);return void s({type:treeTypes.focus,id:A,lastInteractedWith:A});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:u?treeTypes.toggleSelect:treeTypes.select,id:p.id,multiSelect:c,lastInteractedWith:p.id}),i&&!n.has(p.id)&&s({type:treeTypes.changeSelectMany,ids:propagatedIds(t,[p.id],n),select:!u||!r.has(p.id),multiSelect:c,lastInteractedWith:p.id}),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 e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var t=require("react"),a=e(t),r=e(require("classnames")),n=e(require("prop-types"));function d(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 l(e){for(var t=1;t<arguments.length;t++){var a=null!=arguments[t]?arguments[t]:{};t%2?d(Object(a),!0).forEach((function(t){i(e,t,a[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(a)):d(Object(a)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(a,t))}))}return e}function i(e,t,a){return t in e?Object.defineProperty(e,t,{value:a,enumerable:!0,configurable:!0,writable:!0}):e[t]=a,e}function s(){return(s=Object.assign?Object.assign.bind():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 c(e,t){if(null==e)return{};var a,r,n=function(e,t){if(null==e)return{};var a,r,n={},d=Object.keys(e);for(r=0;r<d.length;r++)a=d[r],t.indexOf(a)>=0||(n[a]=e[a]);return n}(e,t);if(Object.getOwnPropertySymbols){var d=Object.getOwnPropertySymbols(e);for(r=0;r<d.length;r++)a=d[r],t.indexOf(a)>=0||Object.prototype.propertyIsEnumerable.call(e,a)&&(n[a]=e[a])}return n}function o(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var a=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null==a)return;var r,n,d=[],l=!0,i=!1;try{for(a=a.call(e);!(l=(r=a.next()).done)&&(d.push(r.value),!t||d.length!==t);l=!0);}catch(e){i=!0,n=e}finally{try{l||null==a.return||a.return()}finally{if(i)throw n}}return d}(e,t)||f(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function u(e){return function(e){if(Array.isArray(e))return p(e)}(e)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(e)||f(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function f(e,t){if(e){if("string"==typeof e)return p(e,t);var a=Object.prototype.toString.call(e).slice(8,-1);return"Object"===a&&e.constructor&&(a=e.constructor.name),"Map"===a||"Set"===a?Array.from(e):"Arguments"===a||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(a)?p(e,t):void 0}}function p(e,t){(null==t||t>e.length)&&(t=e.length);for(var a=0,r=new Array(t);a<t;a++)r[a]=e[a];return r}function h(e,t){var a="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(!a){if(Array.isArray(e)||(a=f(e))||t&&e&&"number"==typeof e.length){a&&(e=a);var r=0,n=function(){};return{s:n,n:function(){return r>=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},f:n}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var d,l=!0,i=!1;return{s:function(){a=a.call(e)},n:function(){var e=a.next();return l=e.done,e},e:function(e){i=!0,d=e},f:function(){try{l||null==a.return||a.return()}finally{if(i)throw d}}}}var v=function(){for(var e=arguments.length,t=new Array(e),a=0;a<e;a++)t[a]=arguments[a];return function(e){var a,r=h(t);try{for(r.s();!(a=r.n()).done;){var n=a.value;if(n&&n(e),e.defaultPrevented)break}}catch(e){r.e(e)}finally{r.f()}}},I=function(e,t){var a,r=new Set,n=h(e);try{for(n.s();!(a=n.n()).done;){var d=a.value;t.has(d)||r.add(d)}}catch(e){n.e(e)}finally{n.f()}return r},S=function(e,t){return new Set([].concat(u(I(e,t)),u(I(t,e))))},b=function(e){var a=t.useRef();return t.useEffect((function(){a.current=e}),[e]),a.current},y=function(e,t){return null!=e[t].children&&e[t].children.length>0},m=function(e,t){return e[t].parent},g=function(e,t,a){var r=[];return function e(t,n){var d=t[n];if(null!=d.children){var l,i=h(d.children.filter((function(e){return!a.has(e)})));try{for(i.s();!(l=i.n()).done;){var s=l.value;r.push(s),e(t,s)}}catch(e){i.e(e)}finally{i.f()}}}(e,t),r},w=function(e,t,a){var r=m(e,t);if(null!=r){var n=e[r],d=n.children.indexOf(t)+a;if(n.children[d])return n.children[d]}return null},E=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)&&y(e,r.id);)r=e[r.children[r.children.length-1]];return r.id},W=function(e,t,a){if(t===e[0].children[0])return null;var r=w(e,t,-1);return null==r?m(e,t):E(e,r,a)},x=function(e,t,a){var r=e[t].id;if(y(e,r)&&a.has(r))return e[r].children[0];for(;;){var n=w(e,r,1);if(null!=n)return n;if(null==(r=m(e,r)))return null}},A=function(e){var t=e.data,a=e.expandedIds,r=e.from,n=e.to,d=[],l=Object.keys(t).length,i=0,s=r;if(d.push(r),r<n)for(;i<l&&(s=x(t,s,a),d.push(s),null!=s&&s!==n);)i+=1;else if(r>n)for(;i<l&&(s=W(t,s,a),d.push(s),null!=s&&s!==n);)i+=1;return d},O=function(e){var t=e.isSelected,a=e.isDisabled,r=e.multiSelect;if(!a)return r?t:!!t||void 0},C=function(e,t,a){return t.concat.apply(t,u(t.filter((function(t){return y(e,t)})).map((function(t){return g(e,t,a)}))))},D=function(e,t,a){window.navigator.userAgent.match(/Trident/)?setTimeout((function(){return!t.contains(document.activeElement)&&a()}),0):!t.contains(e.nativeEvent.relatedTarget)&&a()},k=["data","nodeRenderer","onSelect","onExpand","className","multiSelect","propagateSelect","propagateSelectUpwards","propagateCollapse","expandOnKeyboardSelect","togglableSelect","defaultExpandedIds","defaultSelectedIds","defaultDisabledIds","clickAction","onBlur"],N=["data","element","expandedIds","getClasses","baseClassNames","level"],j=["0"],U={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"},L="COLLAPSE",P="COLLAPSE_MANY",F="EXPAND",R="EXPAND_MANY",T="HALF_SELECT",_="SELECT",K="DESELECT",B="TOGGLE",H="TOGGLE_SELECT",z="SELECT_MANY",G="EXCLUSIVE_CHANGE_SELECT_MANY",M="FOCUS",q="BLUR",X="DISABLE",Y="ENABLE",V=function(e,t){switch(t.type){case L:var a=new Set(e.expandedIds);return a.delete(t.id),l(l({},e),{},{expandedIds:a,tabbableId:t.id,isFocused:!0,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case P:var r,n=new Set(e.expandedIds),d=h(t.ids);try{for(d.s();!(r=d.n()).done;){var i=r.value;n.delete(i)}}catch(e){d.e(e)}finally{d.f()}return l(l({},e),{},{expandedIds:n,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case F:var s=new Set(e.expandedIds);return s.add(t.id),l(l({},e),{},{expandedIds:s,tabbableId:t.id,isFocused:!0,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case R:var c=new Set([].concat(u(e.expandedIds),u(t.ids)));return l(l({},e),{},{expandedIds:c,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case B:var o=new Set(e.expandedIds);return e.expandedIds.has(t.id)?o.delete(t.id):o.add(t.id),l(l({},e),{},{expandedIds:o,tabbableId:t.id,isFocused:!0,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case T:if(e.disabledIds.has(t.id))return e;var f=new Set(e.halfSelectedIds),p=new Set(e.selectedIds);return f.add(t.id),p.delete(t.id),l(l({},e),{},{selectedIds:p,halfSelectedIds:f,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case _:if(e.disabledIds.has(t.id))return e;var v;t.multiSelect?(v=new Set(e.selectedIds)).add(t.id):(v=new Set).add(t.id);var S=new Set(e.halfSelectedIds);return S.delete(t.id),l(l({},e),{},{selectedIds:v,halfSelectedIds:S,tabbableId:t.keepFocus?e.tabbableId:t.id,isFocused:!0,lastUserSelect:t.NotUserAction?e.lastUserSelect:t.id,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case K:if(e.disabledIds.has(t.id))return e;var b;t.multiSelect?(b=new Set(e.selectedIds)).delete(t.id):b=new Set;var y=new Set(e.halfSelectedIds);return y.delete(t.id),l(l({},e),{},{selectedIds:b,halfSelectedIds:y,tabbableId:t.keepFocus?e.tabbableId:t.id,isFocused:!0,lastUserSelect:t.NotUserAction?e.lastUserSelect:t.id,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case H:if(e.disabledIds.has(t.id))return e;var m,g=e.selectedIds.has(t.id);t.multiSelect?(m=new Set(e.selectedIds),g?m.delete(t.id):m.add(t.id)):(m=new Set,g||m.add(t.id));var w=new Set(e.halfSelectedIds);return w.delete(t.id),l(l({},e),{},{selectedIds:m,halfSelectedIds:w,tabbableId:t.id,isFocused:!0,lastUserSelect:t.NotUserAction?e.lastUserSelect:t.id,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case z:var E,W=t.ids.filter((function(t){return!e.disabledIds.has(t)}));if(t.multiSelect){E=t.select?new Set([].concat(u(e.selectedIds),u(W))):I(e.selectedIds,new Set(W));var x=I(e.halfSelectedIds,E);return l(l({},e),{},{selectedIds:E,halfSelectedIds:x,lastAction:t.type,lastInteractedWith:t.lastInteractedWith})}return e;case G:var A,O=t.ids.filter((function(t){return!e.disabledIds.has(t)}));if(t.multiSelect){A=t.select?new Set(O):I(e.selectedIds,new Set(O));var C=I(e.halfSelectedIds,A);return l(l({},e),{},{selectedIds:A,halfSelectedIds:C,lastAction:t.type,lastInteractedWith:t.lastInteractedWith})}return e;case M:return l(l({},e),{},{tabbableId:t.id,isFocused:!0,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case q:return l(l({},e),{},{isFocused:!1});case X:var D=new Set(e.disabledIds);return D.add(t.id),l(l({},e),{},{disabledIds:D});case Y:var k=new Set(e.disabledIds);return k.delete(t.id),l(l({},e),{},{disabledIds:k});default:throw new Error("Invalid action passed to the reducer")}},$=function(e){var a=e.data,r=e.defaultExpandedIds,n=e.defaultSelectedIds,d=e.defaultDisabledIds,l=e.nodeRefs,i=e.onSelect,s=e.onExpand,c=e.multiSelect,u=e.propagateSelectUpwards,f=o(t.useReducer(V,{selectedIds:new Set(n),tabbableId:a[0].children[0],isFocused:!1,expandedIds:new Set(r),halfSelectedIds:new Set,lastUserSelect:a[0].children[0],lastInteractedWith:null,disabledIds:new Set(d)}),2),p=f[0],v=f[1],I=p.selectedIds,g=p.expandedIds,w=p.disabledIds,E=p.tabbableId,W=p.halfSelectedIds,x=p.lastAction,A=p.lastInteractedWith,O=b(I)||new Set,C=S(I,O);t.useEffect((function(){if(i!==Q){var e,t=h(C);try{for(t.s();!(e=t.n()).done;){var r=e.value,n=y(a,r);i({element:a[r],isBranch:n,isExpanded:n?g.has(r):void 0,isSelected:I.has(r),isDisabled:w.has(r),isHalfSelected:n?W.has(r):void 0,treeState:p})}}catch(e){t.e(e)}finally{t.f()}}}),[a,I,g,w,W,C,i,p]);var D=b(g)||new Set;return t.useEffect((function(){var e=S(g,D);if(s!==Q){var t,r=h(e);try{for(r.s();!(t=r.n()).done;){var n=t.value;s({element:a[n],isExpanded:g.has(n),isSelected:I.has(n),isDisabled:w.has(n),isHalfSelected:W.has(n),treeState:p})}}catch(e){r.e(e)}finally{r.f()}}}),[a,I,g,w,W,D,s,p]),t.useEffect((function(){if(u&&c){var e=new Set(C);A&&e.add(A);var t,r=function(e,t,a,r){var n,d={every:new Set,some:new Set,none:new Set},l=h(t);try{for(l.s();!(n=l.n()).done;)for(var i=n.value;;){var s=m(e,i);if(0===s||r.has(s))break;var c=e[s].children.filter((function(e){return!r.has(e)}));if(0===c.length)break;c.some((function(e){return a.has(e)}))?c.every((function(e){return a.has(e)}))?d.every.add(s):d.some.add(s):d.none.add(s),i=s}}catch(e){l.e(e)}finally{l.f()}return d}(a,e,I,w),n=r.every,d=r.some,l=r.none,i=h(n);try{for(i.s();!(t=i.n()).done;){var s=t.value;I.has(s)||v({type:_,id:s,multiSelect:c,keepFocus:!0,NotUserAction:!0,lastInteractedWith:A})}}catch(e){i.e(e)}finally{i.f()}var o,f=h(d);try{for(f.s();!(o=f.n()).done;){var p=o.value;W.has(p)||v({type:T,id:p,multiSelect:c,keepFocus:!0,NotUserAction:!0,lastInteractedWith:A})}}catch(e){f.e(e)}finally{f.f()}var S,b=h(l);try{for(b.s();!(S=b.n()).done;){var y=S.value;(I.has(y)||W.has(y))&&v({type:K,id:y,multiSelect:c,keepFocus:!0,NotUserAction:!0,lastInteractedWith:A})}}catch(e){b.e(e)}finally{b.f()}}}),[a,c,u,I,g,w,W,x,O,C,A]),t.useEffect((function(){var e;if(null!=A&&null!=E){var t=l.current[E];null!=(e=t)&&e.focus&&e.focus()}}),[E,l,A]),[p,v]},J={select:"SELECT",focus:"FOCUS",exclusiveSelect:"EXCLUSIVE_SELECT"},Q=function(){},Z=a.forwardRef((function(e,n){var d=e.data,l=e.nodeRenderer,i=e.onSelect,u=void 0===i?Q:i,f=e.onExpand,p=void 0===f?Q:f,h=e.className,v=void 0===h?"":h,I=e.multiSelect,S=void 0!==I&&I,b=e.propagateSelect,y=void 0!==b&&b,m=e.propagateSelectUpwards,g=void 0!==m&&m,w=e.propagateCollapse,E=void 0!==w&&w,W=e.expandOnKeyboardSelect,x=void 0!==W&&W,A=e.togglableSelect,O=void 0!==A&&A,C=e.defaultExpandedIds,N=void 0===C?[]:C,j=e.defaultSelectedIds,L=void 0===j?[]:j,P=e.defaultDisabledIds,F=void 0===P?[]:P,R=e.clickAction,T=void 0===R?J.select:R,_=e.onBlur,K=c(e,k),B=t.useRef({}),H=o($({data:d,defaultExpandedIds:N,defaultSelectedIds:L,defaultDisabledIds:F,nodeRefs:B,onSelect:u,onExpand:p,multiSelect:S,propagateSelect:y,propagateSelectUpwards:g}),2),z=H[0],G=H[1];y=y&&S;var M=t.useRef();return null!=n&&(M=n),a.createElement("ul",s({className:r(U.root,v),role:"tree","aria-multiselectable":S,ref:M,onBlur:function(e){D(e,M.current,(function(){_&&_({treeState:z,dispatch:G}),G({type:q})}))},onKeyDown:ae({data:d,tabbableId:z.tabbableId,expandedIds:z.expandedIds,selectedIds:z.selectedIds,disabledIds:z.disabledIds,halfSelectedIds:z.halfSelectedIds,dispatch:G,propagateCollapse:E,propagateSelect:y,multiSelect:S,expandOnKeyboardSelect:x,togglableSelect:O})},K),d[0].children.map((function(e,t){return a.createElement(ee,s({key:e,data:d,element:d[e],setsize:d[0].children.length,posinset:t+1,level:1},z,{state:z,dispatch:G,nodeRefs:B,baseClassNames:U,nodeRenderer:l,propagateCollapse:E,propagateSelect:y,propagateSelectUpwards:g,multiSelect:S,togglableSelect:O,clickAction:T}))})))})),ee=function(e){var t=e.element,n=e.dispatch,d=e.data,l=e.selectedIds,c=e.tabbableId,o=e.isFocused,f=e.expandedIds,p=e.disabledIds,h=e.halfSelectedIds,I=e.lastUserSelect,S=e.nodeRefs,b=e.baseClassNames,m=e.nodeRenderer,w=e.setsize,E=e.posinset,W=e.level,x=e.propagateCollapse,D=e.propagateSelect,k=e.multiSelect,N=e.togglableSelect,j=e.clickAction,U=e.state,L=function(e){if(!(e.ctrlKey||e.altKey||e.shiftKey))if(f.has(t.id)&&x){var a=[t.id].concat(u(g(d,t.id,new Set)));n({type:P,ids:a,lastInteractedWith:t.id})}else n({type:B,id:t.id,lastInteractedWith:t.id})},F=function(){return n({type:M,id:t.id,lastInteractedWith:t.id})},R=function(e){if(e.shiftKey){var a=A({data:d,expandedIds:f,from:I,to:t.id,lastInteractedWith:t.id}).filter((function(e){return!p.has(e)}));a=D?C(d,a,p):a,n({type:G,select:!0,multiSelect:k,ids:a,lastInteractedWith:t.id})}else e.ctrlKey||j===J.select?(n({type:N?H:_,id:t.id,multiSelect:k,lastInteractedWith:t.id}),D&&!p.has(t.id)&&n({type:z,ids:C(d,[t.id],p),select:!N||!l.has(t.id),multiSelect:k,lastInteractedWith:t.id})):j===J.exclusiveSelect?n({type:N?H:_,id:t.id,multiSelect:!1,lastInteractedWith:t.id}):j===J.focus&&n({type:M,id:t.id,lastInteractedWith:t.id})},T=function(e){var a;return r(e,(i(a={},"".concat(e,"--expanded"),f.has(t.id)),i(a,"".concat(e,"--selected"),l.has(t.id)),i(a,"".concat(e,"--focused"),c===t.id&&o),a))};return y(d,t.id)?a.createElement("li",{role:"treeitem","aria-expanded":f.has(t.id),"aria-selected":O({isSelected:l.has(t.id),isDisabled:p.has(t.id),multiSelect:k}),"aria-setsize":w,"aria-posinset":E,"aria-level":W,disabled:p.has(t.id),"aria-disabled":p.has(t.id),tabIndex:c===t.id?0:-1,ref:function(e){return S.current[t.id]=e},className:b.branchWrapper},m({element:t,isBranch:!0,isSelected:l.has(t.id),isHalfSelected:h.has(t.id),isExpanded:f.has(t.id),isDisabled:p.has(t.id),dispatch:n,getNodeProps:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=e.onClick;return{onClick:null==t?v(R,L,F):v(t,F),className:r(T(b.node),b.branch)}},setsize:w,posinset:E,level:W,handleSelect:R,handleExpand:L,treeState:U}),a.createElement(te,s({element:t,getClasses:T},e))):a.createElement("li",{role:"none",className:T(b.leafListItem)},m({element:t,isBranch:!1,isSelected:l.has(t.id),isHalfSelected:void 0,isExpanded:!1,isDisabled:p.has(t.id),dispatch:n,getNodeProps:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},a=e.onClick;return{role:"treeitem",tabIndex:c===t.id?0:-1,onClick:v(null==a?R:a,F),ref:function(e){return S.current[t.id]=e},className:r(T(b.node),b.leaf),"aria-setsize":w,"aria-posinset":E,"aria-level":W,"aria-selected":O({isSelected:l.has(t.id),isDisabled:p.has(t.id),multiSelect:k}),disabled:p.has(t.id),"aria-disabled":p.has(t.id)}},setsize:w,posinset:E,level:W,handleSelect:R,handleExpand:Q,treeState:U}))},te=function(e){var t=e.data,r=e.element,n=e.expandedIds,d=e.getClasses,l=e.baseClassNames,i=e.level,o=c(e,N);return a.createElement("ul",{role:"group",className:d(l.nodeGroup)},n.has(r.id)&&r.children.map((function(e,d){return a.createElement(ee,s({data:t,expandedIds:n,baseClassNames:l,key:e,element:t[e],setsize:r.children.length,posinset:d+1,level:i+1},o))})))},ae=function(e){var t=e.data,a=e.expandedIds,r=e.selectedIds,n=e.disabledIds,d=e.tabbableId,l=e.dispatch,i=e.propagateCollapse,s=e.propagateSelect,o=e.multiSelect,f=e.expandOnKeyboardSelect,p=e.togglableSelect;return function(e){var h=t[d],v=h.id;if(e.ctrlKey){if("a"===e.key){e.preventDefault();t[0];var I=c(t,j),S=Object.values(I).map((function(e){return e.id})).filter((function(e){return!n.has(e)}));l({type:z,multiSelect:o,select:Array.from(r).filter((function(e){return!n.has(e)})).length!==S.length,ids:S,lastInteractedWith:h.id})}else if(e.shiftKey&&("Home"===e.key||"End"===e.key)){var b="Home"===e.key?t[0].children[0]:E(t,v,a),w=A({data:t,expandedIds:a,from:v,to:b}).filter((function(e){return!n.has(e)}));l({type:z,multiSelect:o,select:!0,ids:s?C(t,w,n):w}),l({type:M,id:b,lastInteractedWith:b})}}else{if(e.shiftKey)switch(e.key){case"ArrowUp":e.preventDefault();var O=W(t,v,a);return void(null==O&&n.has(O)||(l({type:z,ids:s?C(t,[O],n):[O],select:!0,multiSelect:o,lastInteractedWith:O}),l({type:M,id:O,lastInteractedWith:O})));case"ArrowDown":e.preventDefault();var D=x(t,v,a);return void(null==D&&n.has(D)||(l({type:z,ids:s?C(t,[D],n):[D],multiSelect:o,select:!0,lastInteractedWith:D}),l({type:M,id:D,lastInteractedWith:D})))}switch(e.key){case"ArrowDown":e.preventDefault();var k=x(t,v,a);return void(null!=k&&l({type:M,id:k,lastInteractedWith:k}));case"ArrowUp":e.preventDefault();var N=W(t,v,a);return void(null!=N&&l({type:M,id:N,lastInteractedWith:N}));case"ArrowLeft":if(e.preventDefault(),y(t,v)&&a.has(d))if(i){var U=[v].concat(u(g(t,v,new Set)));l({type:P,ids:U,lastInteractedWith:h.id})}else l({type:L,id:v,lastInteractedWith:v});else if(!t[0].children.includes(v)){var T=m(t,v);l({type:M,id:T,lastInteractedWith:T})}return;case"ArrowRight":return e.preventDefault(),void(y(t,v)&&(a.has(d)?l({type:M,id:h.children[0],lastInteractedWith:h.children[0]}):l({type:F,id:v,lastInteractedWith:v})));case"Home":e.preventDefault(),l({type:M,id:t[0].children[0],lastInteractedWith:t[0].children[0]});break;case"End":e.preventDefault();var K=E(t,t[0].id,a);return void l({type:M,id:K,lastInteractedWith:K});case"*":e.preventDefault();var G=t[m(t,v)].children.filter((function(e){return y(t,e)}));return void l({type:R,ids:G,lastInteractedWith:v});case"Enter":case" ":case"Spacebar":return e.preventDefault(),l({type:p?H:_,id:v,multiSelect:o,lastInteractedWith:v}),s&&!n.has(h.id)&&l({type:z,ids:C(t,[v],n),select:!p||!r.has(v),multiSelect:o,lastInteractedWith:v}),void(f&&l({type:B,id:v,lastInteractedWith:v}));default:if(1===e.key.length)for(var q=x(t,v,a);q!==v;)if(null!=q){if(t[q].name[0].toLowerCase()===e.key.toLowerCase())return void l({type:M,id:q,lastInteractedWith:v});q=x(t,q,a)}else q=t[0].children[0];return}}}};Z.propTypes={data:n.array.isRequired,onSelect:n.func,onExpand:n.func,className:n.string,nodeRenderer:n.func.isRequired,defaultExpandedIds:n.array,defaultSelectedIds:n.array,defaultDisabledIds:n.array,propagateCollapse:n.bool,propagateSelect:n.bool,propagateSelectUpwards:n.bool,multiSelect:n.bool,expandOnKeyboardSelect:n.bool,togglableSelect:n.bool,clickAction:n.oneOf(Object.values(J))},exports.default=Z,exports.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 d,l=h(r.children);try{for(l.s();!(d=l.n()).done;){e(d.value,r.id)}}catch(e){l.e(e)}finally{l.f()}r.children=r.children.map((function(e){return e.id}))}}(e,null),a}; |
@@ -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 p=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}}},h=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(h(e,t)),f(h(t,e))))},S=function(e){var r=t();return a(function(){r.current=e},[e]),r.current},v=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)&&v(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(v(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){var t=e.isSelected,a=e.isDisabled,r=e.multiSelect;if(!a)return r?t:!!t||void 0},O=function(e,t,a){return t.concat.apply(t,f(t.filter(function(t){return v(e,t)}).map(function(t){return b(e,t,a)})))},D=function(e,t,a){window.navigator.userAgent.match(/Trident/)?setTimeout(function(){return!t.contains(document.activeElement)&&a()},0):!t.contains(e.nativeEvent.relatedTarget)&&a()},k={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"},C="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 C: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 p=new Set([].concat(f(e.expandedIds),f(t.ids)));return c({},e,{expandedIds:p,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 S=new Set(e.halfSelectedIds),v=new Set(e.selectedIds);return S.add(t.id),v.delete(t.id),c({},e,{selectedIds:v,halfSelectedIds:S,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))):h(e.selectedIds,new Set(A));var O=h(e.halfSelectedIds,W);return c({},e,{selectedIds:W,halfSelectedIds:O,lastAction:t.type,lastInteractedWith:t.lastInteractedWith})}return e;case _:var D,k=t.ids.filter(function(t){return!e.disabledIds.has(t)});if(t.multiSelect){D=t.select?new Set(k):h(e.selectedIds,new Set(k));var M=h(e.halfSelectedIds,D);return c({},e,{selectedIds:D,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,p=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),h=p[0],b=p[1],m=h.selectedIds,w=h.expandedIds,g=h.disabledIds,x=h.tabbableId,E=h.halfSelectedIds,W=h.lastAction,A=h.lastInteractedWith,O=S(m)||new Set,D=I(m,O);a(function(){if(s!==q){var e=!0,a=!1,r=void 0;try{for(var n,l=D[Symbol.iterator]();!(e=(n=l.next()).done);e=!0){var d=n.value,i=v(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:h})}}catch(e){a=!0,r=e}finally{try{e||null==l.return||l.return()}finally{if(a)throw r}}}},[t,m,w,g,E,D,s,h]);var k=S(w)||new Set;return a(function(){var e=I(w,k);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:h})}}catch(e){r=!0,n=e}finally{try{a||null==d.return||d.return()}finally{if(r)throw n}}}},[t,m,w,g,E,k,c,h]),a(function(){if(f&&o){var e=new Set(D);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 p=c.value;m.has(p)||b({type:F,id:p,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 h=!0,I=!1,S=void 0;try{for(var v,w=n[Symbol.iterator]();!(h=(v=w.next()).done);h=!0){var x=v.value;E.has(x)||b({type:j,id:x,multiSelect:o,keepFocus:!0,NotUserAction:!0,lastInteractedWith:A})}}catch(e){I=!0,S=e}finally{try{h||null==w.return||w.return()}finally{if(I)throw S}}var W=!0,O=!1,k=void 0;try{for(var C,U=l[Symbol.iterator]();!(W=(C=U.next()).done);W=!0){var N=C.value;(m.has(N)||E.has(N))&&b({type:P,id:N,multiSelect:o,keepFocus:!0,NotUserAction:!0,lastInteractedWith:A})}}catch(e){O=!0,k=e}finally{try{W||null==U.return||U.return()}finally{if(O)throw k}}}},[t,o,f,m,w,g,E,W,O,D,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]),[h,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,p=void 0===f?q:f,h=a.className,I=void 0===h?"":h,S=a.multiSelect,v=void 0!==S&&S,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,C=a.defaultExpandedIds,U=void 0===C?[]:C,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:p,multiSelect:v,propagateSelect:b,propagateSelectUpwards:w}),2),z=B[0],G=B[1];b=b&&v;var M=t();return null!=r&&(M=r),e.createElement("ul",i({className:n(k.root,I),role:"tree","aria-multiselectable":v,ref:M,onBlur:function(e){D(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:v,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:k,nodeRenderer:d,setsize:l[0].children.length,posinset:a+1,level:1,propagateCollapse:x,propagateSelect:b,propagateSelectUpwards:w,multiSelect:v,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,h=a.disabledIds,I=a.halfSelectedIds,S=a.lastUserSelect,y=a.nodeRefs,m=a.baseClassNames,w=a.nodeRenderer,g=a.setsize,x=a.posinset,W=a.level,D=a.propagateCollapse,k=a.propagateSelect,C=(a.propagateSelectUpwards,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)&&D){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:S,to:r.id,lastInteractedWith:r.id}).filter(function(e){return!h.has(e)});t=k?O(i,t,h):t,l({type:_,select:!0,multiSelect:C,ids:t,lastInteractedWith:r.id})}else e.ctrlKey||L===Y.select?(l({type:N?T:F,id:r.id,multiSelect:C,lastInteractedWith:r.id}),k&&!h.has(r.id)&&l({type:K,ids:O(i,[r.id],h),select:!N||!s.has(r.id),multiSelect:C,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 v(i,r.id)?e.createElement("li",{role:"treeitem","aria-expanded":u.has(r.id),"aria-selected":A({isSelected:s.has(r.id),isDisabled:h.has(r.id),multiSelect:C}),"aria-setsize":g,"aria-posinset":x,"aria-level":W,disabled:h.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:h.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?p(G,H,z):p(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:h,lastUserSelect:S,dispatch:l,data:i,element:i[a],nodeRefs:y,baseClassNames:m,nodeRenderer:w,setsize:r.children.length,posinset:n+1,level:W+1,propagateCollapse:D,propagateSelect:k,propagateSelectUpwards:k,multiSelect:C,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:h.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:p(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({isSelected:s.has(r.id),isDisabled:h.has(r.id),multiSelect:C}),disabled:h.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,p=e.togglableSelect;return function(e){var h=t[l],I=h.id;if(e.ctrlKey){if("a"===e.key){e.preventDefault();t[0];var S=o(t,["0"]),m=Object.values(S).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:h.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 D=g(t,I,a);return void(null==D&&n.has(D)||(d({type:K,ids:s?O(t,[D],n):[D],select:!0,multiSelect:c,lastInteractedWith:D}),d({type:B,id:D,lastInteractedWith:D})));case"ArrowDown":e.preventDefault();var k=x(t,I,a);return void(null==k&&n.has(k)||(d({type:K,ids:s?O(t,[k],n):[k],multiSelect:c,select:!0,lastInteractedWith:k}),d({type:B,id:k,lastInteractedWith:k})))}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 P=g(t,I,a);return void(null!=P&&d({type:B,id:P,lastInteractedWith:P}));case"ArrowLeft":if(e.preventDefault(),v(t,I)&&a.has(l))if(i){var _=[I].concat(f(b(t,I,new Set)));d({type:U,ids:_,lastInteractedWith:h.id})}else d({type:C,id:I,lastInteractedWith:I});else if(!t[0].children.includes(I)){var H=y(t,I);d({type:B,id:H,lastInteractedWith:H})}return;case"ArrowRight":return e.preventDefault(),void(v(t,I)&&(a.has(l)?d({type:B,id:h.children[0],lastInteractedWith:h.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 z=w(t,t[0].id,a);return void d({type:B,id:z,lastInteractedWith:z});case"*":e.preventDefault();var G=t[y(t,I)].children.filter(function(e){return v(t,e)});return void d({type:L,ids:G,lastInteractedWith:I});case"Enter":case" ":case"Spacebar":return e.preventDefault(),!n.has(I)&&d({type:p?T:F,id:h.id,multiSelect:c,lastInteractedWith:h.id}),s&&!n.has(h.id)&&d({type:K,ids:O(t,[h.id],n),select:!p||!r.has(h.id),multiSelect:c,lastInteractedWith:h.id}),void(u&&d({type:R,id:I,lastInteractedWith:I}));default:if(1===e.key.length)for(var M=x(t,I,a);M!==I;)if(null!=M){if(t[M].name[0].toLowerCase()===e.key.toLowerCase())return void d({type:B,id:M,lastInteractedWith:I});M=x(t,M,a)}else M=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 n}from"react";import r from"classnames";import d from"prop-types";function l(e,t){var a=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),a.push.apply(a,n)}return a}function i(e){for(var t=1;t<arguments.length;t++){var a=null!=arguments[t]?arguments[t]:{};t%2?l(Object(a),!0).forEach((function(t){s(e,t,a[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(a)):l(Object(a)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(a,t))}))}return e}function s(e,t,a){return t in e?Object.defineProperty(e,t,{value:a,enumerable:!0,configurable:!0,writable:!0}):e[t]=a,e}function c(){return(c=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var a=arguments[t];for(var n in a)Object.prototype.hasOwnProperty.call(a,n)&&(e[n]=a[n])}return e}).apply(this,arguments)}function o(e,t){if(null==e)return{};var a,n,r=function(e,t){if(null==e)return{};var a,n,r={},d=Object.keys(e);for(n=0;n<d.length;n++)a=d[n],t.indexOf(a)>=0||(r[a]=e[a]);return r}(e,t);if(Object.getOwnPropertySymbols){var d=Object.getOwnPropertySymbols(e);for(n=0;n<d.length;n++)a=d[n],t.indexOf(a)>=0||Object.prototype.propertyIsEnumerable.call(e,a)&&(r[a]=e[a])}return r}function u(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var a=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null==a)return;var n,r,d=[],l=!0,i=!1;try{for(a=a.call(e);!(l=(n=a.next()).done)&&(d.push(n.value),!t||d.length!==t);l=!0);}catch(e){i=!0,r=e}finally{try{l||null==a.return||a.return()}finally{if(i)throw r}}return d}(e,t)||p(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function f(e){return function(e){if(Array.isArray(e))return h(e)}(e)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(e)||p(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function p(e,t){if(e){if("string"==typeof e)return h(e,t);var a=Object.prototype.toString.call(e).slice(8,-1);return"Object"===a&&e.constructor&&(a=e.constructor.name),"Map"===a||"Set"===a?Array.from(e):"Arguments"===a||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(a)?h(e,t):void 0}}function h(e,t){(null==t||t>e.length)&&(t=e.length);for(var a=0,n=new Array(t);a<t;a++)n[a]=e[a];return n}function v(e,t){var a="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(!a){if(Array.isArray(e)||(a=p(e))||t&&e&&"number"==typeof e.length){a&&(e=a);var n=0,r=function(){};return{s:r,n:function(){return n>=e.length?{done:!0}:{done:!1,value:e[n++]}},e:function(e){throw e},f:r}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var d,l=!0,i=!1;return{s:function(){a=a.call(e)},n:function(){var e=a.next();return l=e.done,e},e:function(e){i=!0,d=e},f:function(){try{l||null==a.return||a.return()}finally{if(i)throw d}}}}var I=function(){for(var e=arguments.length,t=new Array(e),a=0;a<e;a++)t[a]=arguments[a];return function(e){var a,n=v(t);try{for(n.s();!(a=n.n()).done;){var r=a.value;if(r&&r(e),e.defaultPrevented)break}}catch(e){n.e(e)}finally{n.f()}}},S=function(e,t){var a,n=new Set,r=v(e);try{for(r.s();!(a=r.n()).done;){var d=a.value;t.has(d)||n.add(d)}}catch(e){r.e(e)}finally{r.f()}return n},b=function(e,t){return new Set([].concat(f(S(e,t)),f(S(t,e))))},y=function(e){var n=t();return a((function(){n.current=e}),[e]),n.current},m=function(e,t){return null!=e[t].children&&e[t].children.length>0},g=function(e,t){return e[t].parent},w=function(e,t,a){var n=[];return function e(t,r){var d=t[r];if(null!=d.children){var l,i=v(d.children.filter((function(e){return!a.has(e)})));try{for(i.s();!(l=i.n()).done;){var s=l.value;n.push(s),e(t,s)}}catch(e){i.e(e)}finally{i.f()}}}(e,t),n},E=function(e,t,a){var n=g(e,t);if(null!=n){var r=e[n],d=r.children.indexOf(t)+a;if(r.children[d])return r.children[d]}return null},W=function(e,t,a){var n=e[t];for(e[0].id===t&&(n=e[e[t].children[e[t].children.length-1]]);a.has(n.id)&&m(e,n.id);)n=e[n.children[n.children.length-1]];return n.id},x=function(e,t,a){if(t===e[0].children[0])return null;var n=E(e,t,-1);return null==n?g(e,t):W(e,n,a)},A=function(e,t,a){var n=e[t].id;if(m(e,n)&&a.has(n))return e[n].children[0];for(;;){var r=E(e,n,1);if(null!=r)return r;if(null==(n=g(e,n)))return null}},O=function(e){var t=e.data,a=e.expandedIds,n=e.from,r=e.to,d=[],l=Object.keys(t).length,i=0,s=n;if(d.push(n),n<r)for(;i<l&&(s=A(t,s,a),d.push(s),null!=s&&s!==r);)i+=1;else if(n>r)for(;i<l&&(s=x(t,s,a),d.push(s),null!=s&&s!==r);)i+=1;return d},C=function(e){var t=0,a=[];return function e(n,r){if(n.id=t,n.parent=r,a[t]=n,t+=1,null!=n.children&&0!==n.children.length){var d,l=v(n.children);try{for(l.s();!(d=l.n()).done;){e(d.value,n.id)}}catch(e){l.e(e)}finally{l.f()}n.children=n.children.map((function(e){return e.id}))}}(e,null),a},D=function(e){var t=e.isSelected,a=e.isDisabled,n=e.multiSelect;if(!a)return n?t:!!t||void 0},k=function(e,t,a){return t.concat.apply(t,f(t.filter((function(t){return m(e,t)})).map((function(t){return w(e,t,a)}))))},N=function(e,t,a){window.navigator.userAgent.match(/Trident/)?setTimeout((function(){return!t.contains(document.activeElement)&&a()}),0):!t.contains(e.nativeEvent.relatedTarget)&&a()},U=["data","nodeRenderer","onSelect","onExpand","className","multiSelect","propagateSelect","propagateSelectUpwards","propagateCollapse","expandOnKeyboardSelect","togglableSelect","defaultExpandedIds","defaultSelectedIds","defaultDisabledIds","clickAction","onBlur"],j=["data","element","expandedIds","getClasses","baseClassNames","level"],L=["0"],P={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"},F="COLLAPSE",T="COLLAPSE_MANY",K="EXPAND",R="EXPAND_MANY",_="HALF_SELECT",B="SELECT",H="DESELECT",z="TOGGLE",G="TOGGLE_SELECT",M="SELECT_MANY",X="EXCLUSIVE_CHANGE_SELECT_MANY",Y="FOCUS",q="BLUR",V="DISABLE",$="ENABLE",J=function(e,t){switch(t.type){case F:var a=new Set(e.expandedIds);return a.delete(t.id),i(i({},e),{},{expandedIds:a,tabbableId:t.id,isFocused:!0,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case T:var n,r=new Set(e.expandedIds),d=v(t.ids);try{for(d.s();!(n=d.n()).done;){var l=n.value;r.delete(l)}}catch(e){d.e(e)}finally{d.f()}return i(i({},e),{},{expandedIds:r,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case K:var s=new Set(e.expandedIds);return s.add(t.id),i(i({},e),{},{expandedIds:s,tabbableId:t.id,isFocused:!0,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case R:var c=new Set([].concat(f(e.expandedIds),f(t.ids)));return i(i({},e),{},{expandedIds:c,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case z:var o=new Set(e.expandedIds);return e.expandedIds.has(t.id)?o.delete(t.id):o.add(t.id),i(i({},e),{},{expandedIds:o,tabbableId:t.id,isFocused:!0,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case _:if(e.disabledIds.has(t.id))return e;var u=new Set(e.halfSelectedIds),p=new Set(e.selectedIds);return u.add(t.id),p.delete(t.id),i(i({},e),{},{selectedIds:p,halfSelectedIds:u,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case B:if(e.disabledIds.has(t.id))return e;var h;t.multiSelect?(h=new Set(e.selectedIds)).add(t.id):(h=new Set).add(t.id);var I=new Set(e.halfSelectedIds);return I.delete(t.id),i(i({},e),{},{selectedIds:h,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 H:if(e.disabledIds.has(t.id))return e;var b;t.multiSelect?(b=new Set(e.selectedIds)).delete(t.id):b=new Set;var y=new Set(e.halfSelectedIds);return y.delete(t.id),i(i({},e),{},{selectedIds:b,halfSelectedIds:y,tabbableId:t.keepFocus?e.tabbableId:t.id,isFocused:!0,lastUserSelect:t.NotUserAction?e.lastUserSelect:t.id,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case G:if(e.disabledIds.has(t.id))return e;var m,g=e.selectedIds.has(t.id);t.multiSelect?(m=new Set(e.selectedIds),g?m.delete(t.id):m.add(t.id)):(m=new Set,g||m.add(t.id));var w=new Set(e.halfSelectedIds);return w.delete(t.id),i(i({},e),{},{selectedIds:m,halfSelectedIds:w,tabbableId:t.id,isFocused:!0,lastUserSelect:t.NotUserAction?e.lastUserSelect:t.id,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case M:var E,W=t.ids.filter((function(t){return!e.disabledIds.has(t)}));if(t.multiSelect){E=t.select?new Set([].concat(f(e.selectedIds),f(W))):S(e.selectedIds,new Set(W));var x=S(e.halfSelectedIds,E);return i(i({},e),{},{selectedIds:E,halfSelectedIds:x,lastAction:t.type,lastInteractedWith:t.lastInteractedWith})}return e;case X:var A,O=t.ids.filter((function(t){return!e.disabledIds.has(t)}));if(t.multiSelect){A=t.select?new Set(O):S(e.selectedIds,new Set(O));var C=S(e.halfSelectedIds,A);return i(i({},e),{},{selectedIds:A,halfSelectedIds:C,lastAction:t.type,lastInteractedWith:t.lastInteractedWith})}return e;case Y:return i(i({},e),{},{tabbableId:t.id,isFocused:!0,lastAction:t.type,lastInteractedWith:t.lastInteractedWith});case q:return i(i({},e),{},{isFocused:!1});case V:var D=new Set(e.disabledIds);return D.add(t.id),i(i({},e),{},{disabledIds:D});case $:var k=new Set(e.disabledIds);return k.delete(t.id),i(i({},e),{},{disabledIds:k});default:throw new Error("Invalid action passed to the reducer")}},Q=function(e){var t=e.data,r=e.defaultExpandedIds,d=e.defaultSelectedIds,l=e.defaultDisabledIds,i=e.nodeRefs,s=e.onSelect,c=e.onExpand,o=e.multiSelect,f=e.propagateSelectUpwards,p=u(n(J,{selectedIds:new Set(d),tabbableId:t[0].children[0],isFocused:!1,expandedIds:new Set(r),halfSelectedIds:new Set,lastUserSelect:t[0].children[0],lastInteractedWith:null,disabledIds:new Set(l)}),2),h=p[0],I=p[1],S=h.selectedIds,w=h.expandedIds,E=h.disabledIds,W=h.tabbableId,x=h.halfSelectedIds,A=h.lastAction,O=h.lastInteractedWith,C=y(S)||new Set,D=b(S,C);a((function(){if(s!==ee){var e,a=v(D);try{for(a.s();!(e=a.n()).done;){var n=e.value,r=m(t,n);s({element:t[n],isBranch:r,isExpanded:r?w.has(n):void 0,isSelected:S.has(n),isDisabled:E.has(n),isHalfSelected:r?x.has(n):void 0,treeState:h})}}catch(e){a.e(e)}finally{a.f()}}}),[t,S,w,E,x,D,s,h]);var k=y(w)||new Set;return a((function(){var e=b(w,k);if(c!==ee){var a,n=v(e);try{for(n.s();!(a=n.n()).done;){var r=a.value;c({element:t[r],isExpanded:w.has(r),isSelected:S.has(r),isDisabled:E.has(r),isHalfSelected:x.has(r),treeState:h})}}catch(e){n.e(e)}finally{n.f()}}}),[t,S,w,E,x,k,c,h]),a((function(){if(f&&o){var e=new Set(D);O&&e.add(O);var a,n=function(e,t,a,n){var r,d={every:new Set,some:new Set,none:new Set},l=v(t);try{for(l.s();!(r=l.n()).done;)for(var i=r.value;;){var s=g(e,i);if(0===s||n.has(s))break;var c=e[s].children.filter((function(e){return!n.has(e)}));if(0===c.length)break;c.some((function(e){return a.has(e)}))?c.every((function(e){return a.has(e)}))?d.every.add(s):d.some.add(s):d.none.add(s),i=s}}catch(e){l.e(e)}finally{l.f()}return d}(t,e,S,E),r=n.every,d=n.some,l=n.none,i=v(r);try{for(i.s();!(a=i.n()).done;){var s=a.value;S.has(s)||I({type:B,id:s,multiSelect:o,keepFocus:!0,NotUserAction:!0,lastInteractedWith:O})}}catch(e){i.e(e)}finally{i.f()}var c,u=v(d);try{for(u.s();!(c=u.n()).done;){var p=c.value;x.has(p)||I({type:_,id:p,multiSelect:o,keepFocus:!0,NotUserAction:!0,lastInteractedWith:O})}}catch(e){u.e(e)}finally{u.f()}var h,b=v(l);try{for(b.s();!(h=b.n()).done;){var y=h.value;(S.has(y)||x.has(y))&&I({type:H,id:y,multiSelect:o,keepFocus:!0,NotUserAction:!0,lastInteractedWith:O})}}catch(e){b.e(e)}finally{b.f()}}}),[t,o,f,S,w,E,x,A,C,D,O]),a((function(){var e;if(null!=O&&null!=W){var t=i.current[W];null!=(e=t)&&e.focus&&e.focus()}}),[W,i,O]),[h,I]},Z={select:"SELECT",focus:"FOCUS",exclusiveSelect:"EXCLUSIVE_SELECT"},ee=function(){},te=e.forwardRef((function(a,n){var d=a.data,l=a.nodeRenderer,i=a.onSelect,s=void 0===i?ee:i,f=a.onExpand,p=void 0===f?ee:f,h=a.className,v=void 0===h?"":h,I=a.multiSelect,S=void 0!==I&&I,b=a.propagateSelect,y=void 0!==b&&b,m=a.propagateSelectUpwards,g=void 0!==m&&m,w=a.propagateCollapse,E=void 0!==w&&w,W=a.expandOnKeyboardSelect,x=void 0!==W&&W,A=a.togglableSelect,O=void 0!==A&&A,C=a.defaultExpandedIds,D=void 0===C?[]:C,k=a.defaultSelectedIds,j=void 0===k?[]:k,L=a.defaultDisabledIds,F=void 0===L?[]:L,T=a.clickAction,K=void 0===T?Z.select:T,R=a.onBlur,_=o(a,U),B=t({}),H=u(Q({data:d,defaultExpandedIds:D,defaultSelectedIds:j,defaultDisabledIds:F,nodeRefs:B,onSelect:s,onExpand:p,multiSelect:S,propagateSelect:y,propagateSelectUpwards:g}),2),z=H[0],G=H[1];y=y&&S;var M=t();return null!=n&&(M=n),e.createElement("ul",c({className:r(P.root,v),role:"tree","aria-multiselectable":S,ref:M,onBlur:function(e){N(e,M.current,(function(){R&&R({treeState:z,dispatch:G}),G({type:q})}))},onKeyDown:re({data:d,tabbableId:z.tabbableId,expandedIds:z.expandedIds,selectedIds:z.selectedIds,disabledIds:z.disabledIds,halfSelectedIds:z.halfSelectedIds,dispatch:G,propagateCollapse:E,propagateSelect:y,multiSelect:S,expandOnKeyboardSelect:x,togglableSelect:O})},_),d[0].children.map((function(t,a){return e.createElement(ae,c({key:t,data:d,element:d[t],setsize:d[0].children.length,posinset:a+1,level:1},z,{state:z,dispatch:G,nodeRefs:B,baseClassNames:P,nodeRenderer:l,propagateCollapse:E,propagateSelect:y,propagateSelectUpwards:g,multiSelect:S,togglableSelect:O,clickAction:K}))})))})),ae=function(t){var a=t.element,n=t.dispatch,d=t.data,l=t.selectedIds,i=t.tabbableId,o=t.isFocused,u=t.expandedIds,p=t.disabledIds,h=t.halfSelectedIds,v=t.lastUserSelect,S=t.nodeRefs,b=t.baseClassNames,y=t.nodeRenderer,g=t.setsize,E=t.posinset,W=t.level,x=t.propagateCollapse,A=t.propagateSelect,C=t.multiSelect,N=t.togglableSelect,U=t.clickAction,j=t.state,L=function(e){if(!(e.ctrlKey||e.altKey||e.shiftKey))if(u.has(a.id)&&x){var t=[a.id].concat(f(w(d,a.id,new Set)));n({type:T,ids:t,lastInteractedWith:a.id})}else n({type:z,id:a.id,lastInteractedWith:a.id})},P=function(){return n({type:Y,id:a.id,lastInteractedWith:a.id})},F=function(e){if(e.shiftKey){var t=O({data:d,expandedIds:u,from:v,to:a.id,lastInteractedWith:a.id}).filter((function(e){return!p.has(e)}));t=A?k(d,t,p):t,n({type:X,select:!0,multiSelect:C,ids:t,lastInteractedWith:a.id})}else e.ctrlKey||U===Z.select?(n({type:N?G:B,id:a.id,multiSelect:C,lastInteractedWith:a.id}),A&&!p.has(a.id)&&n({type:M,ids:k(d,[a.id],p),select:!N||!l.has(a.id),multiSelect:C,lastInteractedWith:a.id})):U===Z.exclusiveSelect?n({type:N?G:B,id:a.id,multiSelect:!1,lastInteractedWith:a.id}):U===Z.focus&&n({type:Y,id:a.id,lastInteractedWith:a.id})},K=function(e){var t;return r(e,(s(t={},"".concat(e,"--expanded"),u.has(a.id)),s(t,"".concat(e,"--selected"),l.has(a.id)),s(t,"".concat(e,"--focused"),i===a.id&&o),t))};return m(d,a.id)?e.createElement("li",{role:"treeitem","aria-expanded":u.has(a.id),"aria-selected":D({isSelected:l.has(a.id),isDisabled:p.has(a.id),multiSelect:C}),"aria-setsize":g,"aria-posinset":E,"aria-level":W,disabled:p.has(a.id),"aria-disabled":p.has(a.id),tabIndex:i===a.id?0:-1,ref:function(e){return S.current[a.id]=e},className:b.branchWrapper},y({element:a,isBranch:!0,isSelected:l.has(a.id),isHalfSelected:h.has(a.id),isExpanded:u.has(a.id),isDisabled:p.has(a.id),dispatch:n,getNodeProps:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=e.onClick;return{onClick:null==t?I(F,L,P):I(t,P),className:r(K(b.node),b.branch)}},setsize:g,posinset:E,level:W,handleSelect:F,handleExpand:L,treeState:j}),e.createElement(ne,c({element:a,getClasses:K},t))):e.createElement("li",{role:"none",className:K(b.leafListItem)},y({element:a,isBranch:!1,isSelected:l.has(a.id),isHalfSelected:void 0,isExpanded:!1,isDisabled:p.has(a.id),dispatch:n,getNodeProps:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=e.onClick;return{role:"treeitem",tabIndex:i===a.id?0:-1,onClick:I(null==t?F:t,P),ref:function(e){return S.current[a.id]=e},className:r(K(b.node),b.leaf),"aria-setsize":g,"aria-posinset":E,"aria-level":W,"aria-selected":D({isSelected:l.has(a.id),isDisabled:p.has(a.id),multiSelect:C}),disabled:p.has(a.id),"aria-disabled":p.has(a.id)}},setsize:g,posinset:E,level:W,handleSelect:F,handleExpand:ee,treeState:j}))},ne=function(t){var a=t.data,n=t.element,r=t.expandedIds,d=t.getClasses,l=t.baseClassNames,i=t.level,s=o(t,j);return e.createElement("ul",{role:"group",className:d(l.nodeGroup)},r.has(n.id)&&n.children.map((function(t,d){return e.createElement(ae,c({data:a,expandedIds:r,baseClassNames:l,key:t,element:a[t],setsize:n.children.length,posinset:d+1,level:i+1},s))})))},re=function(e){var t=e.data,a=e.expandedIds,n=e.selectedIds,r=e.disabledIds,d=e.tabbableId,l=e.dispatch,i=e.propagateCollapse,s=e.propagateSelect,c=e.multiSelect,u=e.expandOnKeyboardSelect,p=e.togglableSelect;return function(e){var h=t[d],v=h.id;if(e.ctrlKey){if("a"===e.key){e.preventDefault();t[0];var I=o(t,L),S=Object.values(I).map((function(e){return e.id})).filter((function(e){return!r.has(e)}));l({type:M,multiSelect:c,select:Array.from(n).filter((function(e){return!r.has(e)})).length!==S.length,ids:S,lastInteractedWith:h.id})}else if(e.shiftKey&&("Home"===e.key||"End"===e.key)){var b="Home"===e.key?t[0].children[0]:W(t,v,a),y=O({data:t,expandedIds:a,from:v,to:b}).filter((function(e){return!r.has(e)}));l({type:M,multiSelect:c,select:!0,ids:s?k(t,y,r):y}),l({type:Y,id:b,lastInteractedWith:b})}}else{if(e.shiftKey)switch(e.key){case"ArrowUp":e.preventDefault();var E=x(t,v,a);return void(null==E&&r.has(E)||(l({type:M,ids:s?k(t,[E],r):[E],select:!0,multiSelect:c,lastInteractedWith:E}),l({type:Y,id:E,lastInteractedWith:E})));case"ArrowDown":e.preventDefault();var C=A(t,v,a);return void(null==C&&r.has(C)||(l({type:M,ids:s?k(t,[C],r):[C],multiSelect:c,select:!0,lastInteractedWith:C}),l({type:Y,id:C,lastInteractedWith:C})))}switch(e.key){case"ArrowDown":e.preventDefault();var D=A(t,v,a);return void(null!=D&&l({type:Y,id:D,lastInteractedWith:D}));case"ArrowUp":e.preventDefault();var N=x(t,v,a);return void(null!=N&&l({type:Y,id:N,lastInteractedWith:N}));case"ArrowLeft":if(e.preventDefault(),m(t,v)&&a.has(d))if(i){var U=[v].concat(f(w(t,v,new Set)));l({type:T,ids:U,lastInteractedWith:h.id})}else l({type:F,id:v,lastInteractedWith:v});else if(!t[0].children.includes(v)){var j=g(t,v);l({type:Y,id:j,lastInteractedWith:j})}return;case"ArrowRight":return e.preventDefault(),void(m(t,v)&&(a.has(d)?l({type:Y,id:h.children[0],lastInteractedWith:h.children[0]}):l({type:K,id:v,lastInteractedWith:v})));case"Home":e.preventDefault(),l({type:Y,id:t[0].children[0],lastInteractedWith:t[0].children[0]});break;case"End":e.preventDefault();var P=W(t,t[0].id,a);return void l({type:Y,id:P,lastInteractedWith:P});case"*":e.preventDefault();var _=t[g(t,v)].children.filter((function(e){return m(t,e)}));return void l({type:R,ids:_,lastInteractedWith:v});case"Enter":case" ":case"Spacebar":return e.preventDefault(),l({type:p?G:B,id:v,multiSelect:c,lastInteractedWith:v}),s&&!r.has(h.id)&&l({type:M,ids:k(t,[v],r),select:!p||!n.has(v),multiSelect:c,lastInteractedWith:v}),void(u&&l({type:z,id:v,lastInteractedWith:v}));default:if(1===e.key.length)for(var H=A(t,v,a);H!==v;)if(null!=H){if(t[H].name[0].toLowerCase()===e.key.toLowerCase())return void l({type:Y,id:H,lastInteractedWith:v});H=A(t,H,a)}else H=t[0].children[0];return}}}};te.propTypes={data:d.array.isRequired,onSelect:d.func,onExpand:d.func,className:d.string,nodeRenderer:d.func.isRequired,defaultExpandedIds:d.array,defaultSelectedIds:d.array,defaultDisabledIds:d.array,propagateCollapse:d.bool,propagateSelect:d.bool,propagateSelectUpwards:d.bool,multiSelect:d.bool,expandOnKeyboardSelect:d.bool,togglableSelect:d.bool,clickAction:d.oneOf(Object.values(Z))};export default te;export{C 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.3", | ||
"version": "2.1.4", | ||
"author": "lissitz (https://github.com/lissitz)", | ||
@@ -11,3 +11,4 @@ "main": "dist/react-accessible-treeview.cjs.js", | ||
"prop-types": "^15.7.2", | ||
"react": "^16.8.0" | ||
"react": "^16.8.0 || ^17.0.0", | ||
"react-dom": "^16.8.0 || ^17.0.0" | ||
}, | ||
@@ -23,4 +24,8 @@ "devDependencies": { | ||
"@storybook/react": "^5.1.11", | ||
"@testing-library/dom": "^6.1.0", | ||
"@testing-library/jest-dom": "^4.1.0", | ||
"@testing-library/react": "^9.1.3", | ||
"babel-jest": "^24.9.0", | ||
"babel-loader": "^8.0.6", | ||
"babel-preset-react-app": "^10.0.1", | ||
"classnames": "^2.2.6", | ||
@@ -33,8 +38,9 @@ "eslint": "^6.1.0", | ||
"eslint-plugin-react": "^7.14.3", | ||
"eslint-plugin-react-hooks": "^1.5.0", | ||
"eslint-plugin-react-hooks": "^2.0.0", | ||
"jest": "^24.9.0", | ||
"lint-staged": "^9.2.2", | ||
"prettier": "^1.18.2", | ||
"react": "^16.8.0", | ||
"react": "^17.0.0", | ||
"react-icons": "^3.7.0", | ||
"react-test-renderer": "^17.0.0", | ||
"rollup": "^1.16.7", | ||
@@ -51,3 +57,4 @@ "rollup-plugin-babel": "^4.3.3", | ||
"build-storybook": "build-storybook", | ||
"prepare": "npm run build" | ||
"prepare": "npm run build", | ||
"test": "jest" | ||
}, | ||
@@ -84,4 +91,3 @@ "files": [ | ||
"tree" | ||
], | ||
"dependencies": {} | ||
] | ||
} |
@@ -29,3 +29,3 @@ # react-accessible-treeview [![Build Status](https://travis-ci.org/lissitz/react-accessible-treeview.svg?branch=master)](https://travis-ci.org/lissitz/react-accessible-treeview) [![Greenkeeper badge](https://badges.greenkeeper.io/lissitz/react-accessible-treeview.svg)](https://greenkeeper.io/) [![npm version](https://badge.fury.io/js/react-accessible-treeview.svg)](https://badge.fury.io/js/react-accessible-treeview) [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT) | ||
| `expandOnKeyboardSelect` | `bool` | `false` | Selecting a node with a keyboard (using Space or Enter) will also toggle its expanded state | | ||
| `togglableSelect` | `bool` | `false` | Wether the selected state is togglable | | ||
| `togglableSelect` | `bool` | `false` | Whether the selected state is togglable | | ||
| `defaultSelectedIds` | `array` | `[]` | Array with the ids of the default selected nodes | | ||
@@ -43,8 +43,8 @@ | `defaultExpandedIds` | `array` | `[]` | Array with the ids of the default expanded nodes | | ||
| Property | Type | Default | Description | | ||
| ---------- | ----------- | -------- | -------------------------------------------------------- | | ||
| `id` | `number` | required | A nonnegative interger that uniquely identifies the node | | ||
| `name` | `string` | required | Used to match on key press | | ||
| `children` | `array[id]` | required | An array with the ids of the children nodes. | | ||
| `parent` | `id` | required | The parent of the node. `null` for the root node | | ||
| Property | Type | Default | Description | | ||
| ---------- | ----------- | -------- | ------------------------------------------------------- | | ||
| `id` | `number` | required | A nonnegative integer that uniquely identifies the node | | ||
| `name` | `string` | required | Used to match on key press | | ||
| `children` | `array[id]` | required | An array with the ids of the children nodes. | | ||
| `parent` | `id` | required | The parent of the node. `null` for the root node | | ||
@@ -68,3 +68,3 @@ The first item of the array represents the root node and won't be displayed. | ||
{ name: "package.json", id: 10, parent: 0 }, | ||
{ name: "webpack.config.js", id: 11, parent: 0 } | ||
{ name: "webpack.config.js", id: 11, parent: 0 }, | ||
]; | ||
@@ -89,3 +89,3 @@ ``` | ||
| `isExpanded` | `bool` or `undefined` | If the node is a branch node, whether it is expanded, else undefined | | ||
| `isDisabled` | `bool` | Wheter the rendered node is disabled | | ||
| `isDisabled` | `bool` | Whether the rendered node is disabled | | ||
| `level` | `number` | A positive integer that corresponds to the aria-level attribute | | ||
@@ -161,4 +161,4 @@ | `setsize` | `number` | A positive integer that corresponds to the aria-setsize attribute | | ||
| selectedIds | `Set` | `new Set(defaultSelectedIds)` | Set of the ids of the selected nodes | | ||
| tabbableId | `number` | `data[0].children[0]` | if of the node with tabindex = 0 | | ||
| isFocused | `bool` | `false` | wether the tree has focus | | ||
| tabbableId | `number` | `data[0].children[0]` | Id of the node with tabindex = 0 | | ||
| isFocused | `bool` | `false` | Whether the tree has focus | | ||
| expandedIds | `Set` | `new Set(defaultExpandedIds)` | Set of the ids of the expanded nodes | | ||
@@ -165,0 +165,0 @@ | halfSelectedIds | `Set` | `new Set()` | Set of the ids of the selected nodes | |
@@ -0,0 +0,0 @@ import TreeView from "./TreeView"; |
@@ -20,3 +20,3 @@ import React, { useReducer, useRef, useEffect } from "react"; | ||
propagatedIds, | ||
onComponentBlur | ||
onComponentBlur, | ||
} from "./utils"; | ||
@@ -31,3 +31,3 @@ | ||
leaf: "tree-node__leaf", | ||
nodeGroup: "tree-node-group" | ||
nodeGroup: "tree-node-group", | ||
}; | ||
@@ -51,3 +51,3 @@ | ||
disable: "DISABLE", | ||
enable: "ENABLE" | ||
enable: "ENABLE", | ||
}; | ||
@@ -66,3 +66,3 @@ | ||
lastAction: action.type, | ||
lastInteractedWith: action.lastInteractedWith | ||
lastInteractedWith: action.lastInteractedWith, | ||
}; | ||
@@ -79,3 +79,3 @@ } | ||
lastAction: action.type, | ||
lastInteractedWith: action.lastInteractedWith | ||
lastInteractedWith: action.lastInteractedWith, | ||
}; | ||
@@ -92,3 +92,3 @@ } | ||
lastAction: action.type, | ||
lastInteractedWith: action.lastInteractedWith | ||
lastInteractedWith: action.lastInteractedWith, | ||
}; | ||
@@ -102,3 +102,3 @@ } | ||
lastAction: action.type, | ||
lastInteractedWith: action.lastInteractedWith | ||
lastInteractedWith: action.lastInteractedWith, | ||
}; | ||
@@ -116,3 +116,3 @@ } | ||
lastAction: action.type, | ||
lastInteractedWith: action.lastInteractedWith | ||
lastInteractedWith: action.lastInteractedWith, | ||
}; | ||
@@ -131,3 +131,3 @@ } | ||
lastAction: action.type, | ||
lastInteractedWith: action.lastInteractedWith | ||
lastInteractedWith: action.lastInteractedWith, | ||
}; | ||
@@ -156,3 +156,3 @@ } | ||
lastAction: action.type, | ||
lastInteractedWith: action.lastInteractedWith | ||
lastInteractedWith: action.lastInteractedWith, | ||
}; | ||
@@ -179,3 +179,3 @@ } | ||
lastAction: action.type, | ||
lastInteractedWith: action.lastInteractedWith | ||
lastInteractedWith: action.lastInteractedWith, | ||
}; | ||
@@ -206,3 +206,3 @@ } | ||
lastAction: action.type, | ||
lastInteractedWith: action.lastInteractedWith | ||
lastInteractedWith: action.lastInteractedWith, | ||
}; | ||
@@ -212,3 +212,3 @@ } | ||
let selectedIds; | ||
const ids = action.ids.filter(id => !state.disabledIds.has(id)); | ||
const ids = action.ids.filter((id) => !state.disabledIds.has(id)); | ||
if (action.multiSelect) { | ||
@@ -226,3 +226,3 @@ if (action.select) { | ||
lastAction: action.type, | ||
lastInteractedWith: action.lastInteractedWith | ||
lastInteractedWith: action.lastInteractedWith, | ||
}; | ||
@@ -234,3 +234,3 @@ } | ||
let selectedIds; | ||
const ids = action.ids.filter(id => !state.disabledIds.has(id)); | ||
const ids = action.ids.filter((id) => !state.disabledIds.has(id)); | ||
if (action.multiSelect) { | ||
@@ -248,3 +248,3 @@ if (action.select) { | ||
lastAction: action.type, | ||
lastInteractedWith: action.lastInteractedWith | ||
lastInteractedWith: action.lastInteractedWith, | ||
}; | ||
@@ -260,3 +260,3 @@ } | ||
lastAction: action.type, | ||
lastInteractedWith: action.lastInteractedWith | ||
lastInteractedWith: action.lastInteractedWith, | ||
}; | ||
@@ -266,3 +266,3 @@ case treeTypes.blur: | ||
...state, | ||
isFocused: false | ||
isFocused: false, | ||
}; | ||
@@ -274,3 +274,3 @@ case treeTypes.disable: { | ||
...state, | ||
disabledIds | ||
disabledIds, | ||
}; | ||
@@ -283,3 +283,3 @@ } | ||
...state, | ||
disabledIds | ||
disabledIds, | ||
}; | ||
@@ -301,3 +301,3 @@ } | ||
multiSelect, | ||
propagateSelectUpwards | ||
propagateSelectUpwards, | ||
}) => { | ||
@@ -312,3 +312,3 @@ const [state, dispatch] = useReducer(treeReducer, { | ||
lastInteractedWith: null, | ||
disabledIds: new Set(defaultDisabledIds) | ||
disabledIds: new Set(defaultDisabledIds), | ||
}); | ||
@@ -323,3 +323,3 @@ | ||
lastAction, | ||
lastInteractedWith | ||
lastInteractedWith, | ||
} = state; | ||
@@ -340,3 +340,3 @@ const prevSelectedIds = usePrevious(selectedIds) || new Set(); | ||
isHalfSelected: isBranch ? halfSelectedIds.has(toggledId) : undefined, | ||
treeState: state | ||
treeState: state, | ||
}); | ||
@@ -353,3 +353,3 @@ } | ||
onSelect, | ||
state | ||
state, | ||
]); | ||
@@ -368,3 +368,3 @@ | ||
isHalfSelected: halfSelectedIds.has(id), | ||
treeState: state | ||
treeState: state, | ||
}); | ||
@@ -381,3 +381,3 @@ } | ||
onExpand, | ||
state | ||
state, | ||
]); | ||
@@ -406,3 +406,3 @@ | ||
NotUserAction: true, | ||
lastInteractedWith | ||
lastInteractedWith, | ||
}); | ||
@@ -419,3 +419,3 @@ } | ||
NotUserAction: true, | ||
lastInteractedWith | ||
lastInteractedWith, | ||
}); | ||
@@ -431,3 +431,3 @@ } | ||
NotUserAction: true, | ||
lastInteractedWith | ||
lastInteractedWith, | ||
}); | ||
@@ -447,3 +447,3 @@ } | ||
toggledIds, | ||
lastInteractedWith | ||
lastInteractedWith, | ||
]); | ||
@@ -466,3 +466,3 @@ | ||
focus: "FOCUS", | ||
exclusiveSelect: "EXCLUSIVE_SELECT" | ||
exclusiveSelect: "EXCLUSIVE_SELECT", | ||
}; | ||
@@ -504,3 +504,3 @@ | ||
propagateSelect, | ||
propagateSelectUpwards | ||
propagateSelectUpwards, | ||
}); | ||
@@ -518,3 +518,3 @@ propagateSelect = propagateSelect && multiSelect; | ||
ref={innerRef} | ||
onBlur={event => { | ||
onBlur={(event) => { | ||
onComponentBlur(event, innerRef.current, () => { | ||
@@ -524,3 +524,3 @@ onBlur && | ||
treeState: state, | ||
dispatch | ||
dispatch, | ||
}); | ||
@@ -542,3 +542,3 @@ dispatch({ type: treeTypes.blur }); | ||
expandOnKeyboardSelect, | ||
togglableSelect | ||
togglableSelect, | ||
})} | ||
@@ -550,19 +550,13 @@ {...other} | ||
key={x} | ||
selectedIds={state.selectedIds} | ||
tabbableId={state.tabbableId} | ||
isFocused={state.isFocused} | ||
expandedIds={state.expandedIds} | ||
disabledIds={state.disabledIds} | ||
halfSelectedIds={state.halfSelectedIds} | ||
lastUserSelect={state.lastUserSelect} | ||
lastInteractedWith={state.lastInteractedWith} | ||
dispatch={dispatch} | ||
data={data} | ||
element={data[x]} | ||
setsize={data[0].children.length} | ||
posinset={index + 1} | ||
level={1} | ||
{...state} | ||
state={state} | ||
dispatch={dispatch} | ||
nodeRefs={nodeRefs} | ||
baseClassNames={baseClassNames} | ||
nodeRenderer={nodeRenderer} | ||
setsize={data[0].children.length} | ||
posinset={index + 1} | ||
level={1} | ||
propagateCollapse={propagateCollapse} | ||
@@ -574,3 +568,2 @@ propagateSelect={propagateSelect} | ||
clickAction={clickAction} | ||
state={state} | ||
/> | ||
@@ -582,29 +575,29 @@ ))} | ||
const Node = ({ | ||
element, | ||
dispatch, | ||
data, | ||
selectedIds, | ||
tabbableId, | ||
isFocused, | ||
expandedIds, | ||
disabledIds, | ||
halfSelectedIds, | ||
lastUserSelect, | ||
nodeRefs, | ||
baseClassNames, | ||
nodeRenderer, | ||
setsize, | ||
posinset, | ||
level, | ||
propagateCollapse, | ||
propagateSelect, | ||
propagateSelectUpwards, | ||
multiSelect, | ||
togglableSelect, | ||
clickAction, | ||
lastInteractedWith, | ||
state | ||
}) => { | ||
const handleExpand = event => { | ||
const Node = (props) => { | ||
const { | ||
element, | ||
dispatch, | ||
data, | ||
selectedIds, | ||
tabbableId, | ||
isFocused, | ||
expandedIds, | ||
disabledIds, | ||
halfSelectedIds, | ||
lastUserSelect, | ||
nodeRefs, | ||
baseClassNames, | ||
nodeRenderer, | ||
setsize, | ||
posinset, | ||
level, | ||
propagateCollapse, | ||
propagateSelect, | ||
multiSelect, | ||
togglableSelect, | ||
clickAction, | ||
state, | ||
} = props; | ||
const handleExpand = (event) => { | ||
if (event.ctrlKey || event.altKey || event.shiftKey) return; | ||
@@ -616,3 +609,3 @@ if (expandedIds.has(element.id) && propagateCollapse) { | ||
ids, | ||
lastInteractedWith: element.id | ||
lastInteractedWith: element.id, | ||
}); | ||
@@ -623,3 +616,3 @@ } else { | ||
id: element.id, | ||
lastInteractedWith: element.id | ||
lastInteractedWith: element.id, | ||
}); | ||
@@ -629,11 +622,10 @@ } | ||
const handleFocus = () => { | ||
const handleFocus = () => | ||
dispatch({ | ||
type: treeTypes.focus, | ||
id: element.id, | ||
lastInteractedWith: element.id | ||
lastInteractedWith: element.id, | ||
}); | ||
}; | ||
const handleSelect = event => { | ||
const handleSelect = (event) => { | ||
if (event.shiftKey) { | ||
@@ -645,4 +637,4 @@ let ids = getAccessibleRange({ | ||
to: element.id, | ||
lastInteractedWith: element.id | ||
}).filter(id => !disabledIds.has(id)); | ||
lastInteractedWith: element.id, | ||
}).filter((id) => !disabledIds.has(id)); | ||
ids = propagateSelect ? propagatedIds(data, ids, disabledIds) : ids; | ||
@@ -654,3 +646,3 @@ dispatch({ | ||
ids, | ||
lastInteractedWith: element.id | ||
lastInteractedWith: element.id, | ||
}); | ||
@@ -663,3 +655,3 @@ } else if (event.ctrlKey || clickAction === clickActions.select) { | ||
multiSelect, | ||
lastInteractedWith: element.id | ||
lastInteractedWith: element.id, | ||
}); | ||
@@ -673,3 +665,3 @@ propagateSelect && | ||
multiSelect, | ||
lastInteractedWith: element.id | ||
lastInteractedWith: element.id, | ||
}); | ||
@@ -681,3 +673,3 @@ } else if (clickAction === clickActions.exclusiveSelect) { | ||
multiSelect: false, | ||
lastInteractedWith: element.id | ||
lastInteractedWith: element.id, | ||
}); | ||
@@ -688,3 +680,3 @@ } else if (clickAction === clickActions.focus) { | ||
id: element.id, | ||
lastInteractedWith: element.id | ||
lastInteractedWith: element.id, | ||
}); | ||
@@ -694,7 +686,7 @@ } | ||
const getClasses = className => { | ||
const getClasses = (className) => { | ||
return cx(className, { | ||
[`${className}--expanded`]: expandedIds.has(element.id), | ||
[`${className}--selected`]: selectedIds.has(element.id), | ||
[`${className}--focused`]: tabbableId === element.id && isFocused | ||
[`${className}--focused`]: tabbableId === element.id && isFocused, | ||
}); | ||
@@ -711,3 +703,3 @@ }; | ||
: composeHandlers(onClick, handleFocus), | ||
ref: x => (nodeRefs.current[element.id] = x), | ||
ref: (x) => (nodeRefs.current[element.id] = x), | ||
className: cx(getClasses(baseClassNames.node), baseClassNames.leaf), | ||
@@ -720,5 +712,6 @@ "aria-setsize": setsize, | ||
isDisabled: disabledIds.has(element.id), | ||
multiSelect | ||
multiSelect, | ||
}), | ||
disabled: disabledIds.has(element.id) | ||
disabled: disabledIds.has(element.id), | ||
"aria-disabled": disabledIds.has(element.id), | ||
}; | ||
@@ -733,3 +726,3 @@ }; | ||
: composeHandlers(onClick, handleFocus), | ||
className: cx(getClasses(baseClassNames.node), baseClassNames.branch) | ||
className: cx(getClasses(baseClassNames.node), baseClassNames.branch), | ||
}; | ||
@@ -745,3 +738,3 @@ }; | ||
isDisabled: disabledIds.has(element.id), | ||
multiSelect | ||
multiSelect, | ||
})} | ||
@@ -752,4 +745,5 @@ aria-setsize={setsize} | ||
disabled={disabledIds.has(element.id)} | ||
aria-disabled={disabledIds.has(element.id)} | ||
tabIndex={tabbableId === element.id ? 0 : -1} | ||
ref={x => (nodeRefs.current[element.id] = x)} | ||
ref={(x) => (nodeRefs.current[element.id] = x)} | ||
className={baseClassNames.branchWrapper} | ||
@@ -771,37 +765,5 @@ > | ||
handleExpand, | ||
treeState: state | ||
treeState: state, | ||
})} | ||
{expandedIds.has(element.id) && ( | ||
<ul role="group" className={getClasses(baseClassNames.nodeGroup)}> | ||
{element.children.map((x, index) => ( | ||
<Node | ||
key={x} | ||
selectedIds={selectedIds} | ||
tabbableId={tabbableId} | ||
isFocused={isFocused} | ||
expandedIds={expandedIds} | ||
halfSelectedIds={halfSelectedIds} | ||
disabledIds={disabledIds} | ||
lastUserSelect={lastUserSelect} | ||
dispatch={dispatch} | ||
data={data} | ||
element={data[x]} | ||
nodeRefs={nodeRefs} | ||
baseClassNames={baseClassNames} | ||
nodeRenderer={nodeRenderer} | ||
setsize={element.children.length} | ||
posinset={index + 1} | ||
level={level + 1} | ||
propagateCollapse={propagateCollapse} | ||
propagateSelect={propagateSelect} | ||
propagateSelectUpwards={propagateSelect} | ||
multiSelect={multiSelect} | ||
togglableSelect={togglableSelect} | ||
clickAction={clickAction} | ||
lastInteractedWith={lastInteractedWith} | ||
state={state} | ||
/> | ||
))} | ||
</ul> | ||
)} | ||
<NodeGroup element={element} getClasses={getClasses} {...props} /> | ||
</li> | ||
@@ -824,3 +786,3 @@ ) : ( | ||
handleExpand: noop, | ||
treeState: state | ||
treeState: state, | ||
})} | ||
@@ -831,2 +793,29 @@ </li> | ||
const NodeGroup = ({ | ||
data, | ||
element, | ||
expandedIds, | ||
getClasses, | ||
baseClassNames, | ||
level, | ||
...rest | ||
}) => ( | ||
<ul role="group" className={getClasses(baseClassNames.nodeGroup)}> | ||
{expandedIds.has(element.id) && | ||
element.children.map((x, index) => ( | ||
<Node | ||
data={data} | ||
expandedIds={expandedIds} | ||
baseClassNames={baseClassNames} | ||
key={x} | ||
element={data[x]} | ||
setsize={element.children.length} | ||
posinset={index + 1} | ||
level={level + 1} | ||
{...rest} | ||
/> | ||
))} | ||
</ul> | ||
); | ||
const handleKeyDown = ({ | ||
@@ -843,4 +832,4 @@ data, | ||
expandOnKeyboardSelect, | ||
togglableSelect | ||
}) => event => { | ||
togglableSelect, | ||
}) => (event) => { | ||
const element = data[tabbableId]; | ||
@@ -853,4 +842,4 @@ const id = element.id; | ||
const ids = Object.values(dataWithoutRoot) | ||
.map(x => x.id) | ||
.filter(id => !disabledIds.has(id)); | ||
.map((x) => x.id) | ||
.filter((id) => !disabledIds.has(id)); | ||
dispatch({ | ||
@@ -860,6 +849,6 @@ type: treeTypes.changeSelectMany, | ||
select: | ||
Array.from(selectedIds).filter(id => !disabledIds.has(id)).length !== | ||
ids.length, | ||
Array.from(selectedIds).filter((id) => !disabledIds.has(id)) | ||
.length !== ids.length, | ||
ids, | ||
lastInteractedWith: element.id | ||
lastInteractedWith: element.id, | ||
}); | ||
@@ -878,4 +867,4 @@ } else if ( | ||
from: id, | ||
to: newId | ||
}).filter(id => !disabledIds.has(id)); | ||
to: newId, | ||
}).filter((id) => !disabledIds.has(id)); | ||
dispatch({ | ||
@@ -885,3 +874,3 @@ type: treeTypes.changeSelectMany, | ||
select: true, | ||
ids: propagateSelect ? propagatedIds(data, range, disabledIds) : range | ||
ids: propagateSelect ? propagatedIds(data, range, disabledIds) : range, | ||
}); | ||
@@ -891,3 +880,3 @@ dispatch({ | ||
id: newId, | ||
lastInteractedWith: newId | ||
lastInteractedWith: newId, | ||
}); | ||
@@ -911,3 +900,3 @@ } | ||
multiSelect, | ||
lastInteractedWith: previous | ||
lastInteractedWith: previous, | ||
}); | ||
@@ -917,3 +906,3 @@ dispatch({ | ||
id: previous, | ||
lastInteractedWith: previous | ||
lastInteractedWith: previous, | ||
}); | ||
@@ -934,3 +923,3 @@ } | ||
select: true, | ||
lastInteractedWith: next | ||
lastInteractedWith: next, | ||
}); | ||
@@ -940,3 +929,3 @@ dispatch({ | ||
id: next, | ||
lastInteractedWith: next | ||
lastInteractedWith: next, | ||
}); | ||
@@ -958,3 +947,3 @@ } | ||
id: next, | ||
lastInteractedWith: next | ||
lastInteractedWith: next, | ||
}); | ||
@@ -971,3 +960,3 @@ } | ||
id: previous, | ||
lastInteractedWith: previous | ||
lastInteractedWith: previous, | ||
}); | ||
@@ -985,3 +974,3 @@ } | ||
ids, | ||
lastInteractedWith: element.id | ||
lastInteractedWith: element.id, | ||
}); | ||
@@ -992,3 +981,3 @@ } else { | ||
id, | ||
lastInteractedWith: id | ||
lastInteractedWith: id, | ||
}); | ||
@@ -1003,3 +992,3 @@ } | ||
id: parentId, | ||
lastInteractedWith: parentId | ||
lastInteractedWith: parentId, | ||
}); | ||
@@ -1017,3 +1006,3 @@ } | ||
id: element.children[0], | ||
lastInteractedWith: element.children[0] | ||
lastInteractedWith: element.children[0], | ||
}); | ||
@@ -1031,3 +1020,3 @@ } else { | ||
id: data[0].children[0], | ||
lastInteractedWith: data[0].children[0] | ||
lastInteractedWith: data[0].children[0], | ||
}); | ||
@@ -1041,3 +1030,3 @@ break; | ||
id: lastAccessible, | ||
lastInteractedWith: lastAccessible | ||
lastInteractedWith: lastAccessible, | ||
}); | ||
@@ -1048,3 +1037,3 @@ return; | ||
event.preventDefault(); | ||
const nodes = data[getParent(data, id)].children.filter(x => | ||
const nodes = data[getParent(data, id)].children.filter((x) => | ||
isBranchNode(data, x) | ||
@@ -1055,3 +1044,3 @@ ); | ||
ids: nodes, | ||
lastInteractedWith: id | ||
lastInteractedWith: id, | ||
}); | ||
@@ -1065,9 +1054,8 @@ return; | ||
event.preventDefault(); | ||
!disabledIds.has(id) && | ||
dispatch({ | ||
type: togglableSelect ? treeTypes.toggleSelect : treeTypes.select, | ||
id: element.id, | ||
multiSelect, | ||
lastInteractedWith: element.id | ||
}); | ||
dispatch({ | ||
type: togglableSelect ? treeTypes.toggleSelect : treeTypes.select, | ||
id: id, | ||
multiSelect, | ||
lastInteractedWith: id, | ||
}); | ||
propagateSelect && | ||
@@ -1077,6 +1065,6 @@ !disabledIds.has(element.id) && | ||
type: treeTypes.changeSelectMany, | ||
ids: propagatedIds(data, [element.id], disabledIds), | ||
select: togglableSelect ? !selectedIds.has(element.id) : true, | ||
ids: propagatedIds(data, [id], disabledIds), | ||
select: togglableSelect ? !selectedIds.has(id) : true, | ||
multiSelect, | ||
lastInteractedWith: element.id | ||
lastInteractedWith: id, | ||
}); | ||
@@ -1100,3 +1088,3 @@ expandOnKeyboardSelect && | ||
id: currentId, | ||
lastInteractedWith: id | ||
lastInteractedWith: id, | ||
}); | ||
@@ -1156,5 +1144,5 @@ return; | ||
/** action to perform on click */ | ||
clickAction: PropTypes.oneOf(Object.values(clickActions)) | ||
clickAction: PropTypes.oneOf(Object.values(clickActions)), | ||
}; | ||
export default TreeView; |
import { useRef, useEffect } from "react"; | ||
export const composeHandlers = (...handlers) => event => { | ||
export const composeHandlers = (...handlers) => (event) => { | ||
for (const handler of handlers) { | ||
@@ -25,3 +25,3 @@ handler && handler(event); | ||
export const usePrevious = x => { | ||
export const usePrevious = (x) => { | ||
const ref = useRef(); | ||
@@ -37,3 +37,3 @@ useEffect(() => { | ||
export const focusRef = ref => { | ||
export const focusRef = (ref) => { | ||
if (ref != null && ref.focus) { | ||
@@ -53,3 +53,3 @@ ref.focus(); | ||
if (node.children == null) return; | ||
for (const childId of node.children.filter(x => !disabledIds.has(x))) { | ||
for (const childId of node.children.filter((x) => !disabledIds.has(x))) { | ||
descendants.push(childId); | ||
@@ -126,10 +126,10 @@ getDescendantsHelper(data, childId); | ||
const enabledChildren = data[parent].children.filter( | ||
x => !disabledIds.has(x) | ||
(x) => !disabledIds.has(x) | ||
); | ||
if (enabledChildren.length === 0) break; | ||
const some = enabledChildren.some(x => selectedIds.has(x)); | ||
const some = enabledChildren.some((x) => selectedIds.has(x)); | ||
if (!some) { | ||
changes.none.add(parent); | ||
} else { | ||
if (enabledChildren.every(x => selectedIds.has(x))) { | ||
if (enabledChildren.every((x) => selectedIds.has(x))) { | ||
changes.every.add(parent); | ||
@@ -170,7 +170,7 @@ } else { | ||
export const flattenTree = function(tree) { | ||
export const flattenTree = function (tree) { | ||
let count = 0; | ||
const flattenedTree = []; | ||
const flattenTreeHelper = function(tree, parent) { | ||
const flattenTreeHelper = function (tree, parent) { | ||
tree.id = count; | ||
@@ -184,3 +184,3 @@ tree.parent = parent; | ||
} | ||
tree.children = tree.children.map(x => x.id); | ||
tree.children = tree.children.map((x) => x.id); | ||
}; | ||
@@ -201,4 +201,4 @@ | ||
...ids | ||
.filter(id => isBranchNode(data, id)) | ||
.map(id => getDescendants(data, id, disabledIds)) | ||
.filter((id) => isBranchNode(data, id)) | ||
.map((id) => getDescendants(data, id, disabledIds)) | ||
); | ||
@@ -205,0 +205,0 @@ |
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
95908
4
33
8
1374