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

92.59% Statements 25/27
100% Branches 20/20
50% Functions 2/4
92.59% Lines 25/27

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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320                                10x         10x                                         2x                   10x                 2x               2x 1x                                         23x               23x           23x                                   23x     4x   13x                   2x                   4x                                                         23x   23x       23x               23x                                                                                                                             2x                                         2x                     2x 2x                                             2x         2x        
// 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, Fragment } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import SvgIcon from '../../shared/svg-icon';
import { ButtonGroup } from '../button-groups/base/example';
import ButtonIcon from '../button-icons/';
import { Spinner } from '../spinners/base/example';
 
/**
 * ActionsMenu - controls the display of action icons in the title card
 * iconColor - sets the color of the action icons
 */
const ActionsMenu = props => {
  const buttonIconClassNames = classNames(
    'slds-button_icon slds-button_icon-x-small',
    props.iconColor === 'white' && 'slds-button_icon-inverse'
  );
 
  return (
    <div className="slds-file__actions-menu">
      <ButtonGroup>
        <ButtonIcon
          className={buttonIconClassNames}
          symbol="download"
          assistiveText="Download"
          title="Download"
        />
        <ButtonIcon
          className={buttonIconClassNames}
          symbol="down"
          aria-haspopup="true"
          assistiveText="More Actions"
          title="More Actions"
        />
      </ButtonGroup>
    </div>
  );
};
 
ActionsMenu.propTypes = {
  iconColor: PropTypes.oneOf(['white', 'black'])
};
 
/**
 * ActionsConditional - controls the display of a wrapper div for actions
 * scrim - triggers a gradient background behind the action items when used without a title
 * iconColor - sets the color of the action icons
 */
const ActionsConditional = props => {
  return props.scrim ? (
    <div className="slds-file__title slds-file__title_scrim">
      <ActionsMenu iconColor={props.iconColor} />
    </div>
  ) : (
    <ActionsMenu iconColor={props.iconColor} />
  );
};
 
ActionsConditional.propTypes = {
  iconColor: PropTypes.oneOf(['white', 'black']),
  scrim: PropTypes.bool
};
 
/**
 * ExternalIcon - controls the display of the Salesforce logo
 */
const ExternalIcon = () => (
  <div className="slds-file__external-icon">
    <span className="slds-file__icon slds-icon_container" title="salesforce1">
      <SvgIcon
        className="slds-icon slds-icon-text-default"
        sprite="utility"
        symbol="salesforce1"
      />
      <span className="slds-assistive-text">Data provided by: salesforce1</span>
    </span>
  </div>
);
 
/// ////////////////////////////////////////
// Exports
/// ////////////////////////////////////////
 
/**
 * File component
 */
export class File extends Component {
  constructor(props) {
    super(props);
 
    /**
     * State
     * hasCaption - determines if a title should be rendered
     * hasIcon - determines if an icon should be rendered within the title card
     * hasTitle - determines if an additional class should be added for hover states on Files with a title card. This does not include Files with the `slds-file__title_overlay` class.
     */
    this.state = {
      hasCaption: !this.props.noCaption,
      hasIcon: this.props.symbol,
      hasTitle: this.props.hasTitleCard && !this.props.hasOverlay
    };
 
    this.fileContents = this.fileContents.bind(this);
  }
 
  /**
   * fileContents - Determines what media to render within a File component
   * isLoading - renders Spinner
   * hasImage - renders a landscape-oriented image
   * hasImagePortrait - renders a portrait-oriented image
   * default - renders an icon
   */
  fileContents() {
    const {
      hasImage,
      hasImagePortrait,
      isLoading,
      sprite,
      symbol,
      title
    } = this.props;
 
    if (isLoading) {
      return <Spinner className="slds-spinner_medium" />;
    } else if (hasImage) {
      return (
        <Fragment>
          <span className="slds-assistive-text">Preview:</span>
          <img
            src="/assets/images/placeholder-img@16x9.jpg"
            alt="Description of the image"
          />
        </Fragment>
      );
    } else if (hasImagePortrait) {
      return (
        <Fragment>
          <span className="slds-assistive-text">Preview:</span>
          <img
            src="/assets/images/placeholder-img@9x16.jpg"
            alt="Description of the image"
          />
        </Fragment>
      );
    } else {
      return (
        <Fragment>
          <span className="slds-assistive-text">Preview:</span>
          <span className="slds-file__icon slds-icon_container" title={symbol}>
            <SvgIcon className="slds-icon" sprite={sprite} symbol={symbol} />
            <span className="slds-assistive-text">{title}</span>
          </span>
        </Fragment>
      );
    }
  }
 
  render() {
    const {
      has16x9Crop,
      has1x1Crop,
      has4x3Crop,
      hasActions,
      hasCrop,
      hasImagePortrait,
      hasOverlay,
      hasScrim,
      hasTitleCard,
      iconColor,
      isCard,
      isExternalSource,
      isLoading,
      symbol,
      title
    } = this.props;
 
    const { hasCaption, hasIcon, hasTitle } = this.state;
 
    // If no crop prop is passed, default to `slds-file__figure`
    const figureClasses =
      classNames({
        'slds-file__crop': hasCrop,
        'slds-file__crop slds-file__crop_16-by-9': has16x9Crop,
        'slds-file__crop slds-file__crop_4-by-3': has4x3Crop,
        'slds-file__crop slds-file__crop_1-by-1': has1x1Crop,
        'slds-file__figure slds-file__figure_portrait': hasImagePortrait
      }) || 'slds-file__figure';
 
    return (
      <div
        className={classNames('slds-file', {
          'slds-file_card': isCard,
          'slds-file_loading': isLoading,
          'slds-has-title': hasTitle
        })}
      >
        <figure>
          {/*
            aXe is reporting an a11y violation where it says the <a> does not detect any discernible text when using <Spinner>, despite this not being the case (Spinner has "Loading" in .slds-assistive-text). To prevent this from breaking the build, we're excluding `.slds-file figure > a` from aXe linting. Tread carefully.
          */}
          <a
            href="#"
            className={figureClasses}
            onClick={e => e.preventDefault()}
          >
            {hasOverlay && <div className="slds-file_overlay" />}
 
            {this.fileContents()}
          </a>
          {hasCaption && (
            <figcaption
              className={classNames('slds-file__title', {
                'slds-file__title_card': hasTitleCard,
                'slds-file__title_overlay slds-align_absolute-center slds-text-heading_large': hasOverlay,
                'slds-file-has-actions': hasActions
              })}
            >
              <div className="slds-media slds-media_small slds-media_center">
                <div className="slds-media__figure slds-line-height_reset">
                  {hasIcon && (
                    <span className="slds-icon_container" title={symbol}>
                      <SvgIcon
                        className="slds-icon slds-icon_x-small"
                        sprite="doctype"
                        symbol={symbol}
                      />
                      <span className="slds-assistive-text">{symbol}</span>
                    </span>
                  )}
                </div>
                <div className="slds-media__body">
                  <span className="slds-file__text slds-truncate" title={title}>
                    {title}
                    {hasOverlay && (
                      <span className="slds-assistive-text">more files</span>
                    )}
                  </span>
                </div>
              </div>
            </figcaption>
          )}
        </figure>
        {isExternalSource && <ExternalIcon />}
        {hasActions && (
          <ActionsConditional scrim={hasScrim} iconColor={iconColor} />
        )}
      </div>
    );
  }
}
 
File.propTypes = {
  has16x9Crop: PropTypes.bool,
  has1x1Crop: PropTypes.bool,
  has4x3Crop: PropTypes.bool,
  hasActions: PropTypes.bool,
  hasCrop: PropTypes.bool,
  hasImage: PropTypes.bool,
  hasImagePortrait: PropTypes.bool,
  hasOverlay: PropTypes.bool,
  hasScrim: PropTypes.bool,
  hasTitleCard: PropTypes.bool,
  iconColor: PropTypes.oneOf(['white', 'black']),
  isCard: PropTypes.bool,
  isExternalSource: PropTypes.bool,
  isLoading: PropTypes.bool,
  noCaption: PropTypes.bool,
  sprite: PropTypes.string,
  symbol: PropTypes.string,
  title: PropTypes.string
};
 
File.defaultProps = {
  iconColor: 'black',
  sprite: 'doctype',
  title: 'Image Title'
};
 
/**
 * AttachmentLink
 * articleTitle
 * articleDescription
 */
export const AttachmentLink = props => (
  <a
    href="#"
    className="slds-media slds-box slds-grow slds-text-link_reset"
    onClick={e => e.preventDefault()}
  >
    <div className="slds-media__figure slds-medium-show">
      <div className="slds-file slds-size_small">
        <figure className="slds-file__crop slds-file__crop_16-by-9">
          <img
            src="/assets/images/placeholder-img@16x9.jpg"
            alt="Description of the image"
          />
        </figure>
      </div>
    </div>
    <div className="slds-media__body">
      <h3 className="slds-text-heading_small">{props.articleTitle}</h3>
      <p>{props.articleDescription}</p>
      <span className="slds-text-body_small">{props.articleTitle}</span>
    </div>
  </a>
);
 
AttachmentLink.propTypes = {
  articleDescription: PropTypes.string,
  articleTitle: PropTypes.string
};
 
AttachmentLink.defaultProps = {
  articleDescription: 'Article Description',
  articleTitle: 'Article Title'
};