@@ -213,6 +213,9 @@ export function getClosestMatchingOption(
213213 selectedOption = - 1 ,
214214 discriminatorField ?: string
215215) : number {
216+ if ( options . length === 0 ) {
217+ return selectedOption ;
218+ }
216219 // First resolve any refs in the options
217220 const resolvedOptions = options . map ( ( option ) => {
218221 return resolveAllReferences ( option , rootSchema ) ;
@@ -249,7 +252,7 @@ export function getClosestMatchingOption(
249252 if ( allValidIndexes . length === 1 ) {
250253 return allValidIndexes [ 0 ] ! ;
251254 }
252- if ( ! allValidIndexes . length ) {
255+ if ( allValidIndexes . length === 0 ) {
253256 // No indexes were valid, so we'll score all the options, add all the indexes
254257 for ( let i = 0 ; i < resolvedOptions . length ; i ++ ) {
255258 allValidIndexes . push ( i ) ;
@@ -259,44 +262,22 @@ export function getClosestMatchingOption(
259262 let bestScore = 0 ;
260263 let bestIndex = selectedOption ;
261264 // Score all the options in the list of valid indexes and return the index with the best score
262- // const { bestIndex }: BestType = allValidIndexes.reduce(
263- // (scoreData: BestType, index: number) => {
264- // const { bestScore } = scoreData;
265- // const option = resolvedOptions[index];
266- // const score = calculateIndexScore(
267- // validator,
268- // rootSchema,
269- // option,
270- // formData
271- // );
272- // scoreCount.add(score);
273- // if (score > bestScore) {
274- // return { bestIndex: index, bestScore: score };
275- // }
276- // return scoreData;
277- // },
278- // { bestIndex: selectedOption, bestScore: 0 }
279- // );
280265 for ( let i = 0 ; i < allValidIndexes . length ; i ++ ) {
281266 const index = allValidIndexes [ i ] ! ;
282- if ( index === selectedOption ) {
283- continue ;
284- }
285267 const option = resolvedOptions [ index ] ;
286- const score = calculateIndexScore (
287- validator ,
288- rootSchema ,
289- option ,
290- formData ,
291- )
268+ const score = calculateIndexScore ( validator , rootSchema , option , formData ) ;
292269 scoreCount . add ( score ) ;
293270 if ( score > bestScore ) {
294271 bestScore = score ;
295272 bestIndex = index ;
296273 }
297274 }
298275 // if all scores are the same go with selectedOption
299- if ( scoreCount . size === 1 && selectedOption >= 0 ) {
276+ if (
277+ allValidIndexes . length > 1 &&
278+ scoreCount . size === 1 &&
279+ selectedOption >= 0
280+ ) {
300281 return selectedOption ;
301282 }
302283
0 commit comments