1
- import React , { useState } from 'react' ;
1
+ import React , { useState } from 'react' ;
2
2
import './styles.scss' ;
3
3
import IconTooltip from '../IconTooltip' ;
4
- import { IconButton , TooltipProps } from '@mui/material' ;
4
+ import { IconButton , TooltipProps } from '@mui/material' ;
5
5
import Icon from '../Icon/Icon' ;
6
- import PillList , { PillListItem } from '../PillList' ;
6
+ import PillList , { PillListItem } from '../PillList' ;
7
7
8
8
export type MultiSelectProps < K , V > = {
9
9
className ?: string ;
@@ -22,6 +22,7 @@ export type MultiSelectProps<K, V> = {
22
22
selectedOptions : K [ ] ;
23
23
tooltipTitle ?: TooltipProps [ 'title' ] ;
24
24
disabled ?: boolean ;
25
+ onBlur ?: ( ) => void ;
25
26
} ;
26
27
27
28
export default function MultiSelect < K , V > ( props : MultiSelectProps < K , V > ) : JSX . Element {
@@ -50,20 +51,25 @@ export default function MultiSelect<K, V>(props: MultiSelectProps<K, V>): JSX.El
50
51
setOpenedOptions ( ( isOpen ) => ! isOpen && ! disabled ) ;
51
52
} ;
52
53
53
- const closeOptions = ( ) => {
54
+ const onBlurHandler = ( ) => {
54
55
setOpenedOptions ( false ) ;
56
+ if ( props . onBlur ) {
57
+ props . onBlur ( ) ;
58
+ }
55
59
} ;
56
60
57
61
const unselectedOptions = Array . from < K > ( options . keys ( ) ) . filter ( ( key : K ) => ! selectedOptions . includes ( key ) ) ;
58
62
59
- const valuesPillData = selectedOptions . map ( ( item ) => {
60
- const value = options . get ( item ) ;
63
+ const valuesPillData = selectedOptions
64
+ . map ( ( item ) => {
65
+ const value = options . get ( item ) ;
61
66
62
- return {
63
- id : item ,
64
- value : value ? valueRenderer ( value ) : ( missingValuePlaceholder || '' ) ,
65
- } ;
66
- } ) . filter ( ( data ) => data . value ) as PillListItem < K > [ ] ;
67
+ return {
68
+ id : item ,
69
+ value : value ? valueRenderer ( value ) : missingValuePlaceholder || '' ,
70
+ } ;
71
+ } )
72
+ . filter ( ( data ) => data . value ) as PillListItem < K > [ ] ;
67
73
68
74
return (
69
75
< div className = { `multi-select ${ className } ` } >
@@ -72,26 +78,15 @@ export default function MultiSelect<K, V>(props: MultiSelectProps<K, V>): JSX.El
72
78
{ label } { tooltipTitle && < IconTooltip title = { tooltipTitle } /> }
73
79
</ label >
74
80
) }
75
- < div
76
- className = { `multi-select__container ${ fullWidth ? 'multi-select__container--fullWidth' : '' } ` }
77
- onBlur = { closeOptions }
78
- tabIndex = { 0 }
79
- >
81
+ < div className = { `multi-select__container ${ fullWidth ? 'multi-select__container--fullWidth' : '' } ` } onBlur = { onBlurHandler } tabIndex = { 0 } >
80
82
< div id = { id } className = { `multi-select__values${ disabled ? '--disabled' : '' } ` } onClick = { toggleOptions } >
81
- { selectedOptions . length > 0
82
- ? ( < PillList
83
- data = { valuesPillData }
84
- onRemove = { onRemove }
85
- onClick = { onPillClick }
86
- className = { pillListClassName }
87
- /> )
88
- : ( < p className = 'multi-select__placeholder-text' > { placeHolder } </ p > )
89
- }
83
+ { selectedOptions . length > 0 ? (
84
+ < PillList data = { valuesPillData } onRemove = { onRemove } onClick = { onPillClick } className = { pillListClassName } />
85
+ ) : (
86
+ < p className = 'multi-select__placeholder-text' > { placeHolder } </ p >
87
+ ) }
90
88
< div className = { 'multi-select__values__icon-button' } aria-label = 'show-options' >
91
- < Icon
92
- name = { openedOptions ? 'chevronUp' : 'chevronDown' }
93
- className = { `multi-select__values__icon-right${ disabled ? '--disabled' : '' } ` }
94
- />
89
+ < Icon name = { openedOptions ? 'chevronUp' : 'chevronDown' } className = { `multi-select__values__icon-right${ disabled ? '--disabled' : '' } ` } />
95
90
</ div >
96
91
</ div >
97
92
{ options && unselectedOptions . length > 0 && openedOptions && (
@@ -101,9 +96,9 @@ export default function MultiSelect<K, V>(props: MultiSelectProps<K, V>): JSX.El
101
96
const optionValue = options . get ( optionKey ) ;
102
97
103
98
return (
104
- < li key = { index } className = 'select-value' onClick = { ( ) => onAdd ( optionKey ) } > {
105
- optionValue ? valueRenderer ( optionValue ) : ( missingValuePlaceholder || '' )
106
- } </ li >
99
+ < li key = { index } className = 'select-value' onClick = { ( ) => onAdd ( optionKey ) } >
100
+ { optionValue ? valueRenderer ( optionValue ) : missingValuePlaceholder || '' }
101
+ </ li >
107
102
) ;
108
103
} ) }
109
104
</ ul >
0 commit comments