All files / elements/forms/utils/PlainText component.js

100% Statements 18/18
100% Branches 10/10
100% Functions 4/4
100% Lines 16/16

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          21x 21x 13x 13x 5x   10x 1x 9x 2x   7x     21x             21x 13x 13x             21x           21x      
import React from 'react';
import PropTypes from 'prop-types';
import { removeSomeProps } from 'src/utils/componentHelpers';
import { control, spaceProps, typography } from 'src/utils/styledHelpers';
 
const isArrayOptionsValue = value => !!value && Array.isArray(value);
const getDisplayValue = props => {
  const { value } = props;
  if (isArrayOptionsValue(value)) {
    return value.map(e => e.label).join(', ');
  }
  if (!!value && !!value.label) {
    return value.label;
  } if (typeof value === 'string') {
    return value;
  }
  return '';
};
 
const propsToTrim = [
  'labelText',
  ...Object.keys(control.propTypes),
  ...Object.keys(spaceProps.propTypes),
  ...Object.keys(typography.propTypes),
  'value',
];
export const PlainTextComponent = props => {
  const displayString = getDisplayValue(props);
  return (
    <div {...removeSomeProps(props, propsToTrim)}>
      <div>{displayString}</div>
    </div>
  );
};
 
PlainTextComponent.propTypes = {
  ...control.propTypes,
  ...typography.propTypes,
  value: PropTypes.any,
};
 
PlainTextComponent.defaultProps = {
  value: null,
};