2023-07-18 11:40:53 +00:00
|
|
|
import { Select, SelectOption } from "@patternfly/react-core";
|
|
|
|
import { useState } from "react";
|
2023-08-09 16:49:02 +00:00
|
|
|
import {
|
|
|
|
Controller,
|
|
|
|
useFormContext,
|
|
|
|
ControllerRenderProps,
|
|
|
|
FieldValues,
|
|
|
|
} from "react-hook-form";
|
2023-07-18 11:40:53 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
|
|
|
|
import { Options } from "../UserProfileFields";
|
2023-08-30 07:22:34 +00:00
|
|
|
import { fieldName, unWrap } from "../utils";
|
2023-07-18 11:40:53 +00:00
|
|
|
import { UserProfileFieldsProps, UserProfileGroup } from "./UserProfileGroup";
|
|
|
|
|
2023-08-30 07:22:34 +00:00
|
|
|
type OptionLabel = Record<string, string> | undefined;
|
2023-08-17 19:26:52 +00:00
|
|
|
export const SelectComponent = (attribute: UserProfileFieldsProps) => {
|
2023-09-08 13:17:17 +00:00
|
|
|
const { t } = useTranslation();
|
2023-07-18 11:40:53 +00:00
|
|
|
const { control } = useFormContext();
|
|
|
|
const [open, setOpen] = useState(false);
|
|
|
|
|
2023-08-09 16:49:02 +00:00
|
|
|
const isMultiValue = (field: ControllerRenderProps<FieldValues, string>) => {
|
|
|
|
return (
|
|
|
|
attribute.annotations?.["inputType"] === "multiselect" ||
|
|
|
|
Array.isArray(field.value)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const setValue = (
|
|
|
|
value: string,
|
|
|
|
field: ControllerRenderProps<FieldValues, string>,
|
|
|
|
) => {
|
|
|
|
if (isMultiValue(field)) {
|
|
|
|
if (field.value.includes(value)) {
|
|
|
|
field.onChange(field.value.filter((item: string) => item !== value));
|
|
|
|
} else {
|
|
|
|
field.onChange([...field.value, value]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
field.onChange(value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-07-19 13:18:07 +00:00
|
|
|
const options =
|
|
|
|
(attribute.validations?.options as Options | undefined)?.options || [];
|
2023-08-09 16:49:02 +00:00
|
|
|
|
2023-08-30 07:22:34 +00:00
|
|
|
const optionLabel = attribute.annotations?.[
|
|
|
|
"inputOptionLabels"
|
|
|
|
] as OptionLabel;
|
|
|
|
const label = (label: string) =>
|
|
|
|
optionLabel ? t(unWrap(optionLabel[label])) : label;
|
|
|
|
|
2023-07-18 11:40:53 +00:00
|
|
|
return (
|
|
|
|
<UserProfileGroup {...attribute}>
|
|
|
|
<Controller
|
|
|
|
name={fieldName(attribute)}
|
|
|
|
defaultValue=""
|
|
|
|
control={control}
|
|
|
|
render={({ field }) => (
|
|
|
|
<Select
|
|
|
|
toggleId={attribute.name}
|
|
|
|
onToggle={(b) => setOpen(b)}
|
2023-08-09 16:49:02 +00:00
|
|
|
isCreatable
|
|
|
|
onCreateOption={(value) => setValue(value, field)}
|
2023-07-18 11:40:53 +00:00
|
|
|
onSelect={(_, value) => {
|
|
|
|
const option = value.toString();
|
2023-08-09 16:49:02 +00:00
|
|
|
setValue(option, field);
|
|
|
|
if (!Array.isArray(field.value)) {
|
2023-07-18 11:40:53 +00:00
|
|
|
setOpen(false);
|
|
|
|
}
|
|
|
|
}}
|
2023-08-09 16:49:02 +00:00
|
|
|
selections={
|
2023-09-14 09:01:15 +00:00
|
|
|
field.value ? field.value : isMultiValue(field) ? [] : t("choose")
|
2023-08-09 16:49:02 +00:00
|
|
|
}
|
|
|
|
variant={isMultiValue(field) ? "typeaheadmulti" : "single"}
|
2023-09-14 09:01:15 +00:00
|
|
|
aria-label={t("selectOne")}
|
2023-07-18 11:40:53 +00:00
|
|
|
isOpen={open}
|
2023-08-17 19:26:52 +00:00
|
|
|
readOnly={attribute.readOnly}
|
2023-07-18 11:40:53 +00:00
|
|
|
>
|
|
|
|
{options.map((option) => (
|
|
|
|
<SelectOption
|
|
|
|
selected={field.value === option}
|
|
|
|
key={option}
|
|
|
|
value={option}
|
|
|
|
>
|
2023-08-30 07:22:34 +00:00
|
|
|
{label(option)}
|
2023-07-18 11:40:53 +00:00
|
|
|
</SelectOption>
|
|
|
|
))}
|
|
|
|
</Select>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</UserProfileGroup>
|
|
|
|
);
|
|
|
|
};
|