All files / src/form FormControl.tsx

100% Statements 3/3
80% Branches 4/5
100% Functions 1/1
100% Lines 3/3

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78                                                                                                    56x 56x               56x                                    
import React from "react";
import classNames from "classnames";
import { StyledProps } from "../_type";
import { SlideTransition, FadeTransition } from "../transition";
import { useConfig } from "../_util/config-context";
 
export interface FormControlProps extends StyledProps {
  /**
   * 表单组件
   */
  children?: React.ReactNode;
 
  /**
   * 字段状态
   */
  status?: "success" | "error" | "validating";
 
  /**
   * 是否展示 icon
   * @default true
   */
  showStatusIcon?: boolean;
 
  /**
   * 表单说明消息/错误消息
   */
  message?: React.ReactNode;
 
  /**
   * 是否增加顶部间距,以对齐标签文本
   * @default false
   */
  alignLabelTop?: boolean;
 
  /**
   * 是否为必填字段
   * @default false
   */
  required?: boolean;
}
 
export function FormControl({
  status,
  children,
  message,
  className,
  style,
  alignLabelTop,
  showStatusIcon = true,
}: FormControlProps) {
  const { classPrefix } = useConfig();
  const controlClassName = classNames(
    `${classPrefix}-form__controls`,
    {
      [`${classPrefix}-form__controls--text`]: alignLabelTop,
      [`is-${status}`]: status,
    },
    className
  );
  return (
    <div className={controlClassName} style={style}>
      {children}
      <SlideTransition in={Boolean(status)} from={[-10, 0]}>
        {showStatusIcon ? (
          <b className={`${classPrefix}-icon ${classPrefix}-icon-valid`} />
        ) : (
          <b />
        )}
      </SlideTransition>
      {/* <FadeTransition in={Boolean(message)}> */}
      {Boolean(message) && (
        <div className={`${classPrefix}-form__help-text`}>{message}</div>
      )}
      {/* </FadeTransition> */}
    </div>
  );
}