@keycloakify/keycloak-admin-ui
Version:
Repackaged Keycloak Admin UI
24 lines • 2.3 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Button, Chip, FormGroup, Split, SplitItem, } from "@patternfly/react-core";
import { Controller, useFormContext } from "react-hook-form";
import { useTranslation } from "react-i18next";
import useToggle from "../../utils/useToggle";
import { FormErrorText, HelpItem } from "../../ui-shared";
import { AddRoleMappingModal } from "../../components/role-mapping/AddRoleMappingModal";
import { ServiceRole } from "../../components/role-mapping/RoleMapping";
import { convertToName } from "../../components/dynamic/DynamicComponents";
const parseValue = (value) => (value === null || value === void 0 ? void 0 : value.includes(".")) ? value.split(".") : ["", value || ""];
const parseRow = (value) => {
var _a;
return ((_a = value.client) === null || _a === void 0 ? void 0 : _a.clientId)
? `${value.client.clientId}.${value.role.name}`
: value.role.name;
};
export const RoleComponent = ({ name, label, helpText, defaultValue, required, isDisabled = false, }) => {
const { t } = useTranslation();
const [openModal, toggleModal] = useToggle();
const { control, formState: { errors }, } = useFormContext();
const fieldName = convertToName(name);
return (_jsxs(FormGroup, { label: t(label), labelIcon: _jsx(HelpItem, { helpText: t(helpText), fieldLabelId: `${label}` }), fieldId: name, isRequired: required, children: [_jsx(Controller, { name: fieldName, defaultValue: defaultValue || "", control: control, render: ({ field }) => (_jsxs(Split, { children: [openModal && (_jsx(AddRoleMappingModal, { id: "id", type: "roles", name: name, onAssign: (rows) => field.onChange(parseRow(rows[0])), onClose: toggleModal, isRadio: true })), field.value !== "" && (_jsx(SplitItem, { children: _jsx(Chip, { textMaxWidth: "500px", onClick: () => field.onChange(""), children: _jsx(ServiceRole, { role: { name: parseValue(field.value)[1] }, client: { clientId: parseValue(field.value)[0] } }) }) })), _jsx(SplitItem, { children: _jsx(Button, { onClick: toggleModal, variant: "secondary", "data-testid": "add-roles", disabled: isDisabled, children: t("selectRole.label") }) })] })) }), errors[fieldName] && _jsx(FormErrorText, { message: t("required") })] }));
};
//# sourceMappingURL=RoleComponent.js.map