All files / packages/design-system/ui/components/data-tables/advanced index.jsx

100% Statements 26/26
100% Branches 18/18
100% Functions 5/5
100% Lines 26/26

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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252                                                  12x 11x                                                                                                                   1x 1x                                 1x             1x 5x               1x 1x               1x 3x             1x 1x               1x 4x                     12x                       1x 1x                     6x   5x                               1x 1x                 1x 2x                                                           1x 1x                      
// 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, { useContext } from 'react';
import PropTypes from 'prop-types';
import { IsDependentOn } from '../../../shared/helpers';
 
import Score from '../../dynamic-icons/score';
import MediaObject from '../../../utilities/media-objects/index.react';
import Input from '../../input/';
import { FormElement } from '../../form-element';
import {
  TBodyTr,
  Td,
  RowTh,
  SelectRowCell,
  ReadOnlyCell,
  RowActionsCell,
  DataTableContext
} from '../';
 
/**
 * @name AdvancedDataTableTr - Table row for advanced data table components
 */
export const AdvancedDataTableTr = props => {
  const { isActionableMode } = useContext(DataTableContext);
  return (
    <TBodyTr isSelected={props.isSelected}>
      <Td isRightAligned type="advanced">
        <SelectRowCell
          checked={props.isSelected}
          hasSingleRowSelect={props.hasSingleRowSelect}
          index={props.index}
          inputTabIndex={isActionableMode ? '0' : '-1'}
        />
      </Td>
      <RowTh
        hasFocus={!isActionableMode && props.index === 1 && props.hasFocus}
        tabIndex={!isActionableMode && props.index === 1 ? '0' : null}
      >
        <ReadOnlyCell cellLink cellText={props.recordName} />
      </RowTh>
      <Td type="advanced">
        <ReadOnlyCell cellText={props.accountName} />
      </Td>
      <Td type="advanced">
        <ReadOnlyCell cellText={props.closeDate} />
      </Td>
      <Td type="advanced">
        <ReadOnlyCell cellText={props.stage} />
      </Td>
      <Td type="advanced">
        <ReadOnlyCell cellText={props.confidence} />
      </Td>
      {props.hasScores && props.amountScore && props.amountScoreLabel ? (
        <Td type="advanced">
          <div className="slds-grid slds-grid_vertical-align-center">
            <div className="slds-truncate" title={props.amount}>
              {props.amount}
            </div>
            <div className="slds-icon_container slds-m-left_x-small slds-m-right_xx-small">
              <Score data-slds-state={props.amountScore} />
            </div>
            <div className="slds-truncate" title={props.amountScoreLabel}>
              {props.amountScoreLabel}
            </div>
          </div>
        </Td>
      ) : (
        <Td type="advanced">
          <ReadOnlyCell cellText={props.amount} />
        </Td>
      )}
      <Td type="advanced">
        <ReadOnlyCell cellLink cellText={props.contact} />
      </Td>
      {props.hasRowActions && (
        <Td type="advanced">
          <RowActionsCell rowName={props.recordName} />
        </Td>
      )}
    </TBodyTr>
  );
};
AdvancedDataTableTr.displayName = 'AdvancedDataTableTr';
AdvancedDataTableTr.propTypes = {
  accountName: PropTypes.string.isRequired,
  amount: PropTypes.string.isRequired,
  amountScore: IsDependentOn('amountScoreLabel', PropTypes.string),
  amountScoreLabel: IsDependentOn('amountScore', PropTypes.string),
  closeDate: PropTypes.string.isRequired,
  confidence: PropTypes.string.isRequired,
  contact: PropTypes.string.isRequired,
  hasFocus: PropTypes.bool,
  hasRowActions: PropTypes.bool,
  hasScores: PropTypes.bool,
  index: PropTypes.number.isRequired,
  hasSingleRowSelect: PropTypes.bool,
  recordName: PropTypes.string.isRequired,
  isSelected: PropTypes.bool,
  stage: PropTypes.string.isRequired
};
AdvancedDataTableTr.defaultProps = {
  hasRowActions: true
};
 
/**
 * @name ProductImage - A common cell container for product image
 */
export const ProductImage = props => (
  <div className="slds-size_xx-small">
    <img
      alt={props.productName}
      src={props.productImgSrc}
      title={props.productName}
    />
  </div>
);
ProductImage.displayName = 'ProductImage';
ProductImage.propTypes = {
  productImgSrc: PropTypes.string.isRequired,
  productName: PropTypes.string.isRequired
};
 
/**
 * @name ProductPriceCell - A common cell container for product price
 */
export const ProductPriceCell = props => (
  <React.Fragment>
    <p>
      <s>{props.priceOriginal}</s>
    </p>
    <p>{props.priceDiscount}</p>
  </React.Fragment>
);
ProductPriceCell.displayName = 'ProductPriceCell';
ProductPriceCell.propTypes = {
  priceDiscount: PropTypes.string.isRequired,
  priceOriginal: PropTypes.string.isRequired
};
 
/**
 * @name ProductItemDetailsCell - A common cell container for product details
 */
export const ProductItemDetailsCell = props => (
  <MediaObject
    figureLeft={
      <ProductImage
        productImgSrc={props.productImgSrc}
        productName={props.productName}
      />
    }
  >
    <ReadOnlyCell cellLink cellText={props.productName} />
    <ul>
      {props.productProperties.map((property, i) => (
        <li
          className="slds-truncate"
          key={i}
          title={`${property.label}: ${property.value}`}
        >
          {property.label}: <strong>{property.value}</strong>
        </li>
      ))}
    </ul>
    <p className="slds-text-color_success">{props.labelInventory}</p>
  </MediaObject>
);
ProductItemDetailsCell.displayName = 'ProductItemDetailsCell';
ProductItemDetailsCell.propTypes = {
  labelInventory: PropTypes.string.isRequired,
  productImgSrc: PropTypes.string.isRequired,
  productName: PropTypes.string.isRequired,
  productProperties: PropTypes.array.isRequired
};
 
/**
 * @name ProductQuantityCell - A common cell container for product quantity field
 */
export const ProductQuantityCell = props => {
  const { isActionableMode } = useContext(DataTableContext);
 
  return (
    <FormElement
      inputId={props.inputId}
      labelClassName="slds-assistive-text"
      labelContent={props.labelText}
    >
      <Input
        className="slds-size_xxx-small slds-text-align_center slds-p-horizontal_x-small"
        defaultValue={props.quantity}
        id={props.inputId}
        placeholder=" "
        tabIndex={!isActionableMode ? '-1' : null}
      />
    </FormElement>
  );
};
ProductQuantityCell.displayName = 'ProductQuantityCell';
ProductQuantityCell.propTypes = {
  labelText: PropTypes.string.isRequired,
  inputId: PropTypes.string.isRequired,
  quantity: PropTypes.string
};
 
/**
 * @name ProductDataTableTr - Table row for advanced data table components
 */
export const ProductDataTableTr = props => (
  <TBodyTr isTopAligned>
    <RowTh>
      <ProductItemDetailsCell
        labelInventory={props.labelInventory}
        productImgSrc={props.productImgSrc}
        productName={props.productName}
        productProperties={props.productProperties}
      />
    </RowTh>
    <Td type="advanced">
      <ProductQuantityCell
        inputId={`product-quantity-text-input-id-${props.index}`}
        labelText={`${props.productName} quantity`}
        quantity={props.quantity}
      />
    </Td>
    <Td type="advanced">
      <ReadOnlyCell cellText={props.dateAdded} />
    </Td>
    <Td type="advanced">
      <ProductPriceCell
        priceDiscount={props.priceDiscount}
        priceOriginal={props.priceOriginal}
      />
    </Td>
    <Td type="advanced">
      <RowActionsCell rowName={props.productName} />
    </Td>
  </TBodyTr>
);
ProductDataTableTr.displayName = 'ProductDataTableTr';
ProductDataTableTr.propTypes = {
  dateAdded: PropTypes.string.isRequired,
  index: PropTypes.number.isRequired,
  labelInventory: PropTypes.string.isRequired,
  priceDiscount: PropTypes.string.isRequired,
  priceOriginal: PropTypes.string.isRequired,
  productImgSrc: PropTypes.string.isRequired,
  productName: PropTypes.string.isRequired,
  productProperties: PropTypes.array.isRequired,
  quantity: PropTypes.string
};