import React, { useState } from 'react'; import { useFormikContext } from 'formik'; import { Location } from '@/api/admin/locations/getLocations'; import searchLocations from '@/api/admin/locations/searchLocations'; import SearchableSelect, { Option } from '@/components/elements/SearchableSelect'; export default ({ selected }: { selected: Location | null }) => { const context = useFormikContext(); const [ location, setLocation ] = useState(selected); const [ locations, setLocations ] = useState(null); const onSearch = (query: string): Promise => { return new Promise((resolve, reject) => { searchLocations({ short: query }).then((locations) => { setLocations(locations); return resolve(); }).catch(reject); }); }; const onSelect = (location: Location | null) => { setLocation(location); context.setFieldValue('locationId', location?.id || null); }; const getSelectedText = (location: Location | null): string => { return location?.short || ''; }; return ( {locations?.map(d => ( ))} ); };