@@ -4,12 +4,12 @@ import { useMemo, useState } from 'react';
44import useCioClient from './useCioClient' ;
55import useDownShift from './useDownShift' ;
66import {
7- CioAutocompleteProps ,
87 CioClientConfig ,
98 Section ,
109 UserDefinedSection ,
1110 HTMLPropsWithCioDataAttributes ,
1211 Item ,
12+ UseCioAutocompleteOptions ,
1313} from '../types' ;
1414import usePrevious from './usePrevious' ;
1515import {
@@ -23,7 +23,8 @@ import {
2323import useConsoleErrors from './useConsoleErrors' ;
2424import useSections from './useSections' ;
2525import useRecommendationsObserver from './useRecommendationsObserver' ;
26- import { isAutocompleteSection , isCustomSection , isRecommendationsSection } from '../typeGuards' ;
26+ import { isCustomSection , isRecommendationsSection } from '../typeGuards' ;
27+ import useNormalizedProps from './useNormalizedProps' ;
2728
2829export const defaultSections : UserDefinedSection [ ] = [
2930 {
@@ -36,65 +37,21 @@ export const defaultSections: UserDefinedSection[] = [
3637 } ,
3738] ;
3839
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-
6240const useCioAutocomplete = ( options : UseCioAutocompleteOptions ) => {
63- // eslint-disable-next-line react-hooks/exhaustive-deps
64- const memoizedOptions = useMemo ( ( ) => options , [ JSON . stringify ( options ) ] ) ;
65-
41+ const { sections, zeroStateSections, cioJsClientOptions, advancedParameters } =
42+ useNormalizedProps ( options ) ;
6643 const {
6744 onSubmit,
6845 onChange,
6946 openOnFocus,
7047 apiKey,
7148 cioJsClient,
72- cioJsClientOptions,
7349 placeholder = 'What can we help you find today?' ,
7450 autocompleteClassName = 'cio-autocomplete' ,
75- advancedParameters,
7651 defaultInput,
7752 getSearchResultsUrl,
7853 ...rest
79- } = memoizedOptions ;
80-
81- let { sections = defaultSections , zeroStateSections } = memoizedOptions ;
82-
83- sections = useMemo ( ( ) => {
84- if ( sections ) {
85- return convertLegacyParametersAndAddDefaults ( sections ) ;
86- }
87-
88- return sections ;
89- } , [ sections ] ) ;
90-
91- zeroStateSections = useMemo ( ( ) => {
92- if ( zeroStateSections ) {
93- return convertLegacyParametersAndAddDefaults ( zeroStateSections ) ;
94- }
95-
96- return zeroStateSections ;
97- } , [ zeroStateSections ] ) ;
54+ } = options ;
9855
9956 const [ query , setQuery ] = useState ( defaultInput || '' ) ;
10057 const previousQuery = usePrevious ( query ) ;
0 commit comments