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

86.11% Statements 31/36
56.06% Branches 37/66
42.86% Functions 3/7
85.29% Lines 29/34

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              499x 499x 499x                         499x 499x 499x 499x 499x   499x   499x         118x 118x                                                 499x 165x 165x 165x     165x                       165x                                                                             499x                     165x 165x             165x   165x 165x                   165x   165x   165x 165x                                           499x  
/*
 * 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, { useState } from 'react'
import { FormGroup, IFormGroupProps, Intent, ITextAreaProps, TextArea } from '@blueprintjs/core'
import {
  ExpressionAndRuntimeType,
  ExpressionAndRuntimeTypeProps,
  MultiTypeInputValue,
  FixedTypeComponentProps,
  getMultiTypeFromValue,
  MultiTypeInputType,
  DataTooltipInterface,
  HarnessDocTooltip,
  FormError,
  FormikTooltipContext,
  Container
} from '@wings-software/uicore'
import { connect } from 'formik'
import { get } from 'lodash-es'
import cx from 'classnames'
import { useStrings } from 'framework/strings'
import { ConfigureOptions, ConfigureOptionsProps } from '@common/components/ConfigureOptions/ConfigureOptions'
 
import { errorCheck } from '@common/utils/formikHelpers'
 
import css from './MultiTypeTextArea.module.scss'
 
function MultiTypeTextAreaFixedTypeComponent(
  props: FixedTypeComponentProps & MultiTypeTextAreaProps['textAreaProps']
): React.ReactElement {
  const { onChange, value, ...restProps } = props
  return (
    <TextArea
      growVertically={false}
      {...restProps}
      className={cx(css.input, restProps.className)}
      value={value as string}
      onInput={(event: React.ChangeEvent<HTMLTextAreaElement>) => {
        onChange?.(event.target.value, MultiTypeInputValue.STRING, MultiTypeInputType.FIXED)
      }}
    />
  )
}
 
interface MultiTypeTextAreaConfigureOptionsProps
  extends Omit<ConfigureOptionsProps, 'value' | 'type' | 'variableName' | 'onChange'> {
  variableName?: ConfigureOptionsProps['variableName']
}
 
export interface MultiTypeTextAreaProps
  extends Omit<ExpressionAndRuntimeTypeProps, 'fixedTypeComponent' | 'fixedTypeComponentProps'> {
  textAreaProps?: Omit<ITextAreaProps, 'onChange' | 'value'>
  enableConfigureOptions?: boolean
  configureOptionsProps?: MultiTypeTextAreaConfigureOptionsProps
}
 
export const MultiTypeTextArea: React.FC<MultiTypeTextAreaProps> = props => {
  const { name, value, onChange, enableConfigureOptions = true, textAreaProps, configureOptionsProps, ...rest } = props
  const { getString } = useStrings()
  const [multiType, setMultiType] = useState<MultiTypeInputType>(getMultiTypeFromValue(value))
 
  const expressionAndRuntimeTypeComponent = (
    <ExpressionAndRuntimeType
      name={name}
      value={value}
      onChange={onChange}
      {...rest}
      fixedTypeComponentProps={textAreaProps}
      fixedTypeComponent={MultiTypeTextAreaFixedTypeComponent}
      style={{ flexGrow: 1 }}
      onTypeChange={setMultiType}
      btnClassName={multiType === MultiTypeInputType.FIXED ? css.multiButtonForFixedType : ''}
    />
  )
  return (
    <Container>
      {enableConfigureOptions ? (
        <div style={{ display: 'flex', alignItems: 'center' }}>
          {expressionAndRuntimeTypeComponent}
          {multiType === MultiTypeInputType.RUNTIME && (
            <ConfigureOptions
              value={value as string}
              type={getString('string')}
              variableName={name}
              showRequiredField={false}
              showDefaultField={false}
              showAdvanced={true}
              onChange={val => onChange?.(val, MultiTypeInputValue.STRING, MultiTypeInputType.RUNTIME)}
              style={{ marginLeft: 'var(--spacing-medium)', marginBottom: 12 }}
              {...configureOptionsProps}
              isReadonly={props.disabled}
            />
          )}
        </div>
      ) : (
        expressionAndRuntimeTypeComponent
      )}
    </Container>
  )
}
 
export interface FormMultiTypeTextAreaProps extends Omit<IFormGroupProps, 'label' | 'placeholder'> {
  label: string | React.ReactElement
  name: string
  placeholder?: string
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  formik?: any // TODO: Remove this but not sure why FormikContext<any> was not working
  multiTypeTextArea?: Omit<MultiTypeTextAreaProps, 'name' | 'onChange'>
  onChange?: MultiTypeTextAreaProps['onChange']
  isOptional?: boolean
  tooltipProps?: DataTooltipInterface
}
 
export const FormMultiTypeTextArea: React.FC<FormMultiTypeTextAreaProps> = props => {
  const {
    label,
    multiTypeTextArea,
    placeholder,
    formik,
    name,
    onChange,
    isOptional = false,
    tooltipProps,
    ...restProps
  } = props
  const hasError = errorCheck(name, formik)
 
  const {
    intent = hasError ? Intent.DANGER : Intent.NONE,
    helperText = hasError ? <FormError name={name} errorMessage={get(formik?.errors, name)} /> : null,
    disabled,
    ...rest
  } = restProps
 
  const value: string = get(formik?.values, name, '')
  const customProps: MultiTypeTextAreaProps = {
    ...multiTypeTextArea,
    name,
    textAreaProps: {
      ...multiTypeTextArea?.textAreaProps,
      name,
      placeholder,
      onBlur: () => formik?.setFieldTouched(name)
    }
  }
  const tooltipContext = React.useContext(FormikTooltipContext)
  const dataTooltipId =
    props.tooltipProps?.dataTooltipId || (tooltipContext?.formName ? `${tooltipContext?.formName}_${name}` : '')
 
  const labelToPass = isOptional ? `${label} (Optional)` : label
  return (
    <FormGroup
      {...rest}
      labelFor={name}
      helperText={helperText}
      intent={intent}
      disabled={disabled}
      label={labelToPass ? <HarnessDocTooltip tooltipId={dataTooltipId} labelText={labelToPass} /> : labelToPass}
    >
      <MultiTypeTextArea
        value={value}
        disabled={disabled}
        {...customProps}
        onChange={(val, valueType, type) => {
          formik?.setFieldValue(name, val)
          onChange?.(val, valueType, type)
        }}
      />
    </FormGroup>
  )
}
 
export const FormMultiTypeTextAreaField = connect(FormMultiTypeTextArea)