2024-06-07 16:05:03 +00:00
|
|
|
import { SelectOption } from "@patternfly/react-core";
|
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-06-07 16:05:03 +00:00
|
|
|
import { KeycloakSelect, SelectVariant } from "../select/KeycloakSelect";
|
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-06-05 12:48:48 +00:00
|
|
|
import { UserFormFields, fieldName, 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);
|
2024-06-07 16:05:03 +00:00
|
|
|
const [filter, setFilter] = useState("");
|
2023-10-06 13:15:39 +00:00
|
|
|
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 {
|
2024-06-05 12:48:48 +00:00
|
|
|
if (Array.isArray(field.value)) {
|
|
|
|
field.onChange([...field.value, value]);
|
|
|
|
} else {
|
|
|
|
field.onChange([value]);
|
|
|
|
}
|
2023-08-09 16:49:02 +00:00
|
|
|
}
|
|
|
|
} else {
|
2024-06-07 16:05:03 +00:00
|
|
|
field.onChange(value === field.value ? "" : value);
|
2023-08-09 16:49:02 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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) || {};
|
2024-06-07 16:05:03 +00:00
|
|
|
|
2024-04-08 08:28:17 +00:00
|
|
|
const fetchLabel = (option: string) =>
|
|
|
|
label(props.t, optionLabel[option], option);
|
2023-08-30 07:22:34 +00:00
|
|
|
|
2024-06-07 16:05:03 +00:00
|
|
|
const convertOptions = (selected: string) =>
|
|
|
|
options
|
|
|
|
.filter((o) =>
|
|
|
|
fetchLabel(o)!.toLowerCase().includes(filter.toLowerCase()),
|
|
|
|
)
|
|
|
|
.map((option) => (
|
|
|
|
<SelectOption
|
|
|
|
selected={selected === option}
|
|
|
|
key={option}
|
|
|
|
value={option}
|
|
|
|
>
|
|
|
|
{fetchLabel(option)}
|
|
|
|
</SelectOption>
|
|
|
|
));
|
|
|
|
|
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 }) => (
|
2024-06-07 16:05:03 +00:00
|
|
|
<KeycloakSelect
|
|
|
|
toggleId={attribute.name}
|
|
|
|
onToggle={(b) => setOpen(b)}
|
|
|
|
onClear={() => setValue("", field)}
|
|
|
|
onSelect={(value) => {
|
|
|
|
const option = value.toString();
|
2023-08-09 16:49:02 +00:00
|
|
|
setValue(option, field);
|
2024-06-07 16:05:03 +00:00
|
|
|
if (!Array.isArray(field.value)) {
|
2023-07-18 11:40:53 +00:00
|
|
|
setOpen(false);
|
|
|
|
}
|
|
|
|
}}
|
2024-06-07 16:05:03 +00:00
|
|
|
selections={
|
|
|
|
isMultiValue && Array.isArray(field.value)
|
|
|
|
? field.value
|
|
|
|
: fetchLabel(field.value)
|
|
|
|
}
|
|
|
|
variant={
|
|
|
|
isMultiValue
|
|
|
|
? SelectVariant.typeaheadMulti
|
|
|
|
: options.length >= 10
|
|
|
|
? SelectVariant.typeahead
|
|
|
|
: SelectVariant.single
|
|
|
|
}
|
2023-09-14 09:01:15 +00:00
|
|
|
aria-label={t("selectOne")}
|
2023-07-18 11:40:53 +00:00
|
|
|
isOpen={open}
|
2024-06-07 16:05:03 +00:00
|
|
|
isDisabled={attribute.readOnly}
|
|
|
|
onFilter={(value) => {
|
|
|
|
setFilter(value);
|
|
|
|
return convertOptions(field.value);
|
|
|
|
}}
|
2023-07-18 11:40:53 +00:00
|
|
|
>
|
2024-06-07 16:05:03 +00:00
|
|
|
{convertOptions(field.value)}
|
|
|
|
</KeycloakSelect>
|
2023-07-18 11:40:53 +00:00
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</UserProfileGroup>
|
|
|
|
);
|
|
|
|
};
|