-
Notifications
You must be signed in to change notification settings - Fork 126
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
a3d07c9
commit d45f9f3
Showing
35 changed files
with
480 additions
and
415 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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
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
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,41 @@ | ||
import { msg } from "@lingui/macro"; | ||
import { useLingui } from "@lingui/react"; | ||
import { Loader, Search } from "react-feather"; | ||
import { useState } from "react"; | ||
import { Input } from "@/components/ui/input"; | ||
|
||
interface IProps { | ||
onChange: (value: string) => void; | ||
loading?: boolean; | ||
} | ||
|
||
export function FormSearch({ onChange, loading }: IProps) { | ||
const { _ } = useLingui(); | ||
|
||
const [value, setValue] = useState(""); | ||
|
||
return ( | ||
<div className="relative flex w-full"> | ||
<button | ||
className="text-primary pl-3 border-b border-border" | ||
type="button" | ||
> | ||
{loading ? ( | ||
<Loader className="animate-spin mr-2 h-4 w-4 shrink-0" /> | ||
) : ( | ||
<Search className="mr-2 h-4 w-4 shrink-0" /> | ||
)} | ||
</button> | ||
<Input | ||
className="rounded-none focus-visible:ring-0 px-1 border-t-0 border-x-0" | ||
type="search" | ||
value={value} | ||
onChange={(e) => { | ||
onChange(e.target.value.toLowerCase()); | ||
setValue(e.target.value); | ||
}} | ||
placeholder={_(msg`Search`)} | ||
/> | ||
</div> | ||
); | ||
} |
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,88 @@ | ||
import { useState } from "react"; | ||
import { useAsync, useDebounce } from "react-use"; | ||
import { ISelectData } from "shared/types/options"; | ||
import { useApi } from "frontend/lib/data/useApi"; | ||
import { ApiRequest } from "frontend/lib/data/makeRequest"; | ||
import { ErrorAlert } from "@/components/app/alert"; | ||
import { IBaseFormSelect } from "@/frontend/design-system/components/Form/Select/types"; | ||
import { FormSelect } from "./select"; | ||
|
||
interface IProps extends IBaseFormSelect { | ||
url: string; | ||
referenceUrl?: (value: string) => string; | ||
limit?: number; | ||
} | ||
|
||
export function AsyncFormSelect(props: IProps) { | ||
const { input, url, referenceUrl, limit = 50 } = props; | ||
|
||
const [search, setSearch] = useState(""); | ||
const [debounceSearch, setDebounceSearch] = useState(""); | ||
|
||
const fullData = useApi<ISelectData[]>(url, { | ||
defaultData: [], | ||
}); | ||
|
||
const selectOptions = useApi<ISelectData[]>( | ||
debounceSearch ? `${url}?search=${debounceSearch}` : url, | ||
{ | ||
defaultData: [], | ||
} | ||
); | ||
|
||
const valueLabelToUse = useAsync(async () => { | ||
if (!input.value) { | ||
return undefined; | ||
} | ||
const isValueInFirstDataLoad = fullData.data.find( | ||
({ value }: ISelectData) => value === input.value | ||
); | ||
|
||
if (isValueInFirstDataLoad) { | ||
return isValueInFirstDataLoad.label; | ||
} | ||
|
||
const isValueInSelectionOptions = selectOptions.data.find( | ||
({ value }: ISelectData) => value === input.value | ||
); | ||
|
||
if (isValueInSelectionOptions) { | ||
return isValueInSelectionOptions.label; | ||
} | ||
|
||
if (!referenceUrl) { | ||
return undefined; // or the value | ||
} | ||
|
||
return await ApiRequest.GET(referenceUrl(input.value)); | ||
}, [url, fullData.isLoading]); | ||
|
||
useDebounce( | ||
() => { | ||
setDebounceSearch(search); | ||
}, | ||
700, | ||
[search] | ||
); | ||
|
||
if (fullData.error || selectOptions.error) { | ||
return <ErrorAlert message={fullData.error || selectOptions.error} />; | ||
} | ||
|
||
if (fullData.data.length >= limit) { | ||
return ( | ||
<FormSelect | ||
{...props} | ||
selectData={selectOptions.data} | ||
onSearch={{ | ||
isLoading: selectOptions.isLoading, | ||
onChange: setSearch, | ||
value: search, | ||
valueLabel: valueLabelToUse.value, | ||
}} | ||
/> | ||
); | ||
} | ||
|
||
return <FormSelect {...props} selectData={fullData.data} />; | ||
} |
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
Oops, something went wrong.