parent
5d0571ed98
commit
fc5565ec08
1 changed files with 21 additions and 7 deletions
|
@ -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!)}
|
||||
|
|
Loading…
Reference in a new issue