import { cellFromEvent } from 'azure-devops-ui/List' ; import { IBehavior } from 'azure-devops-ui/Utilities/Behavior' ; import { IEventDispatch } from 'azure-devops-ui/Utilities/Dispatch' ; import { ITreeProps , ITree , Tree } from 'azure-devops-ui/TreeEx' ; import { KeyCode } from 'azure-devops-ui/Util' ; import { sortDelegate , SortOrder } from 'azure-devops-ui/Table' ; export class TreeColumnSorting < T > implements IBehavior < ITreeProps < T > , ITree < T > > { private onSort : sortDelegate ; private props : Readonly < ITreeProps < T > > ; constructor ( onSort ) { this . onSort = onSort ; } public initialize = ( props , table , eventDispatch ) : void => { this . props = props ; eventDispatch . addEventListener ( "click" , this . onClick ) ; eventDispatch . addEventListener ( "keydown" , this . onKeyDown ) ; } ; private onClick = ( event ) => { if ( ! event . defaultPrevented ) { this . processSortEvent ( event ) ; } } ; private onKeyDown = ( event ) => { if ( ! event . defaultPrevented ) { if ( event . which === KeyCode . enter || event . which === KeyCode . space ) { this . processSortEvent ( event ) ; } } } ; private processSortEvent ( event ) { const clickedCell = cellFromEvent ( event ) ; if ( clickedCell . rowIndex === - 1 ) { const column = this . props . columns [ clickedCell . cellIndex ] ; if ( column && column . sortProps ) { this . onSort ( clickedCell . cellIndex , column . sortProps . sortOrder === SortOrder . ascending ? SortOrder . descending : SortOrder . ascending , event ) ; event . preventDefault ( ) ; } } } }