UNPKG

@keycloakify/keycloak-admin-ui

Version:
31 lines 2.57 kB
import { jsx as _jsx } from "react/jsx-runtime"; import { MenuToggle, MenuToggleStatus, Select, SelectList, SelectOption, } from "@patternfly/react-core"; import { get } from "lodash-es"; import { useState } from "react"; import { Controller, useFormContext, } from "react-hook-form"; import { getRuleValue } from "../../../ui-shared/utils/getRuleValue"; import { FormLabel } from "../../../ui-shared/controls/FormLabel"; import { isSelectBasedOptions, isString, key, } from "../../../ui-shared/controls/select-control/SelectControl"; export const SingleSelectControl = ({ id, name, label, options, controller, labelIcon, ...rest }) => { var _a; const { control, formState: { errors }, } = useFormContext(); const [open, setOpen] = useState(false); const required = getRuleValue((_a = controller.rules) === null || _a === void 0 ? void 0 : _a.required) === true; return (_jsx(FormLabel, { name: name, label: label, isRequired: required, error: get(errors, name), labelIcon: labelIcon, children: _jsx(Controller, { ...controller, name: name, control: control, render: ({ field: { onChange, value } }) => (_jsx(Select, { ...rest, onClick: () => setOpen(!open), onOpenChange: () => setOpen(false), selected: isSelectBasedOptions(options) ? options .filter((o) => Array.isArray(value) ? value.includes(o.key) : value === o.key) .map((o) => o.value) : value, toggle: (ref) => { var _a; return (_jsx(MenuToggle, { id: id || name.slice(name.lastIndexOf(".") + 1), ref: ref, onClick: () => setOpen(!open), isExpanded: open, isFullWidth: true, status: get(errors, name) ? MenuToggleStatus.danger : undefined, "aria-label": "toggle", children: isSelectBasedOptions(options) ? (_a = options.find((o) => o.key === (Array.isArray(value) ? value[0] : value))) === null || _a === void 0 ? void 0 : _a.value : value })); }, onSelect: (_event, v) => { const option = v === null || v === void 0 ? void 0 : v.toString(); onChange(Array.isArray(value) ? [option] : option); setOpen(false); }, isOpen: open, children: _jsx(SelectList, { children: options.map((option) => (_jsx(SelectOption, { value: key(option), children: isString(option) ? option : option.value }, key(option)))) }) })) }) })); }; //# sourceMappingURL=SingleSelectControl.js.map