Ensure language defaultValue is used.
parent
a61477a5fe
commit
1c1f8f6fb0
|
@ -1,6 +1,8 @@
|
||||||
import * as React from "react"
|
import * as React from "react"
|
||||||
import { SelectProps } from "@mui/base/Select"
|
import { SelectProps } from "@mui/base/Select"
|
||||||
|
|
||||||
|
import type { Language } from "../types/Language"
|
||||||
|
|
||||||
import { Select, Option } from "./Select"
|
import { Select, Option } from "./Select"
|
||||||
import { useFetchLanguages } from "../utils/queries"
|
import { useFetchLanguages } from "../utils/queries"
|
||||||
|
|
||||||
|
@ -11,31 +13,29 @@ export const SelectLanguage = React.forwardRef(function SelectLanguage(
|
||||||
ref: React.ForwardedRef<HTMLButtonElement>
|
ref: React.ForwardedRef<HTMLButtonElement>
|
||||||
) {
|
) {
|
||||||
const id = React.useId()
|
const id = React.useId()
|
||||||
const [options, setOptions] = React.useState([
|
const [options, setOptions] = React.useState<Language[] | null>(null)
|
||||||
{ value: "", label: "Loading..." },
|
|
||||||
])
|
|
||||||
const { defaultValue, ...other } = props
|
const { defaultValue, ...other } = props
|
||||||
const { isLoading, data } = useFetchLanguages()
|
const { isLoading, data } = useFetchLanguages()
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
if (!data) {
|
if (data) {
|
||||||
return
|
setOptions(data)
|
||||||
}
|
}
|
||||||
setOptions(data.map((row) => ({ value: row.code, label: row.name })))
|
|
||||||
}, [data])
|
}, [data])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Select
|
<Select
|
||||||
ref={ref}
|
ref={ref}
|
||||||
key={isLoading ? `${id}-loading` : `${id}-loaded`}
|
// Use key to force re-render and ensure the defaultValue takes effect.
|
||||||
className={isLoading ? "text-slate-900/60" : ""}
|
key={!options && isLoading ? `${id}-loading` : `${id}-loaded`}
|
||||||
defaultValue={defaultValue}
|
className={!options && isLoading ? "text-slate-900/60" : ""}
|
||||||
|
defaultValue={!options && isLoading ? [""] : defaultValue}
|
||||||
{...other}
|
{...other}
|
||||||
>
|
>
|
||||||
{options.map((entry, index) => {
|
{options?.map((lang, index) => {
|
||||||
return (
|
return (
|
||||||
<Option key={index} value={entry.value}>
|
<Option key={index} value={lang.code}>
|
||||||
{entry.label}
|
{lang.name}
|
||||||
</Option>
|
</Option>
|
||||||
)
|
)
|
||||||
})}
|
})}
|
||||||
|
|
Loading…
Reference in New Issue