import React, { useState, useEffect, useRef } from 'react'; import { Icon, IconTypes } from '../Icon'; import './styles/index.scss'; export interface AvatarProps { image?: string | null, /** click event handler */ onClick?: (event: React.BaseSyntheticEvent) => void, /** mouseOver event handler */ onMouseOver?: (event: React.BaseSyntheticEvent) => void, /** Size in pixels * @default 32px */ size?: number, /** Shape of the avatar - circle, rounded or square * @default circle */ shape?: 'circle' | 'rounded' | 'square'; update?: (url:string) => void, list?: Array, } export function Avatar(props:T):React.ReactElement { const { image, size = 32, shape = 'circle', update, onClick, list = [], } = props; const [error, setError] = useState(false); const [loaded, setLoaded] = useState(false); const [isEdit, setIsEdit] = useState(false); const [avatarListWidth, setAvatarListWidth] = useState(200); const avatarRef = useRef(); const avatarListRef = useRef(); useEffect(() => { setError(false); setLoaded(false); }, [image]); useEffect(() => { if (isEdit && update) { window.addEventListener('mouseup', toggle, false); avatarRef?.current?.addEventListener('mouseup', stopImmediatePropagation); setAvatarListWidth(avatarListRef?.current?.clientWidth); } else { window.removeEventListener('mouseup', toggle, false); avatarRef?.current?.removeEventListener('mouseup', stopImmediatePropagation); } return () => { window.removeEventListener('mouseup', toggle, false); avatarRef?.current?.removeEventListener('mouseup', stopImmediatePropagation); }; }, [isEdit]); const stopImmediatePropagation = (e) => { e.stopPropagation(); }; const toggle = () => { setIsEdit(!isEdit); }; const handleUpdate = (value:string) => { toggle(); update(value); }; return (
{image && !error ? ( setError(true)} onLoad={() => setLoaded(true)} alt={image} src={image} /> ) : (
)} { update && (
) } { update && isEdit && (
    { list.map((item:string, index:number) => { const key = `${item}${index}`; return (
  • { handleUpdate(item); }} > {item}
  • ); }) }
) }
); }