2024-04-05 14:37:05 +00:00
|
|
|
import { Select, SelectOption } from "@patternfly/react-core/deprecated";
|
2023-07-18 11:40:53 +00:00
|
|
|
import { useState } from "react";
|
2023-11-14 11:04:55 +00:00
|
|
|
import { Controller, ControllerRenderProps } from "react-hook-form";
|
2024-03-21 07:09:24 +00:00
|
|
|
import {
|
|
|
|
OptionLabel,
|
|
|
|
Options,
|
|
|
|
UserProfileFieldProps,
|
|
|
|
} from "./UserProfileFields";
|
2023-10-06 13:15:39 +00:00
|
|
|
import { UserProfileGroup } from "./UserProfileGroup";
|
2024-04-08 08:28:17 +00:00
|
|
|
import { UserFormFields, fieldName, isRequiredAttribute, label } from "./utils";
|
2023-07-18 11:40:53 +00:00
|
|
|
|
2023-11-14 11:04:55 +00:00
|
|
|
export const SelectComponent = (props: UserProfileFieldProps) => {
|
|
|
|
const { t, form, inputType, attribute } = props;
|
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
|
|
|
|
2024-04-08 08:28:17 +00:00
|
|
|
const optionLabel =
|
|
|
|
(attribute.annotations?.["inputOptionLabels"] as OptionLabel) || {};
|
|
|
|
const fetchLabel = (option: string) =>
|
|
|
|
label(props.t, optionLabel[option], option);
|
2023-08-30 07:22:34 +00:00
|
|
|
|
2023-07-18 11:40:53 +00:00
|
|
|
return (
|
2023-11-14 11:04:55 +00:00
|
|
|
<UserProfileGroup {...props}>
|
2023-07-18 11:40:53 +00:00
|
|
|
<Controller
|
2023-11-14 11:04:55 +00:00
|
|
|
name={fieldName(attribute.name)}
|
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}
|
2024-04-05 14:37:05 +00:00
|
|
|
onToggle={(_event, b) => setOpen(b)}
|
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
|
|
|
}
|
2024-04-18 20:18:00 +00:00
|
|
|
variant={
|
|
|
|
isMultiValue
|
|
|
|
? "typeaheadmulti"
|
|
|
|
: options.length >= 10
|
|
|
|
? "typeahead"
|
|
|
|
: "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
|
|
|
>
|
2024-01-16 13:02:54 +00:00
|
|
|
{["", ...options].map((option) => (
|
2023-07-18 11:40:53 +00:00
|
|
|
<SelectOption
|
|
|
|
selected={field.value === option}
|
|
|
|
key={option}
|
|
|
|
value={option}
|
|
|
|
>
|
2024-04-08 08:28:17 +00:00
|
|
|
{option ? fetchLabel(option) : t("choose")}
|
2023-07-18 11:40:53 +00:00
|
|
|
</SelectOption>
|
|
|
|
))}
|
|
|
|
</Select>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</UserProfileGroup>
|
|
|
|
);
|
|
|
|
};
|