@@ -46,6 +46,13 @@ interface ToolbarFilterState {
4646 isMounted : boolean ;
4747}
4848
49+ const getCategoryKey = ( categoryName : string | ToolbarLabelGroup ) : string => {
50+ if ( typeof categoryName === 'string' ) {
51+ return categoryName ;
52+ }
53+ return categoryName . key ;
54+ } ;
55+
4956class ToolbarFilter extends Component < ToolbarFilterProps , ToolbarFilterState > {
5057 static displayName = 'ToolbarFilter' ;
5158 static contextType = ToolbarContext ;
@@ -62,24 +69,17 @@ class ToolbarFilter extends Component<ToolbarFilterProps, ToolbarFilterState> {
6269 } ;
6370 }
6471
65- getCategoryKey = ( ) => {
66- const { categoryName } = this . props ;
67- return typeof categoryName !== 'string' && categoryName . hasOwnProperty ( 'key' )
68- ? categoryName . key
69- : categoryName . toString ( ) ;
70- } ;
71-
7272 componentDidMount ( ) {
73- this . context . updateNumberFilters ( this . getCategoryKey ( ) , this . props . labels . length ) ;
73+ this . context . updateNumberFilters ( getCategoryKey ( this . props . categoryName ) , this . props . labels . length ) ;
7474 this . setState ( { isMounted : true } ) ;
7575 }
7676
7777 componentDidUpdate ( ) {
78- this . context . updateNumberFilters ( this . getCategoryKey ( ) , this . props . labels . length ) ;
78+ this . context . updateNumberFilters ( getCategoryKey ( this . props . categoryName ) , this . props . labels . length ) ;
7979 }
8080
8181 componentWillUnmount ( ) {
82- this . context . updateNumberFilters ( this . getCategoryKey ( ) , 0 ) ;
82+ this . context . updateNumberFilters ( getCategoryKey ( this . props . categoryName ) , 0 ) ;
8383 }
8484
8585 render ( ) {
@@ -98,7 +98,7 @@ class ToolbarFilter extends Component<ToolbarFilterProps, ToolbarFilterState> {
9898 } = this . props ;
9999 const { isExpanded : managedIsExpanded , labelGroupContentRef } = this . context ;
100100 const _isExpanded = isExpanded !== undefined ? isExpanded : managedIsExpanded ;
101- const categoryKey = this . getCategoryKey ( ) ;
101+ const categoryKey = getCategoryKey ( categoryName ) ;
102102
103103 const labelGroup = labels . length ? (
104104 < ToolbarItem variant = "label-group" >
@@ -112,11 +112,11 @@ class ToolbarFilter extends Component<ToolbarFilterProps, ToolbarFilterState> {
112112 >
113113 { labels . map ( ( label ) =>
114114 typeof label === 'string' ? (
115- < Label variant = "outline" key = { label } onClose = { ( ) => deleteLabel ( categoryKey , label ) } >
115+ < Label variant = "outline" key = { label } onClose = { ( ) => deleteLabel ?. ( categoryKey , label ) } >
116116 { label }
117117 </ Label >
118118 ) : (
119- < Label key = { label . key } onClose = { ( ) => deleteLabel ( categoryKey , label ) } >
119+ < Label key = { label . key } onClose = { ( ) => deleteLabel ?. ( categoryKey , label ) } >
120120 { label . node }
121121 </ Label >
122122 )
@@ -129,7 +129,7 @@ class ToolbarFilter extends Component<ToolbarFilterProps, ToolbarFilterState> {
129129 return (
130130 < Fragment >
131131 { showToolbarItem && < ToolbarItem { ...props } > { children } </ ToolbarItem > }
132- { labelGroupContentRef ?. current ?. firstElementChild !== null &&
132+ { labelGroupContentRef ?. current ?. firstElementChild &&
133133 ReactDOM . createPortal ( labelGroup , labelGroupContentRef . current . firstElementChild ) }
134134 </ Fragment >
135135 ) ;
0 commit comments