2023-12-05 22:32:30 +00:00
|
|
|
import * as React from "react"
|
|
|
|
import { SelectProps } from "@mui/base/Select"
|
|
|
|
|
2023-12-05 22:55:01 +00:00
|
|
|
import type { Language } from "../types/Language"
|
|
|
|
|
2023-12-05 22:32:30 +00:00
|
|
|
import { Select, Option } from "./Select"
|
|
|
|
import { useFetchLanguages } from "../utils/queries"
|
|
|
|
|
|
|
|
export type SelectLanguageProps = SelectProps<{}, boolean>
|
|
|
|
|
|
|
|
export const SelectLanguage = React.forwardRef(function SelectLanguage(
|
|
|
|
props: SelectLanguageProps,
|
|
|
|
ref: React.ForwardedRef<HTMLButtonElement>
|
|
|
|
) {
|
|
|
|
const id = React.useId()
|
2023-12-05 22:55:01 +00:00
|
|
|
const [options, setOptions] = React.useState<Language[] | null>(null)
|
2023-12-05 22:32:30 +00:00
|
|
|
const { defaultValue, ...other } = props
|
|
|
|
const { isLoading, data } = useFetchLanguages()
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
2023-12-05 22:55:01 +00:00
|
|
|
if (data) {
|
|
|
|
setOptions(data)
|
2023-12-05 22:32:30 +00:00
|
|
|
}
|
|
|
|
}, [data])
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Select
|
|
|
|
ref={ref}
|
2023-12-05 22:55:01 +00:00
|
|
|
// Use key to force re-render and ensure the defaultValue takes effect.
|
|
|
|
key={!options && isLoading ? `${id}-loading` : `${id}-loaded`}
|
|
|
|
className={!options && isLoading ? "text-slate-900/60" : ""}
|
|
|
|
defaultValue={!options && isLoading ? [""] : defaultValue}
|
2023-12-05 22:32:30 +00:00
|
|
|
{...other}
|
|
|
|
>
|
2023-12-05 22:55:01 +00:00
|
|
|
{options?.map((lang, index) => {
|
2023-12-05 22:32:30 +00:00
|
|
|
return (
|
2023-12-05 22:55:01 +00:00
|
|
|
<Option key={index} value={lang.code}>
|
|
|
|
{lang.name}
|
2023-12-05 22:32:30 +00:00
|
|
|
</Option>
|
|
|
|
)
|
|
|
|
})}
|
|
|
|
</Select>
|
|
|
|
)
|
|
|
|
})
|