1+ import { useMemo , useState } from "react" ;
2+
13import { useLocale } from "@calcom/lib/hooks/useLocale" ;
2- import classNames from "@calcom/ui/classNames" ;
34
45import { Dropdown , DropdownMenuContent , DropdownMenuItem , DropdownMenuTrigger } from "../../dropdown" ;
56import { Icon } from "../../icon" ;
@@ -14,9 +15,24 @@ interface IAddVariablesDropdown {
1415
1516export const AddVariablesDropdown = ( props : IAddVariablesDropdown ) => {
1617 const { t } = useLocale ( ) ;
18+ const [ query , setQuery ] = useState ( "" ) ;
19+
20+ const filteredVariables = useMemo ( ( ) => {
21+ const q = query . trim ( ) . toLowerCase ( ) ;
22+ if ( ! q ) return props . variables ;
23+ return props . variables . filter ( ( variable ) => {
24+ const key = variable . toLowerCase ( ) ;
25+ const name = t ( `${ variable } _variable` ) . toLowerCase ( ) ;
26+ const info = t ( `${ variable } _info` ) . toLowerCase ( ) ;
27+ return key . includes ( q ) || name . includes ( q ) || info . includes ( q ) ;
28+ } ) ;
29+ } , [ props . variables , query , t ] ) ;
1730
1831 return (
19- < Dropdown >
32+ < Dropdown
33+ onOpenChange = { ( open ) => {
34+ if ( ! open ) setQuery ( "" ) ;
35+ } } >
2036 < DropdownMenuTrigger aria-label = "Add variable" className = "focus:bg-muted pt-[6px]" >
2137 < div className = "items-center" >
2238 { props . isTextEditor ? (
@@ -49,25 +65,42 @@ export const AddVariablesDropdown = (props: IAddVariablesDropdown) => {
4965 < div className = "text-subtle mb-3 text-left text-xs font-medium uppercase tracking-wide" >
5066 { t ( "add_dynamic_variables" ) }
5167 </ div >
68+ < div className = "mb-2 px-2" >
69+ < input
70+ type = "text"
71+ value = { query }
72+ onChange = { ( e ) => setQuery ( e . target . value ) }
73+ placeholder = { t ( "search_variables" ) }
74+ aria-label = { t ( "search_variables" ) }
75+ className = "border-subtle bg-default focus:ring-brand-800 w-full rounded-md border px-3 py-2 text-sm outline-none focus:ring-1"
76+ />
77+ </ div >
5278 < div className = "max-h-64 overflow-y-auto md:max-h-80" >
53- { props . variables . map ( ( variable ) => (
54- < DropdownMenuItem key = { variable } className = "hover:ring-0" >
55- < button
56- key = { variable }
57- type = "button"
58- className = "hover:bg-muted w-full rounded-md px-3 py-2 text-left transition-colors"
59- onClick = { ( ) => props . addVariable ( t ( `${ variable } _variable` ) ) } >
60- < div className = "flex flex-col space-y-1" >
61- < div className = "text-default font-mono text-sm" >
62- { `{${ t ( `${ variable } _variable` ) . toUpperCase ( ) . replace ( / / g, "_" ) } }` }
79+ { filteredVariables . length === 0 ? (
80+ < div className = "text-subtle px-4 py-2 text-center text-sm" > { t ( "no_variables_found" ) } </ div >
81+ ) : (
82+ filteredVariables . map ( ( variable ) => (
83+ < DropdownMenuItem key = { variable } className = "hover:ring-0" >
84+ < button
85+ key = { variable }
86+ type = "button"
87+ className = "hover:bg-muted w-full rounded-md px-3 py-2 text-left transition-colors"
88+ onClick = { ( ) => {
89+ props . addVariable ( t ( `${ variable } _variable` ) ) ;
90+ setQuery ( "" ) ;
91+ } } >
92+ < div className = "flex flex-col space-y-1" >
93+ < div className = "text-default font-mono text-sm" >
94+ { `{${ t ( `${ variable } _variable` ) . toUpperCase ( ) . replace ( / / g, "_" ) } }` }
95+ </ div >
96+ < div className = "text-muted-foreground hidden text-xs sm:block" >
97+ { t ( `${ variable } _info` ) }
98+ </ div >
6399 </ div >
64- < div className = "text-muted-foreground hidden text-xs sm:block" >
65- { t ( `${ variable } _info` ) }
66- </ div >
67- </ div >
68- </ button >
69- </ DropdownMenuItem >
70- ) ) }
100+ </ button >
101+ </ DropdownMenuItem >
102+ ) )
103+ ) }
71104 </ div >
72105 </ div >
73106 </ DropdownMenuContent >
0 commit comments