All files / elements/typography/codeElements component.js

100% Statements 9/9
100% Branches 2/2
100% Functions 2/2
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 34 35 36 37 38 39 40 41 42              19x       2x 2x 1x   1x     19x         2x                 19x         19x        
import React from 'react';
import PropTypes from 'prop-types';
import reactElementToJSXString from 'react-element-to-jsx-string';
import SyntaxHighlighter from 'react-syntax-highlighter';
import { removeSomeProps } from 'src/utils/componentHelpers';
import { codePropTypes, grapeUICustomStyle } from './utils';
 
const getCode = props => {
  const {
    code,
    codeOptions,
  } = props;
  if (React.isValidElement(code)) {
    return reactElementToJSXString(code, codeOptions);
  }
  return code;
};
 
export const CodeComponent = ({
  language,
  style,
  ...props
}) => (
  <SyntaxHighlighter
    language={language}
    style={style}
    {...removeSomeProps(props, Object.keys(codePropTypes))}
  >
    {getCode(props)}
  </SyntaxHighlighter>
);
 
CodeComponent.propTypes = {
  language: PropTypes.string,
  style: PropTypes.any,
};
 
CodeComponent.defaultProps = {
  language: 'javascript',
  style: grapeUICustomStyle,
};