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

82.46% Statements 47/57
72.09% Branches 62/86
60% Functions 9/15
81.48% Lines 44/54

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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235              522x 522x   522x 522x 522x   522x                 522x                                           522x 1684x 1683x 1683x 1683x         522x 1419x 1419x 1419x 1419x 1419x   1419x 320x     1419x                         3x 3x                                     522x 1415x 1415x 1415x   1415x 316x     1415x                                                                                                                             522x 1318x                 1317x 1317x 1317x                                     522x                     522x 1x 1x 1x   1x               522x 37x 37x              
/*
 * Copyright 2022 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, { useEffect, useState } from 'react'
import { Container, FormInput, Icon, Label, DataTooltipInterface, HarnessDocTooltip } from '@wings-software/uicore'
import type { InputWithIdentifierProps } from '@wings-software/uicore/dist/components/InputWithIdentifier/InputWithIdentifier'
import { defaultTo, isEmpty } from 'lodash-es'
import { Classes, IInputGroupProps, ITagInputProps } from '@blueprintjs/core'
import cx from 'classnames'
import type { FormikProps } from 'formik'
import { useStrings } from 'framework/strings'
import type {
  DescriptionComponentProps,
  DescriptionProps,
  NameIdDescriptionProps,
  NameIdDescriptionTagsDeprecatedProps,
  TagsComponentProps,
  TagsDeprecatedComponentProps
} from './NameIdDescriptionTagsConstants'
import css from './NameIdDescriptionTags.module.scss'
 
export interface NameIdDescriptionTagsProps {
  identifierProps?: Omit<InputWithIdentifierProps, 'formik'>
  inputGroupProps?: IInputGroupProps
  descriptionProps?: DescriptionProps
  tagsProps?: Partial<ITagInputProps> & {
    isOption?: boolean
  }
  formikProps: FormikProps<any>
  className?: string
  tooltipProps?: DataTooltipInterface
}
 
export interface NameIdProps {
  nameLabel?: string // Strong default preference for "Name" vs. Contextual Name (e.g. "Service Name") unless approved otherwise
  namePlaceholder?: string
  identifierProps?: Omit<InputWithIdentifierProps, 'formik'>
  inputGroupProps?: IInputGroupProps
  dataTooltipId?: string
}
 
export const NameId = (props: NameIdProps): JSX.Element => {
  const { getString } = useStrings()
  const { identifierProps, nameLabel = getString('name'), inputGroupProps = {} } = props
  const newInputGroupProps = { placeholder: getString('common.namePlaceholder'), ...inputGroupProps }
  return (
    <FormInput.InputWithIdentifier inputLabel={nameLabel} inputGroupProps={newInputGroupProps} {...identifierProps} />
  )
}
 
export const Description = (props: DescriptionComponentProps): JSX.Element => {
  const { descriptionProps = {}, hasValue, disabled = false } = props
  const { isOptional = true, ...restDescriptionProps } = descriptionProps
  const { getString } = useStrings()
  const [isDescriptionOpen, setDescriptionOpen] = useState<boolean>(hasValue || false)
  const [isDescriptionFocus, setDescriptionFocus] = useState<boolean>(false)
 
  useEffect(() => {
    setDescriptionOpen(defaultTo(hasValue, false))
  }, [hasValue])
 
  return (
    <Container style={{ marginBottom: isDescriptionOpen ? '0' : 'var(--spacing-medium)' }}>
      <Label className={cx(Classes.LABEL, css.descriptionLabel)} data-tooltip-id={props.dataTooltipId}>
        {isOptional ? getString('optionalField', { name: getString('description') }) : getString('description')}
        {props.dataTooltipId ? <HarnessDocTooltip useStandAlone={true} tooltipId={props.dataTooltipId} /> : null}
        {!isDescriptionOpen && (
          <Icon
            className={css.editOpen}
            data-name="edit"
            data-testid="description-edit"
            size={12}
            name="Edit"
            onClick={() => {
              setDescriptionOpen(true)
              setDescriptionFocus(true)
            }}
          />
        )}
      </Label>
      {isDescriptionOpen && (
        <FormInput.TextArea
          data-name="description"
          disabled={disabled}
          autoFocus={isDescriptionFocus}
          name="description"
          placeholder={getString('common.descriptionPlaceholder')}
          {...restDescriptionProps}
        />
      )}
    </Container>
  )
}
 
export const Tags = (props: TagsComponentProps): JSX.Element => {
  const { tagsProps, hasValue, isOptional = true } = props
  const { getString } = useStrings()
  const [isTagsOpen, setTagsOpen] = useState<boolean>(hasValue || false)
 
  useEffect(() => {
    setTagsOpen(defaultTo(hasValue, false))
  }, [hasValue])
 
  return (
    <Container>
      <Label className={cx(Classes.LABEL, css.descriptionLabel)} data-tooltip-id={props.dataTooltipId}>
        {isOptional ? getString('optionalField', { name: getString('tagsLabel') }) : getString('tagsLabel')}
        {props.dataTooltipId ? <HarnessDocTooltip useStandAlone={true} tooltipId={props.dataTooltipId} /> : null}
        {!isTagsOpen && (
          <Icon
            className={css.editOpen}
            data-name="edit"
            data-testid="tags-edit"
            size={12}
            name="Edit"
            onClick={() => {
              setTagsOpen(true)
            }}
          />
        )}
      </Label>
      {isTagsOpen && <FormInput.KVTagInput name="tags" tagsProps={tagsProps} />}
    </Container>
  )
}
 
function TagsDeprecated(props: TagsDeprecatedComponentProps): JSX.Element {
  const { hasValue } = props
  const { getString } = useStrings()
  const [isTagsOpen, setTagsOpen] = useState<boolean>(hasValue || false)
 
  return (
    <Container>
      <Label className={cx(Classes.LABEL, css.descriptionLabel)}>
        {getString('tagsLabel')}
        {!isTagsOpen && (
          <Icon
            className={css.editOpen}
            data-name="Edit"
            size={12}
            name="edit"
            onClick={() => {
              setTagsOpen(true)
            }}
          />
        )}
      </Label>
      {isTagsOpen && (
        <FormInput.TagInput
          name="tags"
          labelFor={name => (typeof name === 'string' ? name : '')}
          itemFromNewTag={newTag => newTag}
          items={[]}
          tagInputProps={{
            noInputBorder: true,
            openOnKeyDown: false,
            showAddTagButton: true,
            showClearAllButton: true,
            allowNewTag: true
          }}
        />
      )}
    </Container>
  )
}
 
export function NameIdDescriptionTags(props: NameIdDescriptionTagsProps): JSX.Element {
  const { getString } = useStrings()
  const {
    className,
    identifierProps,
    descriptionProps,
    tagsProps,
    formikProps,
    inputGroupProps = {},
    tooltipProps
  } = props
  const newInputGroupProps = { placeholder: getString('common.namePlaceholder'), ...inputGroupProps }
  return (
    <Container className={cx(css.main, className)}>
      <NameId identifierProps={identifierProps} inputGroupProps={newInputGroupProps} />
      <Description
        descriptionProps={descriptionProps}
        hasValue={!!formikProps?.values.description}
        dataTooltipId={tooltipProps?.dataTooltipId ? `${tooltipProps.dataTooltipId}_description` : undefined}
      />
      <Tags
        tagsProps={tagsProps}
        isOptional={tagsProps?.isOption}
        hasValue={!isEmpty(formikProps?.values.tags)}
        dataTooltipId={tooltipProps?.dataTooltipId ? `${tooltipProps.dataTooltipId}_tags` : undefined}
      />
    </Container>
  )
}
 
// Requires verification with existing tags
export function NameIdDescriptionTagsDeprecated<T>(props: NameIdDescriptionTagsDeprecatedProps<T>): JSX.Element {
  const { className, identifierProps, descriptionProps, formikProps } = props
  return (
    <Container className={cx(css.main, className)}>
      <NameId identifierProps={identifierProps} />
      <Description descriptionProps={descriptionProps} hasValue={!!formikProps?.values.description} />
      <TagsDeprecated hasValue={!isEmpty(formikProps?.values.tags)} />
    </Container>
  )
}
 
export function NameIdDescription(props: NameIdDescriptionProps): JSX.Element {
  const { getString } = useStrings()
  const { className, identifierProps, descriptionProps, formikProps, inputGroupProps = {} } = props
  const newInputGroupProps = { placeholder: getString('common.namePlaceholder'), ...inputGroupProps }
 
  return (
    <Container className={cx(css.main, className)}>
      <NameId identifierProps={identifierProps} inputGroupProps={newInputGroupProps} />
      <Description descriptionProps={descriptionProps} hasValue={!!formikProps?.values.description} />
    </Container>
  )
}
 
export function DescriptionTags(props: Omit<NameIdDescriptionTagsProps, 'identifierProps'>): JSX.Element {
  const { className, descriptionProps, tagsProps, formikProps } = props
  return (
    <Container className={cx(css.main, className)}>
      <Description descriptionProps={descriptionProps} hasValue={!!formikProps?.values.description} />
      <Tags tagsProps={tagsProps} isOptional={tagsProps?.isOption} hasValue={!isEmpty(formikProps?.values.tags)} />
    </Container>
  )
}