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

92.31% Statements 12/13
63.64% Branches 14/22
50% Functions 1/2
92.31% Lines 12/13

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              119x   119x 119x               119x 119x 119x                                                         119x                     519x   519x   519x   519x                                                         119x  
/*
 * Copyright 2021 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 type { IFormGroupProps } from '@blueprintjs/core'
import { connect, FormikContext } from 'formik'
import {
  FormInput,
  getMultiTypeFromValue,
  MultiTypeInputType,
  MultiTextInputProps,
  DataTooltipInterface,
  HarnessDocTooltip
} from '@wings-software/uicore'
import { get } from 'lodash-es'
import { useStrings } from 'framework/strings'
import { ConfigureOptions, ConfigureOptionsProps } from '@common/components/ConfigureOptions/ConfigureOptions'
 
// TODO: Need to import from uikit but right now it is not being exported from there
// Also, we need to make field label to be a type of string | ReactElement because sometimes we need to pass an element
interface MultiTextTypeInputProps extends Omit<IFormGroupProps, 'label' | 'labelFor'> {
  name: string
  label: string
  placeholder?: string
  onChange?: MultiTextInputProps['onChange']
  multiTextInputProps?: Omit<MultiTextInputProps, 'name'>
}
 
interface MultiTypeTextConfigureOptionsProps
  extends Omit<ConfigureOptionsProps, 'value' | 'type' | 'variableName' | 'onChange'> {
  variableName?: ConfigureOptionsProps['variableName']
}
 
export interface MultiTypeTextProps {
  className?: string
  name: string
  label: string | React.ReactElement
  formik?: FormikContext<any>
  multiTextInputProps?: Omit<MultiTextTypeInputProps, 'name' | 'label'>
  enableConfigureOptions?: boolean
  configureOptionsProps?: MultiTypeTextConfigureOptionsProps
  style?: React.CSSProperties
  tooltipProps?: DataTooltipInterface
}
 
export function MultiTypeText(props: MultiTypeTextProps): React.ReactElement {
  const {
    className,
    label,
    name,
    formik,
    multiTextInputProps,
    enableConfigureOptions = true,
    configureOptionsProps,
    style,
    tooltipProps
  } = props
 
  const value = get(formik?.values, name, '')
 
  const { getString } = useStrings()
 
  return (
    <div className={className} style={style}>
      {label ? <HarnessDocTooltip tooltipId={tooltipProps?.dataTooltipId} labelText={label} /> : label}
      <div style={{ display: 'flex', alignItems: 'center' }}>
        <FormInput.MultiTextInput
          name={name}
          label=""
          style={{ marginBottom: 0, flexGrow: 1 }}
          {...multiTextInputProps}
        />
        {enableConfigureOptions && getMultiTypeFromValue(value) === MultiTypeInputType.RUNTIME && (
          <ConfigureOptions
            value={value}
            type={getString('string')}
            variableName={name}
            showRequiredField={false}
            showDefaultField={false}
            showAdvanced={true}
            onChange={val => formik?.setFieldValue(name, val)}
            style={{ marginLeft: 'var(--spacing-medium)', marginBottom: 12 }}
            {...configureOptionsProps}
            isReadonly={multiTextInputProps?.disabled}
          />
        )}
      </div>
    </div>
  )
}
 
export const MultiTypeTextField = connect(MultiTypeText)