2023-07-18 11:40:53 +00:00
|
|
|
import { Select, SelectOption } from "@patternfly/react-core";
|
|
|
|
import { useState } from "react";
|
2023-10-06 13:15:39 +00:00
|
|
|
import { Controller, ControllerRenderProps, FieldPath } from "react-hook-form";
|
2023-07-18 11:40:53 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
|
2023-10-06 13:15:39 +00:00
|
|
|
import { Options, UserProfileFieldProps } from "../UserProfileFields";
|
|
|
|
import { UserFormFields } from "../form-state";
|
2023-08-30 07:22:34 +00:00
|
|
|
import { fieldName, unWrap } from "../utils";
|
2023-10-06 13:15:39 +00:00
|
|
|
import { UserProfileGroup } from "./UserProfileGroup";
|
|
|
|
import { isRequiredAttribute } from "../utils/user-profile";
|
2023-07-18 11:40:53 +00:00
|
|
|
|
2023-08-30 07:22:34 +00:00
|
|
|
type OptionLabel = Record<string, string> | undefined;
|
2023-10-06 13:15:39 +00:00
|
|
|
export const SelectComponent = ({
|
|
|
|
form,
|
|
|
|
inputType,
|
|
|
|
attribute,
|
|
|
|
}: UserProfileFieldProps) => {
|
2023-09-08 13:17:17 +00:00
|
|
|
const { t } = useTranslation();
|
2023-07-18 11:40:53 +00:00
|
|
|
const [open, setOpen] = useState(false);
|
2023-10-06 13:15:39 +00:00
|
|
|
const isRequired = isRequiredAttribute(attribute);
|
|
|
|
const isMultiValue = inputType === "multiselect";
|
2023-08-09 16:49:02 +00:00
|
|
|
|
|
|
|
const setValue = (
|
|
|
|
value: string,
|
2023-10-06 13:15:39 +00:00
|
|
|
field: ControllerRenderProps<UserFormFields>,
|
2023-08-09 16:49:02 +00:00
|
|
|
) => {
|
2023-10-06 13:15:39 +00:00
|
|
|
if (isMultiValue) {
|
2023-08-09 16:49:02 +00:00
|
|
|
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 =
|
2023-09-25 05:19:54 +00:00
|
|
|
(attribute.validators?.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 (
|
2023-10-06 13:15:39 +00:00
|
|
|
<UserProfileGroup form={form} attribute={attribute}>
|
2023-07-18 11:40:53 +00:00
|
|
|
<Controller
|
2023-10-06 13:15:39 +00:00
|
|
|
name={fieldName(attribute) as FieldPath<UserFormFields>}
|
2023-07-18 11:40:53 +00:00
|
|
|
defaultValue=""
|
2023-10-06 13:15:39 +00:00
|
|
|
control={form.control}
|
2023-07-18 11:40:53 +00:00
|
|
|
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-10-06 13:15:39 +00:00
|
|
|
field.value ? field.value : isMultiValue ? [] : t("choose")
|
2023-08-09 16:49:02 +00:00
|
|
|
}
|
2023-10-06 13:15:39 +00:00
|
|
|
variant={isMultiValue ? "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-10-06 13:15:39 +00:00
|
|
|
isDisabled={attribute.readOnly}
|
|
|
|
required={isRequired}
|
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>
|
|
|
|
);
|
|
|
|
};
|