foundation.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import BaseFoundation, { DefaultAdapter } from '../base/foundation';
  2. import warning from '../utils/warning';
  3. export interface AvatarAdapter<P = Record<string, any>, S = Record<string, any>> extends DefaultAdapter<P, S> {
  4. notifyImgState(isImgExist: boolean): void;
  5. notifyLeave(event: any): void;
  6. notifyEnter(event: any): void;
  7. setFocusVisible: (focusVisible: boolean) => void;
  8. }
  9. export default class AvatarFoundation<P = Record<string, any>, S = Record<string, any>> extends BaseFoundation<AvatarAdapter<P, S>, P, S> {
  10. constructor(adapter: AvatarAdapter<P, S>) {
  11. super({ ...adapter });
  12. }
  13. init() { } // eslint-disable-line
  14. destroy() { } // eslint-disable-line
  15. handleImgLoadError() {
  16. const { onError } = this.getProps();
  17. const errorFlag = onError ? onError() : undefined;
  18. if (errorFlag !== false) {
  19. this._adapter.notifyImgState(false);
  20. }
  21. }
  22. handleEnter(e: any) {
  23. this._adapter.notifyEnter(e);
  24. }
  25. handleLeave(e: any) {
  26. this._adapter.notifyLeave(e);
  27. }
  28. handleFocusVisible = (event: any) => {
  29. const { target } = event;
  30. try {
  31. if (target.matches(':focus-visible')) {
  32. this._adapter.setFocusVisible(true);
  33. }
  34. } catch (error){
  35. warning(true, 'Warning: [Semi Avatar] The current browser does not support the focus-visible');
  36. }
  37. }
  38. handleBlur = () => {
  39. this._adapter.setFocusVisible(false);
  40. }
  41. }