@@ -34,6 +34,7 @@ export const PhoneNumberInput = ({
3434 isRequired,
3535 error,
3636 addon,
37+ showFilterInput = false ,
3738 ...restProps
3839} : Props ) => {
3940 const [ isCallingCodeInputOpen , setIsCallingCodeInputOpen ] = useState ( false ) ;
@@ -46,6 +47,20 @@ export const PhoneNumberInput = ({
4647 [ 'isFeatured' , 'label' ] ,
4748 [ 'desc' , 'asc' ]
4849 ) ;
50+ const hasFeaturedOptions = callingCodeOptions . some ( item => item . isFeatured ) ;
51+ const hasOtherOptions = callingCodeOptions . some ( item => ! item . isFeatured ) ;
52+ const shouldShowGroupHeaders = hasFeaturedOptions && hasOtherOptions ;
53+ const getGroupHeaderLabel = ( item : CallingCodeOption , index : number ) => {
54+ if ( ! shouldShowGroupHeaders ) return '' ;
55+
56+ const previousItem = callingCodeOptions [ index - 1 ] ;
57+ const isGroupBoundary =
58+ item . isFeatured !== ( previousItem || { } ) . isFeatured ;
59+
60+ if ( ! isGroupBoundary ) return '' ;
61+
62+ return item . isFeatured ? featuredOptionsLabel : otherOptionsLabel ;
63+ } ;
4964
5065 const {
5166 getComboboxProps,
@@ -139,23 +154,25 @@ export const PhoneNumberInput = ({
139154 { ...getComboboxProps ( ) }
140155 data-testid = "calling-code-input"
141156 >
142- < S . CallingCodeFilterInputGroup >
143- < S . CallingCodeFilterInput
144- { ...getInputProps (
145- {
146- placeholder : callingCodeFilterInputPlaceholder ,
147- } ,
148- { ...refErrorFix }
157+ { showFilterInput && (
158+ < S . CallingCodeFilterInputGroup >
159+ < S . CallingCodeFilterInput
160+ { ...getInputProps (
161+ {
162+ placeholder : callingCodeFilterInputPlaceholder ,
163+ } ,
164+ { ...refErrorFix }
165+ ) }
166+ ref = { callingCodeFilterInputRef }
167+ data-testid = "calling-code-filter-input"
168+ onFocus = { onFocus }
169+ onBlur = { onBlur }
170+ />
171+ { isLoadingCallingCodeOptions && (
172+ < S . CallingCodeInputLoading data-testid = "calling-code-options-loading" />
149173 ) }
150- ref = { callingCodeFilterInputRef }
151- data-testid = "calling-code-filter-input"
152- onFocus = { onFocus }
153- onBlur = { onBlur }
154- />
155- { isLoadingCallingCodeOptions && (
156- < S . CallingCodeInputLoading data-testid = "calling-code-options-loading" />
157- ) }
158- </ S . CallingCodeFilterInputGroup >
174+ </ S . CallingCodeFilterInputGroup >
175+ ) }
159176 < S . CallingCodeOptionsList { ...getMenuProps ( ) } >
160177 { callingCodeOptions . length > 0 ? (
161178 callingCodeOptions . map ( ( item , index ) => (
@@ -166,11 +183,7 @@ export const PhoneNumberInput = ({
166183 index,
167184 } ) }
168185 title = { item . label }
169- withGroupHeader = {
170- item . isFeatured !==
171- ( callingCodeOptions [ index - 1 ] || { } ) . isFeatured &&
172- ( item . isFeatured ? featuredOptionsLabel : otherOptionsLabel )
173- }
186+ withGroupHeader = { getGroupHeaderLabel ( item , index ) }
174187 >
175188 < Flex >
176189 < S . CallingCodeOptionCallingCode >
@@ -214,6 +227,7 @@ export interface Props {
214227 isDisableCallingCode ?: boolean ;
215228 isPlaceholderFloating ?: boolean ;
216229 isRequired ?: boolean ;
230+ showFilterInput ?: boolean ;
217231}
218232
219233export interface PhoneNumber {
0 commit comments