API Select Widget #166
-
How to fill Select values from API call json reponse and dependency select box change based on selected item ? example like fetching countrylist from API and bind it and based on country selection i need to populate states and based on state selection i need to populated city states and Cities are also returned from API all this requirement is in Stepper |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
There are no API features includes for fetching values, you would need to write an own plugin or widget.
You could use
import { MuiWidgetBinding, Select } from '@ui-schema/ds-material'
import { useProgress, PROGRESS_DONE, PROGRESS_ERROR, PROGRESS_START } from '@ui-schema/ui-schema/UIApi'
const CountrySelect: React.ComponentType<WidgetProps<{}, MuiWidgetBinding> & WithScalarValue> = ({schema, ...props}) => {
const [countries, setCountries] = React.useState<List<string>>(List())
const [loading, setLoading] = useProgress()
React.useEffect(() => {
setLoading(PROGRESS_START)
fetch('https://restcountries.com/v3.1/subregion/europe', {method: 'GET'})
.then(r => r.json())
.then(data => {
console.log(data)
setCountries(List(data.map((d: { name: { common: string } }) => d.name.common)))
setLoading(PROGRESS_DONE)
})
.catch(() => {
setLoading(PROGRESS_ERROR)
})
}, [setCountries, setLoading])
if (loading === PROGRESS_START) {
return <LinearProgress/>
}
return <Select
{...props}
schema={schema.set('enum', countries)}
/>
} For your other requirements, it depends on how your schema is structured, the simplest solution would be to get the value of Otherwise a "country-state-city widget" could make sense, which is e.g. of |
Beta Was this translation helpful? Give feedback.
There are no API features includes for fetching values, you would need to write an own plugin or widget.
api=country
in you schema, then load it accordingly to your schemas definition and also inject it into the schema.You could use
fetch
or e.g. existing redux stores.