import { FC, useState } from 'react'; import { BoxProps, Icon, Button } from '../../../general'; import { Input } from '../Input/Input'; import { InputBox } from '../InputBox/InputBox'; import { isImgUrl } from '../../util/strings'; type AvatarInputProps = { id: string; tabIndex?: number; initialValue?: string; onSave: (url: string) => void; } & BoxProps; export const AvatarInput: FC = (props: AvatarInputProps) => { const { id, tabIndex, initialValue, onSave } = props; const [invalidImg, setInvalidImg] = useState(false); const [value, setValue] = useState(initialValue || ''); const [success, setSuccess] = useState(false); let textButtonContent: string | JSX.Element = 'Save'; if (invalidImg) textButtonContent = 'Clear'; if (success) textButtonContent = ; return ( } rightAdornment={ ) => { if (invalidImg) { setInvalidImg(false); setValue(''); setSuccess(false); return; } evt.preventDefault(); const isImage: boolean = await isImgUrl(value); if (isImage) { setInvalidImg(false); setSuccess(true); onSave(value); } else { setInvalidImg(true); setSuccess(false); } }} > {textButtonContent} } error={invalidImg ? 'Invalid image url' : undefined} inputId={id} > ) => { if (evt.target.value === '') { setInvalidImg(false); setSuccess(false); } setValue(evt.target.value); }} onKeyDown={props.onKeyDown} /> ); };