imageFoundation.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import BaseFoundation, { DefaultAdapter } from "../base/foundation";
  2. import { isObject } from "lodash";
  3. export interface ImageAdapter<P = Record<string, any>, S = Record<string, any>> extends DefaultAdapter<P, S> {
  4. getIsInGroup: () => boolean
  5. }
  6. export default class ImageFoundation<P = Record<string, any>, S = Record<string, any>> extends BaseFoundation<ImageAdapter<P, S>, P, S> {
  7. constructor(adapter: ImageAdapter<P, S>) {
  8. super({ ...adapter });
  9. }
  10. handleClick = (e: any) => {
  11. const { imageID, preview } = this.getProps();
  12. // if preview = false, then it cannot preview
  13. if (!preview) {
  14. return;
  15. }
  16. // if image in group, then use group's Preview components
  17. if (this._adapter.getIsInGroup()) {
  18. const { setCurrentIndex, handleVisibleChange } = this._adapter.getContexts();
  19. setCurrentIndex(imageID);
  20. handleVisibleChange(true);
  21. } else {
  22. // image isn't in group, then use it's own Preview components
  23. this.handlePreviewVisibleChange(true);
  24. }
  25. }
  26. handleLoaded = (e: any) => {
  27. const { onLoad } = this.getProps();
  28. onLoad && onLoad(e);
  29. this.setState ({
  30. loadStatus: "success",
  31. } as any);
  32. }
  33. handleError = (e: any) => {
  34. const { onError } = this.getProps();
  35. onError && onError(e);
  36. this.setState ({
  37. loadStatus: "error",
  38. } as any);
  39. }
  40. handlePreviewVisibleChange = (newVisible: boolean) => {
  41. const { preview } = this.getProps();
  42. if (isObject(preview)) {
  43. const { onVisibleChange } = preview as any;
  44. onVisibleChange && onVisibleChange(newVisible);
  45. if (!("visible" in this.getProps())) {
  46. this.setState({
  47. previewVisible: newVisible,
  48. } as any);
  49. }
  50. } else {
  51. this.setState({
  52. previewVisible: newVisible,
  53. } as any);
  54. }
  55. }
  56. }