-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b053753
commit f270d0c
Showing
14 changed files
with
373 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import React, { useEffect, useRef, useState } from 'react'; | ||
|
||
interface ControlledInputProps extends React.InputHTMLAttributes<HTMLInputElement> { | ||
value: string; | ||
onChange?: (e: React.ChangeEvent<HTMLInputElement>) => void; | ||
} | ||
|
||
const ControlledInput: React.FC<ControlledInputProps> = ({ value, onChange, ...rest }) => { | ||
const [cursor, setCursor] = useState<number | null>(null); | ||
const ref = useRef<HTMLInputElement | null>(null); | ||
|
||
useEffect(() => { | ||
const input = ref.current; | ||
if (input && cursor !== null) { | ||
input.setSelectionRange(cursor, cursor); | ||
} | ||
}, [ref, cursor, value]); | ||
|
||
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
setCursor(e.target.selectionStart || 0); | ||
if (onChange) { | ||
onChange(e); | ||
} | ||
}; | ||
|
||
return <input onChange={handleChange} ref={ref} value={value} {...rest} />; | ||
}; | ||
|
||
export default ControlledInput; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
frontend/src/components/components/AsyncComponentSelect.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import React, { useCallback, useState } from "react"; | ||
import AsyncSelect from "react-select/async"; | ||
import debounce from "lodash/debounce"; | ||
import { useQuery } from "@tanstack/react-query"; | ||
import axios from "axios"; | ||
|
||
interface ComponentOption { | ||
value: string; | ||
label: string; | ||
} | ||
|
||
interface AsyncComponentSelectProps { | ||
placeholder?: string; | ||
onChange: (selected: ComponentOption | null) => void; | ||
value?: ComponentOption | null; | ||
} | ||
|
||
const fetchComponentOptions = async (inputValue: string): Promise<ComponentOption[]> => { | ||
const response = await axios.get("/api/components-autocomplete/", { | ||
params: { q: inputValue }, | ||
xsrfCookieName: "csrftoken", | ||
xsrfHeaderName: "X-CSRFToken", | ||
}); | ||
return response.data.results.map((item: { id: string; text: string }) => ({ | ||
label: item.text, | ||
value: item.id, | ||
})); | ||
}; | ||
|
||
export const useComponentAutocomplete = (inputValue: string) => { | ||
return useQuery({ | ||
enabled: Boolean(inputValue && inputValue.length >= 2), | ||
queryFn: () => fetchComponentOptions(inputValue), | ||
queryKey: ["components-autocomplete", inputValue], | ||
}); | ||
}; | ||
|
||
const AsyncComponentSelect: React.FC<AsyncComponentSelectProps> = ({ | ||
placeholder = "Search components...", | ||
onChange, | ||
value, | ||
}) => { | ||
const [searchTerm, setSearchTerm] = useState(""); | ||
|
||
const { data: componentOptions = [], isLoading } = useComponentAutocomplete(searchTerm); | ||
|
||
const debouncedLoadOptions = useCallback( | ||
debounce((inputValue: string, callback: (options: ComponentOption[]) => void) => { | ||
setSearchTerm(inputValue); | ||
callback(componentOptions); | ||
}, 200), | ||
[componentOptions] | ||
); | ||
|
||
return ( | ||
<AsyncSelect | ||
cacheOptions | ||
className="w-full h-10" | ||
isLoading={isLoading} | ||
loadOptions={debouncedLoadOptions} | ||
menuPortalTarget={document.body} | ||
menuPosition="fixed" | ||
onChange={onChange} | ||
placeholder={placeholder} | ||
styles={{ | ||
menu: (provided) => ({ | ||
...provided, | ||
maxHeight: 200, | ||
overflowY: "auto", | ||
}), | ||
menuPortal: (base) => ({ ...base, zIndex: 9999 }), | ||
}} | ||
value={value} | ||
/> | ||
); | ||
}; | ||
|
||
export default AsyncComponentSelect; |
Oops, something went wrong.