All files / src/inputadornment InputAdornment.tsx

100% Statements 12/12
100% Branches 11/11
100% Functions 3/3
100% Lines 12/12

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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94                                                                    3x 13x     3x         7x                                       10x 10x   2x   10x   5x     10x                       3x             3x  
import React from "react";
import classNames from "classnames";
import { StyledProps } from "../_type";
import { useConfig } from "../_util/config-context";
 
export interface InputAdornmentProps extends StyledProps {
  /**
   * 前缀装饰
   */
  before?: React.ReactNode;
 
  /**
   * 后缀装饰
   */
  after?: React.ReactNode;
 
  /**
   * 被装饰内容
   */
  children?: React.ReactNode;
 
  /**
   * 展示类型
   * @default “default”
   */
  appearance?: "default" | "pure";
 
  /**
   * **\[Deprecated\]** 请使用 `appearance` 属性
   * @deprecated
   */
  appearence?: "default" | "pure";
}
 
const wrapAdornmentAddon = (addon: React.ReactNode, classPrefix: string) => (
  <div className={`${classPrefix}-input-group__addon`}>{addon}</div>
);
 
const wrapAdornmentText = (
  text: string,
  pure: boolean,
  classPrefix: string
) => (
  <span
    className={
      pure
        ? `${classPrefix}-form__help-text--inline`
        : `${classPrefix}-input-group__text`
    }
  >
    {text}
  </span>
);
 
export function InputAdornment({
  before,
  after,
  children,
  appearence,
  appearance = appearence,
  className,
  style,
}: InputAdornmentProps) {
  const { classPrefix } = useConfig();
  if (typeof before === "string") {
    // eslint-disable-next-line no-param-reassign
    before = wrapAdornmentText(before, false, classPrefix);
  }
  if (typeof after === "string") {
    // eslint-disable-next-line no-param-reassign
    after = wrapAdornmentText(after, appearance === "pure", classPrefix);
  }
 
  return (
    <div
      className={classNames(`${classPrefix}-input-group`, className)}
      style={style}
    >
      {before && wrapAdornmentAddon(before, classPrefix)}
      {children}
      {after && wrapAdornmentAddon(after, classPrefix)}
    </div>
  );
}
 
InputAdornment.defaultLabelAlign = "middle";
 
/**
 * @deprecated
 *
 * 请使用 InputAdornment 代替
 */
export const InputAdorment = InputAdornment;