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

92.31% Statements 12/13
53.85% Branches 7/13
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              121x   121x 121x             121x 121x 121x                                                     121x                     44x   44x   44x   44x                                                         121x  
/*
 * 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,
  MultiTypeInputProps,
  SelectOption
} from '@wings-software/uicore'
import { get } from 'lodash-es'
import { useStrings } from 'framework/strings'
import { ConfigureOptions, ConfigureOptionsProps } from '@common/components/ConfigureOptions/ConfigureOptions'
 
interface FormMultiTypeInputProps extends Omit<IFormGroupProps, 'labelFor'> {
  name: string
  label: string
  placeholder?: string
  selectItems: SelectOption[]
  multiTypeInputProps?: Omit<MultiTypeInputProps, 'name'>
}
 
interface MultiTypeSelectConfigureOptionsProps
  extends Omit<ConfigureOptionsProps, 'value' | 'type' | 'variableName' | 'onChange'> {
  variableName?: ConfigureOptionsProps['variableName']
}
 
export interface MultiTypeSelectProps {
  className?: string
  name: string
  label: string | React.ReactElement
  formik?: FormikContext<any>
  multiTypeInputProps: Omit<FormMultiTypeInputProps, 'name' | 'label'>
  enableConfigureOptions?: boolean
  configureOptionsProps?: MultiTypeSelectConfigureOptionsProps
  style?: React.CSSProperties
  disabled?: boolean
}
 
export function MultiTypeSelect(props: MultiTypeSelectProps): React.ReactElement {
  const {
    className,
    label,
    name,
    formik,
    multiTypeInputProps,
    enableConfigureOptions = true,
    configureOptionsProps,
    style,
    disabled = false
  } = props
 
  const value = get(formik?.values, name, '')
 
  const { getString } = useStrings()
 
  return (
    <div className={className} style={style}>
      {label}
      <div style={{ display: 'flex', alignItems: 'center' }}>
        <FormInput.MultiTypeInput
          name={name}
          label=""
          style={{ marginBottom: 0, flexGrow: 1 }}
          {...multiTypeInputProps}
        />
        {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={disabled}
          />
        )}
      </div>
    </div>
  )
}
 
export const MultiTypeSelectField = connect(MultiTypeSelect)