@@ -7,17 +7,10 @@ import { organizations } from "api/queries/organizations";
7
7
import type { AuthorizationCheck , Organization } from "api/typesGenerated" ;
8
8
import { Avatar } from "components/Avatar/Avatar" ;
9
9
import { AvatarData } from "components/Avatar/AvatarData" ;
10
- import { useDebouncedFunction } from "hooks/debounce" ;
11
- import {
12
- type ChangeEvent ,
13
- type ComponentProps ,
14
- type FC ,
15
- useState ,
16
- } from "react" ;
10
+ import { type ComponentProps , type FC , useState } from "react" ;
17
11
import { useQuery } from "react-query" ;
18
12
19
13
export type OrganizationAutocompleteProps = {
20
- value : Organization | null ;
21
14
onChange : ( organization : Organization | null ) => void ;
22
15
label ?: string ;
23
16
className ?: string ;
@@ -27,21 +20,16 @@ export type OrganizationAutocompleteProps = {
27
20
} ;
28
21
29
22
export const OrganizationAutocomplete : FC < OrganizationAutocompleteProps > = ( {
30
- value,
31
23
onChange,
32
24
label,
33
25
className,
34
26
size = "small" ,
35
27
required,
36
28
check,
37
29
} ) => {
38
- const [ autoComplete , setAutoComplete ] = useState < {
39
- value : string ;
40
- open : boolean ;
41
- } > ( {
42
- value : value ?. name ?? "" ,
43
- open : false ,
44
- } ) ;
30
+ const [ open , setOpen ] = useState ( false ) ;
31
+ const [ selected , setSelected ] = useState < Organization | null > ( null ) ;
32
+
45
33
const organizationsQuery = useQuery ( organizations ( ) ) ;
46
34
47
35
const permissionsQuery = useQuery (
@@ -60,16 +48,6 @@ export const OrganizationAutocomplete: FC<OrganizationAutocompleteProps> = ({
60
48
: { enabled : false } ,
61
49
) ;
62
50
63
- const { debounced : debouncedInputOnChange } = useDebouncedFunction (
64
- ( event : ChangeEvent < HTMLInputElement > ) => {
65
- setAutoComplete ( ( state ) => ( {
66
- ...state ,
67
- value : event . target . value ,
68
- } ) ) ;
69
- } ,
70
- 750 ,
71
- ) ;
72
-
73
51
// If an authorization check was provided, filter the organizations based on
74
52
// the results of that check.
75
53
let options = organizationsQuery . data ?? [ ] ;
@@ -85,24 +63,18 @@ export const OrganizationAutocomplete: FC<OrganizationAutocompleteProps> = ({
85
63
className = { className }
86
64
options = { options }
87
65
loading = { organizationsQuery . isLoading }
88
- value = { value }
89
66
data-testid = "organization-autocomplete"
90
- open = { autoComplete . open }
91
- isOptionEqualToValue = { ( a , b ) => a . name === b . name }
67
+ open = { open }
68
+ isOptionEqualToValue = { ( a , b ) => a . id === b . id }
92
69
getOptionLabel = { ( option ) => option . display_name }
93
70
onOpen = { ( ) => {
94
- setAutoComplete ( ( state ) => ( {
95
- ...state ,
96
- open : true ,
97
- } ) ) ;
71
+ setOpen ( true ) ;
98
72
} }
99
73
onClose = { ( ) => {
100
- setAutoComplete ( {
101
- value : value ?. name ?? "" ,
102
- open : false ,
103
- } ) ;
74
+ setOpen ( false ) ;
104
75
} }
105
76
onChange = { ( _ , newValue ) => {
77
+ setSelected ( newValue ) ;
106
78
onChange ( newValue ) ;
107
79
} }
108
80
renderOption = { ( { key, ...props } , option ) => (
@@ -130,13 +102,12 @@ export const OrganizationAutocomplete: FC<OrganizationAutocompleteProps> = ({
130
102
} }
131
103
InputProps = { {
132
104
...params . InputProps ,
133
- onChange : debouncedInputOnChange ,
134
- startAdornment : value && (
135
- < Avatar size = "sm" src = { value . icon } fallback = { value . name } />
105
+ startAdornment : selected && (
106
+ < Avatar size = "sm" src = { selected . icon } fallback = { selected . name } />
136
107
) ,
137
108
endAdornment : (
138
109
< >
139
- { organizationsQuery . isFetching && autoComplete . open && (
110
+ { organizationsQuery . isFetching && open && (
140
111
< CircularProgress size = { 16 } />
141
112
) }
142
113
{ params . InputProps . endAdornment }
@@ -154,6 +125,6 @@ export const OrganizationAutocomplete: FC<OrganizationAutocompleteProps> = ({
154
125
} ;
155
126
156
127
const root = css `
157
- padding-left : 14px !important ; // Same padding left as input
158
- gap : 4px ;
128
+ padding-left : 14px !important ; // Same padding left as input
129
+ gap : 4px ;
159
130
` ;
0 commit comments