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

95.12% Statements 39/41
67.47% Branches 56/83
100% Functions 7/7
94.87% Lines 37/39

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              501x 501x 501x                       501x 501x 501x   501x   501x             501x             226x 226x   197x 197x             1x                 226x                                           501x                     226x 226x               226x   226x 226x 226x   226x 98x         226x 98x 2x       226x 226x 226x   226x 226x                                     1x     1x   1x 1x                 501x  
/*
 * 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 { FormGroup, ICheckboxProps, IFormGroupProps, Intent } from '@blueprintjs/core'
import {
  Checkbox,
  DataTooltipInterface,
  ExpressionAndRuntimeType,
  ExpressionAndRuntimeTypeProps,
  FormError,
  getMultiTypeFromValue,
  HarnessDocTooltip,
  MultiTypeInputType,
  MultiTypeInputValue,
  FormikTooltipContext
} from '@wings-software/uicore'
import cx from 'classnames'
import { get, isNil } from 'lodash-es'
import { connect } from 'formik'
 
import { errorCheck } from '@common/utils/formikHelpers'
 
import css from './MultiTypeCheckbox.module.scss'
 
export interface MultiTypeCheckboxProps
  extends Omit<ExpressionAndRuntimeTypeProps, 'fixedTypeComponent' | 'fixedTypeComponentProps'> {
  textboxProps?: Omit<ICheckboxProps, 'onChange'>
}
 
export const MultiTypeCheckbox: React.FC<MultiTypeCheckboxProps> = ({
  textboxProps,
  value = false,
  children,
  disabled,
  ...rest
}) => {
  const { className = '', ...restProps } = textboxProps || {}
  const fixedTypeComponent = React.useCallback(
    props => {
      const { onChange } = props
      return (
        <Checkbox
          className={cx(css.input, className)}
          disabled={disabled}
          {...restProps}
          checked={value as boolean}
          onChange={(event: React.FormEvent<HTMLInputElement>) => {
            onChange?.(event.currentTarget.checked, MultiTypeInputValue.STRING)
          }}
        >
          {children}
        </Checkbox>
      )
    },
    [value]
  )
  return (
    <ExpressionAndRuntimeType
      value={value as string}
      disabled={disabled}
      {...rest}
      fixedTypeComponent={fixedTypeComponent}
    />
  )
}
 
export interface FormMultiTypeTextboxProps extends Omit<IFormGroupProps, 'label'> {
  label: string
  name: string
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  formik?: any // TODO: Remove this but not sure why FormikContext<any> was not working
  multiTypeTextbox?: Omit<MultiTypeCheckboxProps, 'onChange' | 'name'>
  onChange?: MultiTypeCheckboxProps['onChange']
  setToFalseWhenEmpty?: boolean
  tooltipProps?: DataTooltipInterface
  defaultTrue?: boolean
}
 
export const FormMultiTypeCheckbox: React.FC<FormMultiTypeTextboxProps> = props => {
  const {
    label,
    multiTypeTextbox,
    formik,
    name,
    onChange,
    setToFalseWhenEmpty = false,
    defaultTrue = false,
    className = '',
    ...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,
    tooltipProps,
    ...rest
  } = restProps
 
  const { textboxProps, ...restMultiProps } = multiTypeTextbox || {}
  const value: boolean = get(formik?.values, name, false)
  const [type, setType] = React.useState<MultiTypeInputType>(getMultiTypeFromValue(value))
 
  React.useEffect(() => {
    Iif (setToFalseWhenEmpty && get(formik?.values, name) === '') {
      formik?.setFieldValue(name, false)
    }
  }, [setToFalseWhenEmpty])
 
  React.useEffect(() => {
    if (defaultTrue) {
      formik?.setFieldValue(name, true)
    }
  }, [defaultTrue])
 
  const isFixedValue = type === MultiTypeInputType.FIXED
  const labelToBePassed = !isFixedValue ? label : undefined
  const tooltipContext = React.useContext(FormikTooltipContext)
  const dataTooltipId =
    props.tooltipProps?.dataTooltipId || (tooltipContext?.formName ? `${tooltipContext?.formName}_${name}` : '')
  return (
    <FormGroup
      {...rest}
      label={
        labelToBePassed ? <HarnessDocTooltip tooltipId={dataTooltipId} labelText={labelToBePassed} /> : labelToBePassed
      }
      labelFor={name}
      className={cx(css.multiTypeCheckbox, className)}
      helperText={helperText}
      intent={intent}
      disabled={disabled}
    >
      <MultiTypeCheckbox
        name={name}
        textboxProps={{ name, label, disabled, ...textboxProps }}
        value={value}
        disabled={disabled}
        {...restMultiProps}
        onChange={(val, valueType, typeVal) => {
          Iif (typeVal === MultiTypeInputType.EXPRESSION && isNil(val)) {
            formik?.setFieldValue(name, '')
          } else {
            formik?.setFieldValue(name, val)
          }
          setType(typeVal)
          onChange?.(val, valueType, type)
        }}
      />
      {!labelToBePassed ? (
        <HarnessDocTooltip className={css.tooltip} tooltipId={dataTooltipId} labelText={labelToBePassed} />
      ) : null}
    </FormGroup>
  )
}
export const FormMultiTypeCheckboxField = connect(FormMultiTypeCheckbox)