import { Button, ButtonVariant, InputGroup, TextInput, TextInputProps, } from "@patternfly/react-core"; import { MinusCircleIcon, PlusCircleIcon } from "@patternfly/react-icons"; import { type TFunction } from "i18next"; import { Fragment, useEffect, useMemo } from "react"; import { FieldPath, UseFormReturn, useWatch } from "react-hook-form"; import { UserProfileFieldProps } from "./UserProfileFields"; import { UserProfileGroup } from "./UserProfileGroup"; import { UserFormFields, fieldName, labelAttribute } from "./utils"; export const MultiInputComponent = ({ t, form, attribute, renderer, }: UserProfileFieldProps) => ( ); export type MultiLineInputProps = Omit & { t: TFunction; name: FieldPath; form: UseFormReturn; addButtonLabel?: string; isDisabled?: boolean; defaultValue?: string[]; }; const MultiLineInput = ({ t, name, form, addButtonLabel, isDisabled = false, defaultValue, id, ...rest }: MultiLineInputProps) => { const { register, setValue, control } = form; const value = useWatch({ name, control, defaultValue: defaultValue || "", }); const fields = useMemo(() => { return Array.isArray(value) && value.length !== 0 ? value : defaultValue || [""]; }, [value]); const remove = (index: number) => { update([...fields.slice(0, index), ...fields.slice(index + 1)]); }; const append = () => { update([...fields, ""]); }; const updateValue = (index: number, value: string) => { update([...fields.slice(0, index), value, ...fields.slice(index + 1)]); }; const update = (values: string[]) => { const fieldValue = values.flatMap((field) => field); setValue(name, fieldValue, { shouldDirty: true, }); }; useEffect(() => { register(name); }, [register]); return ( {fields.map((value, index) => ( updateValue(index, value)} name={`${name}.${index}.value`} value={value} isDisabled={isDisabled} {...rest} /> remove(index)} tabIndex={-1} aria-label={t("remove")} isDisabled={fields.length === 1 || isDisabled} > {index === fields.length - 1 && ( {t(addButtonLabel || "add")} )} ))} ); };