All files / modules/10-common/components/ReferenceSelect ReferenceSelect.tsx

84.85% Statements 28/33
48.39% Branches 15/31
63.64% Functions 7/11
84.38% Lines 27/32

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              631x 631x                     631x   631x 631x   631x 631x 631x                                                                       631x 736x 736x 736x                                                           631x                               729x 729x 729x 197x     729x                       6x     6x                                                 2x 2x                                     500x 500x                     631x 542x 542x                  
/*
 * Copyright 2022 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import React from 'react'
import {
  Button,
  ExpressionAndRuntimeTypeProps,
  ExpressionAndRuntimeType,
  MultiTypeInputValue,
  Layout,
  Text,
  FixedTypeComponentProps,
  MultiTypeInputType,
  ButtonVariation
} from '@wings-software/uicore'
import { FontVariation } from '@harness/design-system'
 
import { Classes, Dialog } from '@blueprintjs/core'
import cx from 'classnames'
import type { Scope } from '@common/interfaces/SecretsInterface'
import { useStrings } from 'framework/strings'
import { EntityReferenceProps, EntityReference } from '../EntityReference/EntityReference'
import css from './ReferenceSelect.module.scss'
export interface MinimalObject {
  identifier?: string
  name?: string
}
export interface Item {
  label: string
  value: string
  scope: Scope
}
 
export interface ReferenceSelectDialogTitleProps {
  componentName?: string
  createNewLabel?: string
  createNewHandler?: () => void
  isNewConnectorLabelVisible?: boolean
}
export interface ReferenceSelectProps<T extends MinimalObject>
  extends Omit<EntityReferenceProps<T>, 'onSelect'>,
    ReferenceSelectDialogTitleProps {
  name: string
  placeholder: string
  selectAnReferenceLabel: string
  selected?: Item
  createNewLabel?: string
  createNewHandler?: () => void
  hideModal?: boolean
  selectedRenderer?: JSX.Element
  editRenderer?: JSX.Element
  width?: number
  isNewConnectorLabelVisible?: boolean
  onChange: (record: T, scope: Scope) => void
  disabled?: boolean
  componentName?: string
}
 
export const ReferenceSelectDialogTitle = (props: ReferenceSelectDialogTitleProps): JSX.Element => {
  const { getString } = useStrings()
  const { componentName, createNewHandler, createNewLabel } = props
  return (
    <Layout.Horizontal flex={{ distribution: 'space-between' }}>
      <Layout.Vertical spacing="xsmall">
        <Text font={{ variation: FontVariation.H4 }}>
          {getString('common.entityReferenceTitle', {
            compName: componentName
          })}
        </Text>
      </Layout.Vertical>
 
      {createNewLabel &&
        createNewHandler &&
        (props.isNewConnectorLabelVisible === undefined ? true : props.isNewConnectorLabelVisible) && (
          <>
            <Layout.Horizontal className={Classes.POPOVER_DISMISS}>
              <Button
                variation={ButtonVariation.SECONDARY}
                onClick={() => {
                  props.createNewHandler?.()
                }}
                text={`+ ${createNewLabel}`}
                margin={{ right: 'small' }}
              ></Button>
            </Layout.Horizontal>
          </>
        )}
    </Layout.Horizontal>
  )
}
 
export function ReferenceSelect<T extends MinimalObject>(props: ReferenceSelectProps<T>): JSX.Element {
  const {
    name,
    placeholder,
    selected,
    onChange,
    width = 300,
    createNewLabel,
    createNewHandler,
    isNewConnectorLabelVisible = true,
    editRenderer,
    hideModal = false,
    selectedRenderer,
    componentName = '',
    disabled,
    ...referenceProps
  } = props
  const [isOpen, setOpen] = React.useState(false)
  React.useEffect(() => {
    isOpen && setOpen(!hideModal) //this will hide modal if hideModal changes to true in open state
  }, [hideModal])
 
  return (
    <>
      <Button
        minimal
        data-testid={`cr-field-${name}`}
        className={css.container}
        style={{ width }}
        withoutCurrentColor={true}
        rightIcon="chevron-down"
        iconProps={{ size: 14 }}
        disabled={disabled}
        onClick={e => {
          Iif (disabled) {
            e.preventDefault()
          } else {
            setOpen(true)
          }
        }}
      >
        {selected ? selectedRenderer || selected.label : <span className={css.placeholder}>{placeholder}</span>}
      </Button>
      <Dialog
        isOpen={isOpen}
        enforceFocus={false}
        canEscapeKeyClose
        canOutsideClickClose
        onClose={() => setOpen(false)}
        className={cx(css.referenceSelect, css.dialog)}
        title={ReferenceSelectDialogTitle({
          componentName,
          createNewLabel,
          createNewHandler,
          isNewConnectorLabelVisible
        })}
      >
        <div className={cx(css.contentContainer)}>
          {editRenderer && selected && selected.value && <Layout.Horizontal>{editRenderer}</Layout.Horizontal>}
          <EntityReference<T>
            {...referenceProps}
            onSelect={(record, scope) => {
              setOpen(false)
              onChange(record, scope)
            }}
            onCancel={() => {
              setOpen(false)
            }}
            renderTabSubHeading
          />
        </div>
      </Dialog>
    </>
  )
}
export interface MultiTypeReferenceInputProps<T extends MinimalObject>
  extends Omit<ExpressionAndRuntimeTypeProps, 'fixedTypeComponent' | 'fixedTypeComponentProps'> {
  referenceSelectProps: Omit<ReferenceSelectProps<T>, 'onChange'>
}
function MultiTypeReferenceInputFixedTypeComponent<T extends MinimalObject>(
  props: FixedTypeComponentProps & MultiTypeReferenceInputProps<T>['referenceSelectProps']
): React.ReactElement {
  const { onChange, selected, width = 300, ...restProps } = props
  return (
    <ReferenceSelect
      {...restProps}
      selected={selected}
      width={width}
      onChange={(record, scope) => {
        onChange?.({ record, scope } as any, MultiTypeInputValue.SELECT_OPTION, MultiTypeInputType.FIXED)
      }}
    />
  )
}
export function MultiTypeReferenceInput<T extends MinimalObject>(props: MultiTypeReferenceInputProps<T>): JSX.Element {
  const { referenceSelectProps, ...rest } = props
  return (
    <ExpressionAndRuntimeType<MultiTypeReferenceInputProps<T>['referenceSelectProps']>
      width={referenceSelectProps.width}
      {...rest}
      fixedTypeComponentProps={referenceSelectProps}
      fixedTypeComponent={MultiTypeReferenceInputFixedTypeComponent}
    />
  )
}