@@ -7,17 +7,10 @@ import { organizations } from "api/queries/organizations";
77import type { AuthorizationCheck , Organization } from "api/typesGenerated" ;
88import { Avatar } from "components/Avatar/Avatar" ;
99import { 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" ;
1711import { useQuery } from "react-query" ;
1812
1913export type OrganizationAutocompleteProps = {
20- value : Organization | null ;
2114 onChange : ( organization : Organization | null ) => void ;
2215 label ?: string ;
2316 className ?: string ;
@@ -27,21 +20,16 @@ export type OrganizationAutocompleteProps = {
2720} ;
2821
2922export const OrganizationAutocomplete : FC < OrganizationAutocompleteProps > = ( {
30- value,
3123 onChange,
3224 label,
3325 className,
3426 size = "small" ,
3527 required,
3628 check,
3729} ) => {
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+
4533 const organizationsQuery = useQuery ( organizations ( ) ) ;
4634
4735 const permissionsQuery = useQuery (
@@ -60,16 +48,6 @@ export const OrganizationAutocomplete: FC<OrganizationAutocompleteProps> = ({
6048 : { enabled : false } ,
6149 ) ;
6250
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-
7351 // If an authorization check was provided, filter the organizations based on
7452 // the results of that check.
7553 let options = organizationsQuery . data ?? [ ] ;
@@ -85,24 +63,18 @@ export const OrganizationAutocomplete: FC<OrganizationAutocompleteProps> = ({
8563 className = { className }
8664 options = { options }
8765 loading = { organizationsQuery . isLoading }
88- value = { value }
8966 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 }
9269 getOptionLabel = { ( option ) => option . display_name }
9370 onOpen = { ( ) => {
94- setAutoComplete ( ( state ) => ( {
95- ...state ,
96- open : true ,
97- } ) ) ;
71+ setOpen ( true ) ;
9872 } }
9973 onClose = { ( ) => {
100- setAutoComplete ( {
101- value : value ?. name ?? "" ,
102- open : false ,
103- } ) ;
74+ setOpen ( false ) ;
10475 } }
10576 onChange = { ( _ , newValue ) => {
77+ setSelected ( newValue ) ;
10678 onChange ( newValue ) ;
10779 } }
10880 renderOption = { ( { key, ...props } , option ) => (
@@ -130,13 +102,12 @@ export const OrganizationAutocomplete: FC<OrganizationAutocompleteProps> = ({
130102 } }
131103 InputProps = { {
132104 ...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 } />
136107 ) ,
137108 endAdornment : (
138109 < >
139- { organizationsQuery . isFetching && autoComplete . open && (
110+ { organizationsQuery . isFetching && open && (
140111 < CircularProgress size = { 16 } />
141112 ) }
142113 { params . InputProps . endAdornment }
@@ -154,6 +125,6 @@ export const OrganizationAutocomplete: FC<OrganizationAutocompleteProps> = ({
154125} ;
155126
156127const 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 ;
159130` ;
0 commit comments