All files / modules/85-cv/components/JsonSelector JsonSelectorFormInput.tsx

0% Statements 0/31
0% Branches 0/37
0% Functions 0/5
0% Lines 0/30

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                                                                                                                                                                                                                     
/*
 * 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 { connect } from 'formik'
import get from 'lodash/get'
import isEmpty from 'lodash/isEmpty'
import { FormGroup, Dialog, Icon, Spinner } from '@blueprintjs/core'
import { Container, Text } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import { useModalHook } from '@harness/use-modal'
import classnames from 'classnames'
import JsonSelector from './JsonSelector'
import css from './JsonSelectorFormInput.module.scss'
 
interface JsonSelectorFormInputProps {
  name: string
  label: string
  json?: Record<string, any>
  disabled?: boolean
  loading?: boolean
  placeholder?: string
}
 
function getPlaceholderAndTextColor(
  loading?: boolean,
  json?: Record<string, any>,
  placeholder?: string,
  selectedJsonVal?: string
): { value?: string; valueColor: Color } {
  if (loading) {
    return { value: '', valueColor: Color.BLACK }
  } else if (!selectedJsonVal || !Object.keys(selectedJsonVal)?.length) {
    return { value: !json || !Object.keys(json)?.length ? 'No data' : placeholder, valueColor: Color.GREY_400 }
  }
  return { value: selectedJsonVal, valueColor: Color.BLACK }
}
 
const JsonSelectorFormInput = (props: JsonSelectorFormInputProps & { formik?: any }): JSX.Element => {
  const { name, label, json, formik, loading, placeholder, disabled = false } = props
  const { value, valueColor } = getPlaceholderAndTextColor(loading, json, placeholder, get(formik.values, name))
  const validationError = get(formik.touched, name) ? get(formik.errors, name) : undefined
 
  const onPathSelect = (path: string): void => {
    hideModal()
    formik.setFieldValue(name, path)
    formik.setFieldTouched(name, true)
  }
 
  const onOpenModal = (): void => {
    if (!isEmpty(json)) openModal()
  }
 
  const [openModal, hideModal] = useModalHook(
    () => (
      <Dialog
        isOpen
        usePortal
        autoFocus
        canEscapeKeyClose
        canOutsideClickClose
        enforceFocus={false}
        onClose={hideModal}
        style={{ width: 1200, height: 600, borderLeft: 0, paddingBottom: 0, position: 'relative', overflow: 'hidden' }}
      >
        <div className={css.dialogContent}>
          <div className={css.leftColumn}>
            Select service <br /> instance field name
          </div>
          <JsonSelector className={css.rightColumn} json={json || {}} onPathSelect={onPathSelect} />
        </div>
      </Dialog>
    ),
    [json]
  )
 
  return (
    <FormGroup
      labelFor={name}
      label={label}
      disabled={disabled}
      helperText={validationError}
      intent={validationError?.length ? 'danger' : undefined}
    >
      <Container className={classnames('bp3-input-group', css.inputGroup, disabled ? 'bp3-disabled' : undefined)}>
        <Container
          className={classnames('bp3-input', css.input, disabled ? css.disabledLook : undefined)}
          onClick={onOpenModal}
        >
          <Text color={valueColor}>{value}</Text>
        </Container>
        {loading ? (
          <Spinner size={8} className={css.loadingJsonIcon} />
        ) : (
          <Icon className={css.inputIcon} icon="plus" iconSize={12} />
        )}
      </Container>
    </FormGroup>
  )
}
 
export default connect(JsonSelectorFormInput)