All files / packages/design-system/ui/components/form-element/record-detail index.jsx

94.73% Statements 18/19
96.29% Branches 26/27
88.88% Functions 8/9
94.73% Lines 18/19

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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182                      3x 43x     3x       3x 70x         3x                                   51x   50x                                           18x                     6x                                           3x                                           9x 9x                   31x   49x                                               11x   20x                     3x                  
// Copyright (c) 2015-present, salesforce.com, inc. All rights reserved
// Licensed under BSD 3-Clause - see LICENSE.txt or git.io/sfdc-license
 
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import ButtonIcon from '../../button-icons/';
import { FormElement, FormElementStatic } from '../';
import { Avatar } from '../../avatar/base/example';
import classNames from 'classnames';
import _ from '../../../shared/helpers';
 
export const RecordDetailRow = props => (
  <div className="slds-form__row">{props.children}</div>
);
 
RecordDetailRow.propTypes = {
  children: PropTypes.node
};
 
export const RecordDetailItem = props => (
  <div className="slds-form__item" role="listitem">
    {props.children}
  </div>
);
 
RecordDetailItem.propTypes = {
  children: PropTypes.node
};
 
export const RecordDetailField = props => {
  const {
    isRequired,
    hasTooltip,
    type,
    label,
    value,
    avatar,
    link,
    timestamp,
    hasInlineEdit,
    isHorizontal,
    isStacked,
    column
  } = props;
 
  return (
    <FormElement
      isViewMode
      labelContent={label}
      isEditable={hasInlineEdit}
      hasHint
      isRequired={isRequired}
      hasTooltip={hasTooltip}
      isHorizontal={isHorizontal}
      isStacked={isStacked}
      column={column}
    >
      <FormElementStatic isLongform={type === 'textarea'}>
        {avatar && (
          <Avatar className="slds-avatar_x-small slds-avatar_circle slds-m-right_xx-small">
            <img alt={value} src={avatar} title={value} />
          </Avatar>
        )}
        {link ? (
          <a href="#" onClick={e => e.preventDefault()}>
            {typeof value === 'object'
              ? value.map(option => (
                  <React.Fragment key={option}>
                    {option}
                    <br />
                  </React.Fragment>
                ))
              : value}
          </a>
        ) : typeof value === 'object' &&
          type !== 'richtext' &&
          type !== 'checkbox' ? (
          value.map(option => (
            <React.Fragment key={option}>
              {option}
              <br />
            </React.Fragment>
          ))
        ) : (
          value
        )}
        {timestamp && <React.Fragment>, {timestamp}</React.Fragment>}
      </FormElementStatic>
      {hasInlineEdit && (
        <ButtonIcon
          iconClassName="slds-button__icon_hint"
          symbol="edit"
          assistiveText={`Edit: ${label}`}
          title={`Edit: ${label}`}
        />
      )}
    </FormElement>
  );
};
 
RecordDetailField.propTypes = {
  isRequired: PropTypes.bool,
  hasTooltip: PropTypes.bool,
  type: PropTypes.string,
  label: PropTypes.string,
  avatar: PropTypes.string,
  link: PropTypes.bool,
  timestamp: PropTypes.string,
  hasInlineEdit: PropTypes.bool,
  isHorizontal: PropTypes.bool,
  isStacked: PropTypes.bool,
  column: PropTypes.number
};
 
class RecordDetail extends Component {
  render() {
    const {
      snapshot,
      isViewMode,
      direction,
      hasInlineEdit,
      isDeprecated
    } = this.props;
    return (
      <div
        className={classNames(
          'slds-form',
          isDeprecated && `slds-form_${direction}`
        )}
        role="list"
      >
        {isViewMode
          ? snapshot.rows.map(row => (
              <RecordDetailRow key={_.uniqueId(row.fields.length)}>
                {Object.keys(row.fields).map(field => (
                  <RecordDetailItem key={row.fields[field].label}>
                    {row.fields[field].type && (
                      <RecordDetailField
                        type={row.fields[field].type}
                        label={row.fields[field].label}
                        value={row.fields[field].value}
                        isRequired={row.fields[field].isRequired}
                        hasTooltip={row.fields[field].hasTooltip}
                        avatar={row.fields[field].avatar}
                        link={row.fields[field].link}
                        timestamp={row.fields[field].timestamp}
                        hasInlineEdit={hasInlineEdit}
                        isHorizontal={
                          !isDeprecated && direction === 'horizontal'
                        }
                        isStacked={!isDeprecated && direction === 'stacked'}
                        column={row.fields[field].column}
                      />
                    )}
                  </RecordDetailItem>
                ))}
              </RecordDetailRow>
            ))
          : snapshot.rows.map((row, i) => (
              <RecordDetailRow key={_.uniqueId(`detail-row-${i}-`)}>
                {Object.keys(row.fields).map(field => (
                  <RecordDetailItem key={row.fields[field].label}>
                    {row.fields[field].component && row.fields[field].component}
                  </RecordDetailItem>
                ))}
              </RecordDetailRow>
            ))}
      </div>
    );
  }
}
 
RecordDetail.propTypes = {
  snapshot: PropTypes.object,
  isViewMode: PropTypes.bool,
  direction: PropTypes.string,
  hasInlineEdit: PropTypes.bool,
  isDeprecated: PropTypes.bool
};
 
export default RecordDetail;