import React from "react";
import { TransitionProps } from "react-transition-group/Transition";
import { fade } from "../_util/transition";
import { BaseTransition } from "./BaseTransition";

export interface FadeTransitionProps extends Partial<TransitionProps> {
  /**
   * 目标出现时的透明度，默认为 1
   */
  opacity?: number;
}

export function FadeTransition({
  opacity,
  timeout,
  ...rest
}: FadeTransitionProps) {
  let enterTimeout: number;
  let exitTimeout: number;
  if (typeof timeout === "number") {
    enterTimeout = timeout;
    exitTimeout = timeout;
  } else if (timeout && typeof timeout === "object") {
    enterTimeout = timeout.enter;
    exitTimeout = timeout.exit;
  }
  return (
    <BaseTransition {...fade(opacity, enterTimeout, exitTimeout)} {...rest} />
  );
}

FadeTransition.displayName = "FadeTransition";
