@ -58,11 +58,30 @@ function getSelectedIndex(props) {
values
} = props ;
if ( Array . isArray ( value ) ) {
return values . findIndex ( ( v ) => {
return value . size && v . key === value [ 0 ] ;
} ) ;
}
return values . findIndex ( ( v ) => {
return v . key === value ;
} ) ;
}
function isSelectedItem ( index , props ) {
const {
value ,
values
} = props ;
if ( Array . isArray ( value ) ) {
return value . includes ( values [ index ] . key ) ;
}
return values [ index ] . key === value ;
}
function getKey ( selectedIndex , values ) {
return values [ selectedIndex ] . key ;
}
@ -92,7 +111,7 @@ class EnhancedSelectInput extends Component {
this . _scheduleUpdate ( ) ;
}
if ( prevProps . value !== this . props . value ) {
if ( ! Array . isArray ( this . props . value ) && prevProps . value !== this . props . value ) {
this . setState ( {
selectedIndex : getSelectedIndex ( this . props )
} ) ;
@ -134,7 +153,7 @@ class EnhancedSelectInput extends Component {
const button = document . getElementById ( this . _buttonId ) ;
const options = document . getElementById ( this . _optionsId ) ;
if ( ! button || this . state . isMobile ) {
if ( ! button || ! event . target . isConnected || this . state . isMobile ) {
return ;
}
@ -177,7 +196,7 @@ class EnhancedSelectInput extends Component {
}
if (
selectedIndex == null ||
selectedIndex == null || selectedIndex === - 1 ||
getSelectedOption ( selectedIndex , values ) . isDisabled
) {
if ( keyCode === keyCodes . UP _ARROW ) {
@ -235,12 +254,27 @@ class EnhancedSelectInput extends Component {
}
onSelect = ( value ) => {
this . setState ( { isOpen : false } ) ;
if ( Array . isArray ( this . props . value ) ) {
let newValue = null ;
const index = this . props . value . indexOf ( value ) ;
if ( index === - 1 ) {
newValue = this . props . values . map ( ( v ) => v . key ) . filter ( ( v ) => ( v === value ) || this . props . value . includes ( v ) ) ;
} else {
newValue = [ ... this . props . value ] ;
newValue . splice ( index , 1 ) ;
}
this . props . onChange ( {
name : this . props . name ,
value : newValue
} ) ;
} else {
this . setState ( { isOpen : false } ) ;
this . props . onChange ( {
name : this . props . name ,
value
} ) ;
this . props . onChange ( {
name : this . props . name ,
value
} ) ;
}
}
onMeasure = ( { width } ) => {
@ -258,6 +292,7 @@ class EnhancedSelectInput extends Component {
const {
className ,
disabledClassName ,
value ,
values ,
isDisabled ,
hasError ,
@ -275,6 +310,7 @@ class EnhancedSelectInput extends Component {
isMobile
} = this . state ;
const isMultiSelect = Array . isArray ( value ) ;
const selectedOption = getSelectedOption ( selectedIndex , values ) ;
return (
@ -303,9 +339,12 @@ class EnhancedSelectInput extends Component {
onPress = { this . onPress }
>
< SelectedValueComponent
value = { value }
values = { values }
{ ... selectedValueOptions }
{ ... selectedOption }
isDisabled = { isDisabled }
isMultiSelect = { isMultiSelect }
>
{ selectedOption ? selectedOption . value : null }
< / S e l e c t e d V a l u e C o m p o n e n t >
@ -359,11 +398,17 @@ class EnhancedSelectInput extends Component {
>
{
values . map ( ( v , index ) => {
const hasParent = v . parentKey !== undefined ;
const depth = hasParent ? 1 : 0 ;
const parentSelected = hasParent && value . includes ( v . parentKey ) ;
return (
< OptionComponent
key = { v . key }
id = { v . key }
isSelected = { index === selectedIndex }
depth = { depth }
isSelected = { isSelectedItem ( index , this . props ) }
isDisabled = { parentSelected }
isMultiSelect = { isMultiSelect }
{ ... valueOptions }
{ ... v }
isMobile = { false }
@ -401,11 +446,17 @@ class EnhancedSelectInput extends Component {
< Scroller className = { styles . optionsModalScroller } >
{
values . map ( ( v , index ) => {
const hasParent = v . parentKey !== undefined ;
const depth = hasParent ? 1 : 0 ;
const parentSelected = hasParent && value . includes ( v . parentKey ) ;
return (
< OptionComponent
key = { v . key }
id = { v . key }
isSelected = { index === selectedIndex }
depth = { depth }
isSelected = { isSelectedItem ( index , this . props ) }
isMultiSelect = { isMultiSelect }
isDisabled = { parentSelected }
{ ... valueOptions }
{ ... v }
isMobile = { true }
@ -429,9 +480,9 @@ EnhancedSelectInput.propTypes = {
className : PropTypes . string ,
disabledClassName : PropTypes . string ,
name : PropTypes . string . isRequired ,
value : PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number ]) . isRequired ,
value : PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number , PropTypes . arrayOf ( PropTypes . number ) ]) . isRequired ,
values : PropTypes . arrayOf ( PropTypes . object ) . isRequired ,
isDisabled : PropTypes . bool ,
isDisabled : PropTypes . bool .isRequired ,
hasError : PropTypes . bool ,
hasWarning : PropTypes . bool ,
valueOptions : PropTypes . object . isRequired ,