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

80% Statements 12/15
62.5% Branches 5/8
25% Functions 1/4
80% Lines 12/15

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              167x 167x 167x 167x   167x 167x   167x             167x                                           167x 14x 14x   14x                                                                                                        
/*
 * 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 { IFormGroupProps, IInputGroupProps, HTMLInputProps, Popover, Menu } from '@blueprintjs/core'
import { FormInput, Button, Icon, Utils, TextInput } from '@wings-software/uicore'
import cx from 'classnames'
 
import { useStrings } from 'framework/strings'
import { toVariableStr } from '@common/utils/StringUtils'
 
import css from './TextInputWithCopyBtn.module.scss'
 
export interface ItemNameProps {
  mainText?: string
  secondaryText?: string
}
 
export function ItemName(props: ItemNameProps): React.ReactElement {
  return (
    <div className={css.nameItem}>
      <div className={css.mainText}>{props.mainText}</div>
      <div className={css.secondaryText}>{props.secondaryText}</div>
    </div>
  )
}
 
export interface TextInputWithCopyBtnProps extends Omit<IFormGroupProps, 'labelFor'> {
  name: string
  inputGroup?: Omit<IInputGroupProps & HTMLInputProps, 'name' | 'value' | 'onChange' | 'placeholder'>
  placeholder?: string
  onChange?: IInputGroupProps['onChange']
  localName?: string
  fullName?: string
  outerClassName?: string
  textInputClassName?: string
  popoverWrapperClassName?: string
  staticDisplayValue?: string
}
 
export function TextInputWithCopyBtn(props: TextInputWithCopyBtnProps): React.ReactElement {
  const { localName, fullName, disabled, ...rest } = props
  const { getString } = useStrings()
 
  return (
    <div className={cx(css.textInputWithCopyBtn, props.outerClassName, { [css.disabled]: disabled })}>
      {props.staticDisplayValue ? (
        // Render a non formik text component if the display value is static
        <TextInput
          {...rest}
          value={props.staticDisplayValue}
          disabled={true}
          className={cx(css.input, props.textInputClassName, rest.className)}
        />
      ) : (
        <FormInput.Text
          {...rest}
          inputGroup={{ disabled }}
          disabled={disabled}
          className={cx(css.input, props.textInputClassName, rest.className)}
        />
      )}
      <Popover
        interactionKind="click"
        minimal
        position="bottom-right"
        wrapperTagName="div"
        targetTagName="div"
        className={cx(props.popoverWrapperClassName, css.popoverWrapper)}
        popoverClassName={css.popover}
      >
        <Button icon="copy-alt" className={css.btn} iconProps={{ size: 12 }} disabled={!localName && !fullName} />
        <Menu className={css.menu}>
          {localName ? (
            <Menu.Item
              className={css.menuItem}
              labelClassName={css.menuItemLabel}
              labelElement={<Icon name="copy-alt" />}
              text={<ItemName mainText={localName} secondaryText={getString('variables.copyVariableName')} />}
              onClick={() => Utils.copy(toVariableStr(localName))}
            />
          ) : null}
          {fullName ? (
            <Menu.Item
              className={css.menuItem}
              labelClassName={css.menuItemLabel}
              labelElement={<Icon name="copy-alt" />}
              text={<ItemName mainText={fullName} secondaryText={getString('variables.copyFqn')} />}
              onClick={() => Utils.copy(toVariableStr(fullName))}
            />
          ) : null}
        </Menu>
      </Popover>
    </div>
  )
}