All files / src/form/cells DateTimeCell.tsx

100% Statements 9/9
88.88% Branches 8/9
100% Functions 1/1
100% Lines 9/9

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                              67x 6x     6x 6x 6x 3x   3x 3x   3x          
import React, { ReactElement } from "react";
import { DateFormat, DateTimeFormat } from '@config/base';
import dayjs from "dayjs";
interface DateTimeCellRenderProps {
  value: string;
  type: string;
}
 
/**
 * 日期格式标签
 * value: 日期值。
 * type: 日期格式化。例如:YYYY-MM-DD
 * @param props
 * @constructor
 */
const DateTimeCell = (props: DateTimeCellRenderProps): ReactElement => {
  const { value, type } = props;
 
  // console.log("DateTimeCell", a, value, type);
  const classNameValue = type + "-value";
  const emptyDiv = (<div className={classNameValue} />);
  if (value === null || value === undefined || value === "undefined") {
    return emptyDiv;
  }
  const format = (type === "date") ? DateFormat : DateTimeFormat;
  const datetimeValue = dayjs(value).format(format);
 
  return (datetimeValue != null) ?
    (<div className={classNameValue}>{datetimeValue}</div>) : emptyDiv;
};
 
export default DateTimeCell;