All files / src Moment.tsx

88.88% Statements 16/18
50% Branches 2/4
100% Functions 2/2
88.88% Lines 16/18

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 563x       3x 3x 3x 3x       3x                       3x 34x   34x 3x         34x   34x 34x         34x       34x                   3x  
import React, {
  ElementType,
  ReactNode
} from 'react';
import { useMomentUpdate } from './hooks/useMomentUpdate';
import { useMomentContext } from './MomentContext';
import { objectKeyFilter } from './objects';
import {
  MomentProps,
  momentPropKeys
} from './types';
import {
  getDatetime,
  getContent,
  getTitle
} from './utils';
 
/**
 * Renders a Moment component.
 *
 * @param props - The props for the component.
 * @returns The component.
 */
const Moment: React.FC<MomentProps> = (props: MomentProps) => {
  const { settings } = useMomentContext();
 
  useMomentUpdate(props, (currentProps) => {
    Iif (currentProps.onChange) {
      currentProps.onChange(getContent(currentProps, settings));
    }
  });
 
  const content = getContent(props, settings);
 
  const { withTitle, element, ...remaining } = props;
  const passProps = objectKeyFilter(
    remaining as Record<string, unknown>,
    momentPropKeys
  ) as Record<string, unknown>;
 
  Iif (withTitle) {
    passProps.title = getTitle(props, settings);
  }
 
  return React.createElement(
    (element || settings.element) as ElementType,
    {
      dateTime: getDatetime(props, settings) as unknown as string,
      ...passProps
    },
    content as ReactNode
  );
};
 
export default Moment;