@@ -2,6 +2,7 @@ import { LayoutAnimation } from 'react-native';
22
33import { ANIMATION_DURATION } from '../constants' ;
44import { ERRORS , getReducedSectionData , isValidDefaultOption , searchNormalize } from '../helpers' ;
5+ import { getDefaultSelectionIndex } from '../helpers/get-default-selection-index' ;
56import type { OptionsType , OptionType } from '../types' ;
67import { isSectionOptionsType } from '../types' ;
78
@@ -88,25 +89,29 @@ export const reducer = <T>(state: State<T>, action: ActionType<T>): State<T> =>
8889 }
8990} ;
9091
91- const setDefaultOption = < T > ( options : OptionsType < T > , defaultOption : OptionType < T > | undefined ) => {
92- if ( isValidDefaultOption ( defaultOption ) && options . length > 0 ) {
92+ const setDefaultOption = < T > (
93+ options : OptionsType < T > ,
94+ defaultOption : OptionType < T > | OptionType < T > [ ] | undefined ,
95+ ) => {
96+ if (
97+ ( Array . isArray ( defaultOption )
98+ ? defaultOption . every ( isValidDefaultOption )
99+ : isValidDefaultOption ( defaultOption ) ) &&
100+ options . length > 0 &&
101+ defaultOption !== undefined
102+ ) {
93103 const isSectionedOptions = isSectionOptionsType ( options ) ;
104+ const flatOptions = isSectionedOptions ? getReducedSectionData ( options ) : options ;
94105
95- const foundIndex = isSectionedOptions
96- ? getReducedSectionData ( options ) . findIndex ( ( item ) => item . value === defaultOption . value )
97- : options . findIndex ( ( item ) => item . value === defaultOption . value ) ;
98-
99- if ( foundIndex !== - 1 ) {
100- return {
101- selectedOption : defaultOption ,
102- selectedOptionIndex : foundIndex ,
103- } ;
104- }
106+ return {
107+ selectedOption : defaultOption ,
108+ selectedOptionIndex : getDefaultSelectionIndex ( flatOptions , defaultOption ) ,
109+ } ;
105110 }
106111
107112 return {
108113 selectedOption : null ,
109- selectedOptionIndex : - 1 ,
114+ selectedOptionIndex : Array . isArray ( defaultOption ) ? [ ] : - 1 ,
110115 } ;
111116} ;
112117
@@ -121,7 +126,8 @@ export const createInitialState = <T>({
121126 }
122127
123128 const { selectedOption, selectedOptionIndex } = setDefaultOption ( options , defaultOption ) ;
124- const defaultSearchValue = defaultOption ?. label ?? '' ;
129+ const defaultSearchOption = Array . isArray ( defaultOption ) ? defaultOption [ 0 ] : defaultOption ;
130+ const defaultSearchValue = defaultSearchOption ?. label ?? '' ;
125131
126132 return {
127133 isOpened : false ,
0 commit comments