@@ -4,12 +4,12 @@ import { useMemo, useState } from 'react';
4
4
import useCioClient from './useCioClient' ;
5
5
import useDownShift from './useDownShift' ;
6
6
import {
7
- CioAutocompleteProps ,
8
7
CioClientConfig ,
9
8
Section ,
10
9
UserDefinedSection ,
11
10
HTMLPropsWithCioDataAttributes ,
12
11
Item ,
12
+ UseCioAutocompleteOptions ,
13
13
} from '../types' ;
14
14
import usePrevious from './usePrevious' ;
15
15
import {
@@ -23,7 +23,8 @@ import {
23
23
import useConsoleErrors from './useConsoleErrors' ;
24
24
import useSections from './useSections' ;
25
25
import useRecommendationsObserver from './useRecommendationsObserver' ;
26
- import { isAutocompleteSection , isCustomSection , isRecommendationsSection } from '../typeGuards' ;
26
+ import { isCustomSection , isRecommendationsSection } from '../typeGuards' ;
27
+ import useNormalizedProps from './useNormalizedProps' ;
27
28
28
29
export const defaultSections : UserDefinedSection [ ] = [
29
30
{
@@ -36,65 +37,22 @@ export const defaultSections: UserDefinedSection[] = [
36
37
} ,
37
38
] ;
38
39
39
- export type UseCioAutocompleteOptions = Omit < CioAutocompleteProps , 'children' > ;
40
-
41
- const convertLegacyParametersAndAddDefaults = ( sections : UserDefinedSection [ ] ) =>
42
- sections . map ( ( config ) => {
43
- if ( isRecommendationsSection ( config ) ) {
44
- if ( config . identifier && ! config . podId ) {
45
- return { ...config , podId : config . identifier } ;
46
- }
47
-
48
- if ( ! config . indexSectionName ) {
49
- return { ...config , indexSectionName : 'Products' } ;
50
- }
51
- }
52
-
53
- if ( isAutocompleteSection ( config ) ) {
54
- if ( config . identifier && ! config . indexSectionName ) {
55
- return { ...config , indexSectionName : config . identifier } ;
56
- }
57
- }
58
-
59
- return config ;
60
- } ) ;
61
-
62
40
const useCioAutocomplete = ( options : UseCioAutocompleteOptions ) => {
41
+ const { sections, zeroStateSections, cioJsClientOptions, advancedParameters } =
42
+ useNormalizedProps ( options ) ;
63
43
const {
64
44
onSubmit,
65
45
onChange,
66
46
openOnFocus,
67
47
apiKey,
68
48
cioJsClient,
69
- cioJsClientOptions,
70
49
placeholder = 'What can we help you find today?' ,
71
50
autocompleteClassName = 'cio-autocomplete' ,
72
- advancedParameters,
73
51
defaultInput,
74
52
getSearchResultsUrl,
75
53
onIsOpenChange,
76
54
} = options ;
77
55
78
- let { sections = defaultSections , zeroStateSections } = options ;
79
-
80
- sections = useMemo ( ( ) => {
81
- if ( sections ) {
82
- return convertLegacyParametersAndAddDefaults ( sections ) ;
83
- }
84
-
85
- return sections ;
86
- // eslint-disable-next-line react-hooks/exhaustive-deps
87
- } , [ JSON . stringify ( sections ) ] ) ;
88
-
89
- zeroStateSections = useMemo ( ( ) => {
90
- if ( zeroStateSections ) {
91
- return convertLegacyParametersAndAddDefaults ( zeroStateSections ) ;
92
- }
93
-
94
- return zeroStateSections ;
95
- // eslint-disable-next-line react-hooks/exhaustive-deps
96
- } , [ JSON . stringify ( zeroStateSections ) ] ) ;
97
-
98
56
const [ query , setQuery ] = useState ( defaultInput || '' ) ;
99
57
const previousQuery = usePrevious ( query ) ;
100
58
const cioClient = useCioClient ( { apiKey, cioJsClient, cioJsClientOptions } as CioClientConfig ) ;
0 commit comments