import React, { useState } from 'react'; import { useFormikContext } from 'formik'; import { searchRoles } from '@/api/admin/roles'; import SearchableSelect, { Option } from '@/components/elements/SearchableSelect'; import { UserRole } from '@definitions/admin'; export default ({ selected }: { selected: UserRole | null }) => { const context = useFormikContext(); const [ role, setRole ] = useState(selected); const [ roles, setRoles ] = useState(null); const onSearch = (query: string): Promise => { return new Promise((resolve, reject) => { searchRoles({ name: query }) .then(roles => { setRoles(roles); return resolve(); }) .catch(reject); }); }; const onSelect = (role: UserRole | null) => { setRole(role); context.setFieldValue('adminRoleId', role?.id || null); }; const getSelectedText = (role: UserRole | null): string | undefined => { return role?.name; }; return ( {roles?.map(d => ( ))} ); };