2024-06-05 12:48:48 +00:00
|
|
|
import {
|
|
|
|
Chip,
|
|
|
|
ChipGroup,
|
|
|
|
MenuToggle,
|
|
|
|
Select,
|
|
|
|
SelectList,
|
|
|
|
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-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);
|
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 {
|
|
|
|
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
|
2024-06-05 12:48:48 +00:00
|
|
|
toggle={(ref) => (
|
|
|
|
<MenuToggle
|
|
|
|
id={attribute.name}
|
|
|
|
ref={ref}
|
|
|
|
onClick={() => setOpen(!open)}
|
|
|
|
isExpanded={open}
|
|
|
|
isFullWidth
|
|
|
|
isDisabled={attribute.readOnly}
|
|
|
|
>
|
|
|
|
{(isMultiValue && Array.isArray(field.value) ? (
|
|
|
|
<ChipGroup>
|
|
|
|
{field.value.map((selection, index: number) => (
|
|
|
|
<Chip
|
|
|
|
key={index}
|
|
|
|
onClick={(ev) => {
|
|
|
|
ev.stopPropagation();
|
|
|
|
setValue(selection, field);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{selection}
|
|
|
|
</Chip>
|
|
|
|
))}
|
|
|
|
</ChipGroup>
|
|
|
|
) : (
|
|
|
|
fetchLabel(field.value)
|
|
|
|
)) || t("choose")}
|
|
|
|
</MenuToggle>
|
|
|
|
)}
|
2023-07-18 11:40:53 +00:00
|
|
|
onSelect={(_, value) => {
|
2024-06-05 12:48:48 +00:00
|
|
|
const option = value?.toString() || "";
|
2023-08-09 16:49:02 +00:00
|
|
|
setValue(option, field);
|
2024-06-05 12:48:48 +00:00
|
|
|
if (!isMultiValue) {
|
2023-07-18 11:40:53 +00:00
|
|
|
setOpen(false);
|
|
|
|
}
|
|
|
|
}}
|
2024-06-05 12:48:48 +00:00
|
|
|
selected={field.value}
|
2023-09-14 09:01:15 +00:00
|
|
|
aria-label={t("selectOne")}
|
2023-07-18 11:40:53 +00:00
|
|
|
isOpen={open}
|
|
|
|
>
|
2024-06-05 12:48:48 +00:00
|
|
|
<SelectList>
|
|
|
|
{options.map((option) => (
|
|
|
|
<SelectOption
|
|
|
|
selected={field.value === option}
|
|
|
|
key={option}
|
|
|
|
value={option}
|
|
|
|
>
|
|
|
|
{fetchLabel(option)}
|
|
|
|
</SelectOption>
|
|
|
|
))}
|
|
|
|
</SelectList>
|
2023-07-18 11:40:53 +00:00
|
|
|
</Select>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</UserProfileGroup>
|
|
|
|
);
|
|
|
|
};
|