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

76.27% Statements 45/59
60.61% Branches 60/99
69.57% Functions 16/23
75.86% Lines 44/58

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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301              7x 7x   7x   7x   7x 7x 7x                                                                                                                         6x 6x                     17x 17x 17x                     70x 70x 61x               7x 6x 6x 6x 6x               1x     1x                 1x             1x 5x 5x                               7x 36x 36x     36x       36x 10x 10x       36x               1x     1x                                                                                   7x                                                                                             7x 2x 2x               7x 34x 34x               7x        
/*
 * 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, { useState } from 'react'
import { Container, FormInput, Layout, TagInputProps, Text, tagsType } from '@wings-software/uicore'
import type { ITagInputProps } from '@blueprintjs/core'
import { Color } from '@harness/design-system'
import type { InputWithIdentifierProps } from '@wings-software/uicore/dist/components/InputWithIdentifier/InputWithIdentifier'
import cx from 'classnames'
import type { FormikProps } from 'formik'
import { useStrings } from 'framework/strings'
import i18n from './AddDescriptionAndTags.i18n'
import css from './AddDescriptionAndTags.module.scss'
 
export interface DescriptionAndTagsInputProps<T> {
  formComponent: JSX.Element
  tagInputProps?: TagInputProps<T>
  className?: string
  defaultOpenFields?: string[]
}
 
export interface FormikForAddDescriptionandKVTags {
  name: string
  identifier: string
  description?: string
  tags?: tagsType
}
 
export interface DescriptionAndKVTagsInputProps {
  formComponent: JSX.Element
  formikProps?: FormikProps<FormikForAddDescriptionandKVTags>
  className?: string
  forceOpenTags?: boolean
  tagsProps?: Partial<ITagInputProps>
}
 
export interface AddDescriptionAndTagsWithIdentifier {
  identifierProps: Omit<InputWithIdentifierProps, 'formik'>
  className?: string
  defaultOpenFields?: string[]
}
 
export interface AddDescription {
  identifierProps: Omit<InputWithIdentifierProps, 'formik'>
  className?: string
  formikProps?: FormikProps<FormikForAddDescriptionandKVTags>
}
 
export interface AddDescriptionAndKVTagsWithIdentifier {
  identifierProps: Omit<InputWithIdentifierProps, 'formik'>
  className?: string
  formikProps?: FormikProps<FormikForAddDescriptionandKVTags>
  forceOpenTags?: boolean
  tagsProps?: Partial<ITagInputProps>
}
 
interface FieldLabelWithHideOptionProps {
  onHide: () => void
  fieldLabel: string
}
 
interface FieldLabelWithRemoveOptionProps {
  onRemove: () => void
  fieldLabel: string
}
 
interface AddFieldOptionProps {
  onClick: () => void
  label: string
  isOpen: boolean
}
 
function FieldLabelWithHideOption(props: FieldLabelWithHideOptionProps): JSX.Element {
  const { onHide, fieldLabel } = props
  return (
    <Container className={css.headerRow}>
      <Text inline>{fieldLabel}</Text>
      <Text inline className={css.fieldToggleLabel} onClick={onHide} color={Color.PRIMARY_7}>
        {i18n.hideInput}
      </Text>
    </Container>
  )
}
 
function FieldLabelWithRemoveOption(props: FieldLabelWithRemoveOptionProps): JSX.Element {
  const { onRemove, fieldLabel } = props
  const { getString } = useStrings()
  return (
    <Container className={css.headerRow}>
      <Text inline>{fieldLabel}</Text>
      <Text inline className={css.fieldToggleLabel} onClick={onRemove} color={Color.PRIMARY_7}>
        {getString('removeLabel')}
      </Text>
    </Container>
  )
}
 
function AddFieldOption(props: AddFieldOptionProps): JSX.Element | null {
  const { onClick, label, isOpen } = props
  if (!isOpen) return null
  return (
    <Text className={css.fieldToggleLabel} onClick={onClick} color={Color.PRIMARY_7}>
      {label}
    </Text>
  )
}
 
// Deprecated
export function AddDescriptionAndTags<T>(props: DescriptionAndTagsInputProps<T>): JSX.Element {
  const { formComponent, className, defaultOpenFields = [] } = props
  const [isDescriptionOpen, setIsDescriptionOpen] = useState(defaultOpenFields.includes('description'))
  const [isTagsOpen, setIsTagsOpen] = useState(defaultOpenFields.includes('tags'))
  return (
    <Container className={cx(css.main, className)}>
      <Container className={css.connectorFormNameWrapper}>
        <Container className={css.connectorFormNameElm}>{formComponent}</Container>
        {(!isDescriptionOpen || !isTagsOpen) && (
          <Layout.Vertical spacing="xsmall" style={{ justifyContent: 'center', marginLeft: 'var(--spacing-large)' }}>
            <AddFieldOption
              label={i18n.addDescriptionLabel}
              onClick={() => setIsDescriptionOpen(true)}
              isOpen={!isDescriptionOpen}
            />
            <AddFieldOption label={i18n.addTagsLabel} onClick={() => setIsTagsOpen(true)} isOpen={!isTagsOpen} />
          </Layout.Vertical>
        )}
      </Container>
      {isDescriptionOpen && (
        <FormInput.TextArea
          className={css.expandedDescription}
          name="description"
          label={
            <FieldLabelWithHideOption onHide={() => setIsDescriptionOpen(false)} fieldLabel={i18n.descriptionLabel} />
          }
        />
      )}
      {isTagsOpen && (
        <FormInput.TagInput
          name="tags"
          label={<FieldLabelWithHideOption onHide={() => setIsTagsOpen(false)} fieldLabel={i18n.tagsLabel} />}
          labelFor={name => (typeof name === 'string' ? name : '')}
          itemFromNewTag={newTag => newTag}
          items={[]}
          className="expandedTags"
          tagInputProps={{
            noInputBorder: true,
            openOnKeyDown: false,
            showAddTagButton: true,
            showClearAllButton: true,
            allowNewTag: true
          }}
        />
      )}
    </Container>
  )
}
 
export function AddDescriptionAndKVTags(props: DescriptionAndKVTagsInputProps): JSX.Element {
  const { formComponent, className, formikProps, tagsProps = {}, forceOpenTags = false } = props
  const [isDescriptionOpen, setIsDescriptionOpen] = useState(
    formikProps?.values.description && formikProps.values.description.length > 0
  )
  const [isTagsOpen, setIsTagsOpen] = useState(
    (formikProps?.values.tags && Object.keys(formikProps.values.tags).length > 0) || forceOpenTags
  )
 
  React.useEffect(() => {
    setIsDescriptionOpen(formikProps?.values.description && formikProps.values.description.length > 0)
    setIsTagsOpen((formikProps?.values.tags && Object.keys(formikProps.values.tags).length > 0) || forceOpenTags)
 
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [formikProps?.values?.description, formikProps?.values?.tags, forceOpenTags])
  return (
    <Container className={cx(css.main, className)}>
      <Container className={css.connectorFormNameWrapper}>
        <Container className={css.connectorFormNameElm}>{formComponent}</Container>
        {(!isDescriptionOpen || !isTagsOpen) && (
          <Layout.Vertical spacing="xsmall" style={{ justifyContent: 'center', marginLeft: 'var(--spacing-large)' }}>
            <AddFieldOption
              label={i18n.addDescriptionLabel}
              onClick={() => setIsDescriptionOpen(true)}
              isOpen={!isDescriptionOpen}
            />
            <AddFieldOption label={i18n.addTagsLabel} onClick={() => setIsTagsOpen(true)} isOpen={!isTagsOpen} />
          </Layout.Vertical>
        )}
      </Container>
 
      <Container className={css.connectorFormElm}>
        {isDescriptionOpen && (
          <FormInput.TextArea
            className={css.expandedDescription}
            name="description"
            label={
              <FieldLabelWithRemoveOption
                onRemove={() => {
                  setIsDescriptionOpen(false)
                  formikProps?.setFieldValue('description', '')
                }}
                fieldLabel={i18n.descriptionLabel}
              />
            }
          />
        )}
        {isTagsOpen && (
          <FormInput.KVTagInput
            tagsProps={tagsProps}
            name="tags"
            label={
              <FieldLabelWithRemoveOption
                onRemove={() => {
                  setIsTagsOpen(false)
                  formikProps?.setFieldValue('tags', {})
                }}
                fieldLabel={i18n.tagsLabel}
              />
            }
            className="expandedTags"
          />
        )}
      </Container>
    </Container>
  )
}
 
export function AddDescription(props: DescriptionAndKVTagsInputProps): JSX.Element {
  const { formComponent, className, formikProps } = props
  const [isDescriptionOpen, setIsDescriptionOpen] = useState(
    formikProps?.values.description && formikProps.values.description.length > 0
  )
 
  React.useEffect(() => {
    setIsDescriptionOpen(formikProps?.values.description && formikProps.values.description.length > 0)
 
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [formikProps?.values?.description])
  return (
    <Container className={cx(css.main, className)}>
      <Container className={css.connectorFormNameWrapper}>
        <Container className={css.connectorFormNameElm}>{formComponent}</Container>
        {!isDescriptionOpen && (
          <Layout.Vertical spacing="xsmall" style={{ justifyContent: 'center', marginLeft: 'var(--spacing-large)' }}>
            <AddFieldOption
              label={i18n.addDescriptionLabel}
              onClick={() => setIsDescriptionOpen(true)}
              isOpen={!isDescriptionOpen}
            />
          </Layout.Vertical>
        )}
      </Container>
 
      <Container className={css.connectorFormElm}>
        {isDescriptionOpen && (
          <FormInput.TextArea
            className={css.expandedDescription}
            name="description"
            label={
              <FieldLabelWithRemoveOption
                onRemove={() => {
                  setIsDescriptionOpen(false)
                  formikProps?.setFieldValue('description', '')
                }}
                fieldLabel={i18n.descriptionLabel}
              />
            }
          />
        )}
      </Container>
    </Container>
  )
}
 
export function AddDescriptionAndTagsWithIdentifier(props: AddDescriptionAndTagsWithIdentifier): JSX.Element {
  const { identifierProps, ...additionalProps } = props
  return (
    <AddDescriptionAndTags
      {...additionalProps}
      formComponent={<FormInput.InputWithIdentifier {...identifierProps} />}
    />
  )
}
 
export function AddDescriptionAndKVTagsWithIdentifier(props: AddDescriptionAndKVTagsWithIdentifier): JSX.Element {
  const { identifierProps, ...additionalProps } = props
  return (
    <AddDescriptionAndKVTags
      {...additionalProps}
      formComponent={<FormInput.InputWithIdentifier {...identifierProps} />}
    />
  )
}
 
export function AddDescriptionWithIdentifier(props: AddDescription): JSX.Element {
  const { identifierProps, ...additionalProps } = props
  return <AddDescription {...additionalProps} formComponent={<FormInput.InputWithIdentifier {...identifierProps} />} />
}