11import { Component , ReactNode , createElement } from "react" ;
22import { observer } from "mobx-react" ;
33import classNames from "classnames" ;
4+ import { FaExpandAlt , FaCompressAlt } from "react-icons/fa" ;
45import Table , { TableRowSelection , ColumnProps } from "antd/es/table" ;
56
67// Importing seperate so we don't pollute the CSS too much
@@ -38,6 +39,7 @@ export interface TreeTableProps {
3839 onSelect ?: ( ids : string [ ] ) => void ;
3940 buttonBar ?: ReactNode ;
4041 hideSelectBoxes ?: boolean ;
42+ renderExpandButton ?: boolean ;
4143}
4244
4345const DEBOUNCE = 250 ;
@@ -54,8 +56,8 @@ export class TreeTable extends Component<TreeTableProps> {
5456 this . onRowDblClick = this . onRowDblClick . bind ( this ) ;
5557 this . onExpand = this . onExpand . bind ( this ) ;
5658 this . setSelected = this . setSelected . bind ( this ) ;
57- // this.expandAll = this.expandAll.bind(this);
58- // this.collapseAll = this.collapseAll.bind(this);
59+ this . expandAll = this . expandAll . bind ( this ) ;
60+ this . collapseAll = this . collapseAll . bind ( this ) ;
5961 this . onSelectionChange = this . onSelectionChange . bind ( this ) ;
6062 this . rowClassName = this . rowClassName . bind ( this ) ;
6163 }
@@ -158,11 +160,20 @@ export class TreeTable extends Component<TreeTableProps> {
158160 }
159161
160162 columns = columns . map ( ( col , index ) => {
161- if ( ! col . key ) {
162- return col ;
163+ const newCol = { ...col } ;
164+ if ( ! newCol . key ) {
165+ return newCol ;
166+ }
167+ if ( index === 0 && this . props . renderExpandButton ) {
168+ newCol . title = (
169+ < div >
170+ { this . renderExpandButton ( ) }
171+ { col . title }
172+ </ div >
173+ ) ;
163174 }
164175 return {
165- ...col ,
176+ ...newCol ,
166177 render : ( text , record ) => {
167178 if ( index === 0 && record . _icon ) {
168179 return (
@@ -205,6 +216,23 @@ export class TreeTable extends Component<TreeTableProps> {
205216 ) ;
206217 }
207218
219+ private renderExpandButton ( ) : ReactNode {
220+ const expanded = this . props . store . expandedKeys . length > 0 ;
221+ const className = classNames ( "treetable-expand-button" ) ;
222+ if ( expanded ) {
223+ return (
224+ < div role = "button" className = { className } onClick = { this . collapseAll } >
225+ < FaCompressAlt />
226+ </ div >
227+ ) ;
228+ }
229+ return (
230+ < div role = "button" className = { className } onClick = { this . expandAll } >
231+ < FaExpandAlt />
232+ </ div >
233+ ) ;
234+ }
235+
208236 private setSelected ( keys : string [ ] ) : void {
209237 this . onSelectionChange ( keys ) ;
210238 }
@@ -215,18 +243,18 @@ export class TreeTable extends Component<TreeTableProps> {
215243 return `treetable-treelevel-${ index } ${ className } ` ;
216244 }
217245
218- // private expandAll(): void {
219- // const parentIds = this.props.rows
220- // .filter(row => typeof row._parent !== "undefined" && row._parent)
221- // .map(row => row._parent) as string[];
222- // this.setState({
223- // expandedRowKeys: uniq(parentIds)
224- // });
225- // }
246+ private expandAll ( ) : void {
247+ const parentIds = this . props . store . rowObjects
248+ . filter ( row => typeof row . _parent !== "undefined" && row . _parent )
249+ . map ( row => row . _parent ) as string [ ] ;
250+ const filtered = parentIds . filter ( ( item , i , ar ) => ar . indexOf ( item ) === i ) ;
226251
227- // private collapseAll(): void {
228- // this.props.store.setExpanded([]);
229- // }
252+ this . props . store . setExpanded ( filtered ) ;
253+ }
254+
255+ private collapseAll ( ) : void {
256+ this . props . store . setExpanded ( [ ] ) ;
257+ }
230258
231259 private onRowClick ( record : TableRecord ) : void {
232260 if ( this . props . onClick ) {
0 commit comments