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 | 118x 118x 118x 118x 118x 118x 118x 118x 118x 118x 118x 118x 118x 51x 51x 51x 38x 51x 16x 16x 16x 16x 16x 16x 51x 51x 51x 51x 2x 55x 2x 51x 51x 22x 22x 22x 22x 2x 2x 2x 2x 51x 21x 21x 21x 23x 21x 21x 21x 51x 55x 118x | /* * 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 { v4 as nameSpace, v5 as uuid } from 'uuid' import cx from 'classnames' import { MultiTextInput, Button, getMultiTypeFromValue, MultiTypeInputType, MultiTextInputProps, RUNTIME_INPUT_VALUE } from '@wings-software/uicore' import { Intent } from '@harness/design-system' import { connect, FormikContext } from 'formik' import { get, isEmpty } from 'lodash-es' import { ConfigureOptions, ConfigureOptionsProps } from '@common/components/ConfigureOptions/ConfigureOptions' import { useStrings } from 'framework/strings' import MultiTypeFieldSelector, { MultiTypeFieldSelectorProps } from '@common/components/MultiTypeFieldSelector/MultiTypeFieldSelector' import css from './MultiTypeListInputSet.module.scss' export type ListType = string[] | { [key: string]: string }[] export type MultiTypeListType = ListType | string export type ListUIType = { id: string; value: string }[] export type MultiTypeListUIType = ListUIType | string interface MultiTypeListConfigureOptionsProps extends Omit<ConfigureOptionsProps, 'value' | 'type' | 'variableName' | 'onChange'> { variableName?: ConfigureOptionsProps['variableName'] } export interface MultiTypeListProps { name: string placeholder?: string withObjectStructure?: boolean keyName?: string multiTypeFieldSelectorProps: Omit<MultiTypeFieldSelectorProps, 'name' | 'defaultValueToReset' | 'children'> multiTextInputProps?: Omit<MultiTextInputProps, 'name'> enableConfigureOptions?: boolean configureOptionsProps?: MultiTypeListConfigureOptionsProps formik?: FormikContext<any> style?: React.CSSProperties disabled?: boolean } const generateNewValue: () => { id: string; value: string } = () => ({ id: uuid('', nameSpace()), value: '' }) export const MultiTypeListInputSet = (props: MultiTypeListProps): React.ReactElement => { const { name, placeholder, withObjectStructure, keyName, multiTypeFieldSelectorProps, multiTextInputProps = {}, enableConfigureOptions = true, configureOptionsProps, formik, disabled, ...restProps } = props const { getString } = useStrings() const getStageFormikValues = React.useCallback(() => { return get(formik?.values, name, '') }, [formik?.values, name]) const [value, setValue] = React.useState<ListUIType>(() => { let initialValue = getStageFormikValues() Iif (initialValue === RUNTIME_INPUT_VALUE) { initialValue = [] } const initialValueInCorrectFormat = (initialValue || []).map((item: string | { [key: string]: string }) => ({ id: uuid('', nameSpace()), value: withObjectStructure && keyName ? ((item as { [key: string]: string })[keyName] as string) : item })) as ListUIType // Adding a default value Eif (Array.isArray(initialValueInCorrectFormat) && initialValueInCorrectFormat.length === 0) { initialValueInCorrectFormat.push(generateNewValue()) } return initialValueInCorrectFormat }) const error = get(formik?.errors, name, '') const touched = get(formik?.touched, name) const hasSubmitted = get(formik, 'submitCount', 0) > 0 const addValue: () => void = () => { setValue(currentValue => currentValue.concat(generateNewValue())) } const removeValue: (id: string) => () => void = id => () => { setValue(currentValue => currentValue.filter(item => item.id !== id)) } const changeValue: (id: string, newValue: string) => void = (id, newValue) => { formik?.setFieldTouched(name, true) setValue(currentValue => currentValue.map(item => { if (item.id === id) { return { id, value: newValue } } return item }) ) } React.useEffect(() => { let initialValue = getStageFormikValues() Iif (initialValue === RUNTIME_INPUT_VALUE) { initialValue = [] } const valueWithoutEmptyItems = value.filter(item => !!item.value) if (isEmpty(valueWithoutEmptyItems) && initialValue) { const initialValueInCorrectFormat = initialValue.map((item: string | { [key: string]: string }) => ({ id: uuid('', nameSpace()), value: withObjectStructure && keyName ? (item as { [key: string]: string })[keyName] : typeof item === 'string' ? item : item?.value })) as ListUIType // Adding a default value Eif (Array.isArray(initialValueInCorrectFormat) && !initialValueInCorrectFormat.length) { initialValueInCorrectFormat.push(generateNewValue()) } setValue(initialValueInCorrectFormat) } }, [formik?.values, name]) React.useEffect(() => { let valueInCorrectFormat: ListType = [] Eif (Array.isArray(value)) { valueInCorrectFormat = value .filter(item => !!item.value && typeof item.value === 'string') .map(item => { return withObjectStructure && keyName ? { [keyName]: item.value } : item.value }) as ListType } Eif (get(formik?.values, name, '') !== RUNTIME_INPUT_VALUE) { Eif (isEmpty(valueInCorrectFormat)) { formik?.setFieldValue(name, undefined) } else { formik?.setFieldValue(name, valueInCorrectFormat) } } }, [name, value, formik?.setFieldValue]) return ( <div className={cx(css.group, css.withoutSpacing)} {...restProps}> <MultiTypeFieldSelector name={name} defaultValueToReset={[{ id: uuid('', nameSpace()), value: '' }]} style={{ flexGrow: 1, marginBottom: 0 }} {...multiTypeFieldSelectorProps} disableTypeSelection={multiTypeFieldSelectorProps.disableTypeSelection || disabled} > <> {value.map(({ id, value: valueValue }, index: number) => { // const valueError = get(error, `[${index}].value`) return ( <div className={css.group} key={id}> <div style={{ flexGrow: 1 }}> <MultiTextInput name="" textProps={{ name: `${name}[${index}].value` }} value={valueValue} placeholder={placeholder} onChange={v => changeValue(id, v as any)} data-testid={`value-${name}-[${index}]`} intent={(touched || hasSubmitted) && error ? Intent.DANGER : Intent.NONE} disabled={disabled} allowableTypes={[MultiTypeInputType.FIXED, MultiTypeInputType.EXPRESSION]} {...multiTextInputProps} /> </div> {!disabled && ( <Button icon="main-trash" iconProps={{ size: 20 }} minimal onClick={removeValue(id)} data-testid={`remove-${name}-[${index}]`} style={{ padding: 0 }} /> )} </div> ) })} {!disabled && ( <Button intent="primary" minimal text={getString('plusAdd')} data-testid={`add-${name}`} onClick={addValue} /> )} </> </MultiTypeFieldSelector> {enableConfigureOptions && typeof value === 'string' && getMultiTypeFromValue(value) === MultiTypeInputType.RUNTIME && ( <ConfigureOptions style={{ marginBottom: 11 }} value={value} type={getString('list')} variableName={name} showRequiredField={false} showDefaultField={false} showAdvanced={true} onChange={val => formik?.setFieldValue(name, val)} {...configureOptionsProps} isReadonly={props.disabled} /> )} </div> ) } export default connect(MultiTypeListInputSet) |