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

97.29% Statements 36/37
100% Branches 33/33
100% Functions 6/6
97.22% Lines 35/36

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 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420                                              1x               30x 30x   30x                                                                                       1x                           1x           3x   3x                 1x       13x   1x       1x 34x                   1x           1x 87x                 1x                                 21x 21x 21x 21x 21x   21x                                                                                                                                                                                                                                                             1x                                         1x         1x 10x                           1x                           1x       1x 22x                                           1x       1x 12x                                       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 from 'react';
import PropTypes from 'prop-types';
import { UtilityIcon } from '../icons/base/example';
import SvgIcon from '../../shared/svg-icon';
import ButtonIcon from '../button-icons/';
import { Button } from '../buttons/base/example';
import Combobox from '../combobox/';
import Listbox from '../combobox/listbox/';
import {
  FormElement,
  SimpleFormElementWrapper,
  FormElementSpanFauxLabel,
  FormElementControl
} from '../form-element';
import Input from '../input/';
import * as Snapshot from './snapshots.data';
import classNames from 'classnames';
import _ from '../../shared/helpers';
 
// Private
const placeholderText = 'Select…';
 
export const ExpressionOptions = ({
  optionSelected,
  isInFilter,
  label,
  placeholderText
}) => {
  const listboxId = _.uniqueId('listbox-id-');
  const selected = optionSelected && Snapshot.OptionSelected[optionSelected];
 
  return (
    <div
      className={classNames(
        isInFilter ? 'slds-m-vertical_small' : 'slds-expression__options'
      )}
    >
      <Combobox
        id={_.uniqueId('combobox-id-')}
        aria-controls={listboxId}
        label={label}
        value={selected && selected.value}
        placeholder={placeholderText}
        selectOnly
        inputIconPosition="right"
        rightInputIcon={
          <UtilityIcon
            symbol="down"
            className="slds-icon slds-icon_x-small slds-icon-text-default"
            containerClassName="slds-input__icon slds-input__icon_right"
            assistiveText={false}
            title={false}
          />
        }
        results={
          <Listbox
            id={listboxId}
            hasUniqueId
            snapshot={selected ? selected.snapshot : Snapshot.ConditionsOptions}
            type="plain"
            count={
              selected
                ? Object.keys(selected.snapshot).length
                : Object.keys(Snapshot.ConditionsOptions).length
            }
            visualSelection
          />
        }
        resultsType="listbox"
        hasInteractions
      />
    </div>
  );
};
 
ExpressionOptions.propTypes = {
  label: PropTypes.string.isRequired,
  isInFilter: PropTypes.bool,
  optionSelected: PropTypes.oneOf([
    'any',
    'all',
    'custom',
    'formula',
    'always',
    'group'
  ]),
  placeholderText: PropTypes.string
};
 
ExpressionOptions.defaultProps = {
  label: 'Take Action When',
  placeholderText: placeholderText
};
 
export const ExpressionCustom = props => {
  const inputId = _.uniqueId('text-input-id-');
 
  return (
    <div className="slds-expression__custom-logic">
      <FormElement labelContent="Custom Logic" inputId={inputId}>
        <Input id={inputId} defaultValue={props.defaultValue} />
      </FormElement>
    </div>
  );
};
 
ExpressionCustom.propTypes = {
  defaultValue: PropTypes.string
};
 
export const ExpressionList = props => <ul>{props.children}</ul>;
 
ExpressionList.propTypes = {
  children: PropTypes.node
};
 
export const ExpressionLegend = props => (
  <legend
    className={classNames('slds-expression__legend', {
      'slds-expression__legend_group': props.isGroup
    })}
  >
    {props.legendText && <span>{props.legendText}</span>}
    <span className="slds-assistive-text">{props.assistiveText}</span>
  </legend>
);
 
ExpressionLegend.propTypes = {
  isGroup: PropTypes.bool,
  legendText: PropTypes.string,
  assistiveText: PropTypes.string
};
 
export const ExpressionCol = props => (
  <div
    className={classNames('slds-col', {
      'slds-grow-none': props.doesNotGrow
    })}
  >
    {props.children}
  </div>
);
 
ExpressionCol.propTypes = {
  doesNotGrow: PropTypes.bool,
  children: PropTypes.node
};
 
export const ExpressionRow = ({
  isGroup,
  groupName,
  conditionName,
  placeholderText,
  resourceIsSelected,
  inputIsDisabled,
  hasError,
  errorMessage,
  buttonIsDisabled,
  legendText
}) => {
  const listboxId1 = _.uniqueId('listbox-id-');
  const listboxId2 = _.uniqueId('listbox-id-');
  const inputLabel = 'Value';
  const inputId = _.uniqueId('text-input-id-');
  const errorId = 'error-message-unique-id';
 
  return (
    <li
      className={classNames('slds-expression__row', {
        'slds-expression__row_group': isGroup
      })}
    >
      <fieldset>
        <ExpressionLegend
          legendText={legendText}
          assistiveText={
            isGroup ? conditionName + ' of ' + groupName : conditionName
          }
        />
        <div className="slds-grid slds-gutters_xx-small">
          <ExpressionCol>
            <Combobox
              id={_.uniqueId('combobox-id-')}
              aria-controls={listboxId1}
              label="Resource"
              placeholder={placeholderText}
              selectOnly
              inputIconPosition="right"
              value={resourceIsSelected && 'Resource 1'}
              formClassName="slds-has-error"
              errorId={errorId}
              rightInputIcon={
                <UtilityIcon
                  symbol="down"
                  className="slds-icon slds-icon_x-small slds-icon-text-default"
                  containerClassName="slds-input__icon slds-input__icon_right"
                  assistiveText={false}
                  title={false}
                />
              }
              results={
                <Listbox
                  id={listboxId1}
                  hasUniqueId
                  snapshot={
                    resourceIsSelected
                      ? Snapshot.ResourceSelected
                      : Snapshot.ResourceOptions
                  }
                  type="plain"
                  count={4}
                  visualSelection
                />
              }
              resultsType="listbox"
              hasInteractions
            />
          </ExpressionCol>
          <ExpressionCol doesNotGrow>
            <Combobox
              id={_.uniqueId('combobox-id-')}
              aria-controls={listboxId2}
              label="Operator"
              placeholder={placeholderText}
              selectOnly
              inputIconPosition="right"
              isDisabled={inputIsDisabled}
              rightInputIcon={
                <UtilityIcon
                  symbol="down"
                  className="slds-icon slds-icon_x-small slds-icon-text-default"
                  containerClassName="slds-input__icon slds-input__icon_right"
                  assistiveText={false}
                  title={false}
                />
              }
              results={
                <Listbox
                  id={listboxId2}
                  hasUniqueId
                  snapshot={Snapshot.OperatorOptions}
                  type="plain"
                  count={4}
                  visualSelection
                />
              }
              resultsType="listbox"
              hasInteractions
            />
          </ExpressionCol>
          <ExpressionCol>
            <FormElement
              hasError={hasError}
              labelContent={inputLabel}
              inputId={inputId}
              errorId={errorMessage && errorId}
              inlineMessage={errorMessage}
            >
              <Input
                disabled={inputIsDisabled}
                id={inputId}
                aria-describedby={errorMessage && errorId}
              />
            </FormElement>
          </ExpressionCol>
          <ExpressionCol doesNotGrow>
            <SimpleFormElementWrapper>
              <FormElementSpanFauxLabel>&nbsp;</FormElementSpanFauxLabel>
              <FormElementControl>
                <ButtonIcon
                  theme="neutral"
                  symbol="delete"
                  disabled={buttonIsDisabled}
                  assistiveText={
                    isGroup
                      ? 'Delete ' + conditionName + ' of ' + groupName
                      : 'Delete ' + conditionName
                  }
                  title={
                    isGroup
                      ? 'Delete ' + conditionName + ' of ' + groupName
                      : 'Delete ' + conditionName
                  }
                />
              </FormElementControl>
            </SimpleFormElementWrapper>
          </ExpressionCol>
        </div>
      </fieldset>
    </li>
  );
};
 
ExpressionRow.propTypes = {
  legendText: PropTypes.string,
  conditionName: PropTypes.string.isRequired,
  isGroup: PropTypes.bool,
  groupName: function(props, propName) {
    if (
      props.isGroup &&
      (props[propName] === undefined || typeof props[propName] !== 'string')
    ) {
      return new Error(
        "Please provide a string of this row's group name for assistive text."
      );
    }
  },
  inputIsDisabled: PropTypes.bool,
  buttonIsDisabled: PropTypes.bool,
  resourceIsSelected: PropTypes.bool,
  errorMessage: PropTypes.string,
  placeholderText: PropTypes.string
};
 
ExpressionRow.defaultProps = {
  conditionName: 'Condition 1',
  placeholderText: placeholderText
};
 
export const ExpressionGroup = props => (
  <li className="slds-expression__group">
    <fieldset>
      <ExpressionLegend
        legendText={props.legendText}
        assistiveText={props.assistiveText}
        isGroup
      />
      <ExpressionOptions optionSelected={props.optionSelected} />
      <ul>{props.children}</ul>
      <ExpressionButtons isGroup />
    </fieldset>
  </li>
);
 
ExpressionGroup.propTypes = {
  legendText: PropTypes.string,
  assistiveText: PropTypes.string.isRequired,
  optionSelected: PropTypes.oneOf([
    'any',
    'all',
    'custom',
    'formula',
    'always',
    'group'
  ]),
  children: PropTypes.node
};
 
ExpressionGroup.defaultProps = {
  assistiveText: 'Condition Group 1'
};
 
export const ExpressionButtons = props => (
  <div className="slds-expression__buttons">
    <Button isNeutral>
      <SvgIcon
        className="slds-button__icon slds-button__icon_left"
        sprite="utility"
        symbol="add"
      />
      Add Condition
    </Button>
    {!props.isGroup && (
      <Button isNeutral>
        <SvgIcon
          className="slds-button__icon slds-button__icon_left"
          sprite="utility"
          symbol="add"
        />
        Add Group
      </Button>
    )}
  </div>
);
 
ExpressionButtons.propTypes = {
  isGroup: PropTypes.bool
};
 
export const Expression = props => (
  <div className="slds-expression">
    <h2 className="slds-expression__title">Conditions</h2>
    <ExpressionOptions optionSelected={props.optionSelected} />
    {props.hasCustomLogic && (
      <ExpressionCustom defaultValue={props.customLogicValue} />
    )}
    <ExpressionList>
      <ExpressionRow
        legendText={props.legendText}
        assistiveText="Condition 1"
        inputIsDisabled={props.inputIsDisabled}
        buttonIsDisabled={props.buttonIsDisabled}
        resourceIsSelected={props.resourceIsSelected}
      />
      {props.children}
    </ExpressionList>
    <ExpressionButtons />
  </div>
);
 
Expression.propTypes = {
  hasCustomLogic: PropTypes.bool,
  legendText: PropTypes.string,
  inputIsDisabled: PropTypes.bool,
  buttonIsDisabled: PropTypes.bool,
  optionSelected: PropTypes.oneOf([
    'any',
    'all',
    'custom',
    'formula',
    'always'
  ]),
  resourceIsSelected: PropTypes.bool,
  customLogicValue: PropTypes.string,
  children: PropTypes.node
};