add stringify to MultiLineInput (#23086)

fixes: #22937
This commit is contained in:
Erik Jan de Wit 2023-09-12 11:58:07 +02:00 committed by GitHub
parent 5d0571ed98
commit fc5565ec08
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -12,6 +12,14 @@ import { HelpItem } from "ui-shared";
import type { ComponentProps } from "./components";
import { convertToName } from "./DynamicComponents";
function stringToMultiline(value?: string): string[] {
return typeof value === "string" && value.length > 0 ? value.split("##") : [];
}
function toStringValue(formValue: string[]): string {
return formValue.join("##");
}
export const MultiValuedListComponent = ({
name,
label,
@ -19,6 +27,7 @@ export const MultiValuedListComponent = ({
defaultValue,
options,
isDisabled = false,
stringify,
}: ComponentProps) => {
const { t } = useTranslation();
const { control } = useFormContext();
@ -49,20 +58,25 @@ export const MultiValuedListComponent = ({
variant={SelectVariant.typeaheadMulti}
typeAheadAriaLabel="Select"
onToggle={(isOpen) => setOpen(isOpen)}
selections={field.value}
selections={
stringify ? stringToMultiline(field.value) : field.value
}
onSelect={(_, v) => {
const option = v.toString();
if (field.value.includes(option)) {
field.onChange(
field.value.filter((item: string) => item !== option),
);
const values = stringify
? stringToMultiline(field.value)
: field.value;
let newValue;
if (values.includes(option)) {
newValue = values.filter((item: string) => item !== option);
} else {
field.onChange([...field.value, option]);
newValue = [...values, option];
}
field.onChange(stringify ? toStringValue(newValue) : newValue);
}}
onClear={(event) => {
event.stopPropagation();
field.onChange([]);
field.onChange(stringify ? "" : []);
}}
isOpen={open}
aria-label={t(label!)}