keycloak-scim/src/realm-roles/RoleAttributes.tsx

156 lines
5.3 KiB
TypeScript
Raw Normal View History

2021-01-26 20:06:16 +00:00
import React from "react";
import { useTranslation } from "react-i18next";
import { ArrayField, UseFormMethods } from "react-hook-form";
2020-12-02 14:48:06 +00:00
import { ActionGroup, Button, TextInput } from "@patternfly/react-core";
import {
TableComposable,
Tbody,
Td,
Th,
Thead,
Tr,
} from "@patternfly/react-table";
2020-12-02 14:48:06 +00:00
import { MinusCircleIcon, PlusCircleIcon } from "@patternfly/react-icons";
2020-12-02 14:48:06 +00:00
import { FormAccess } from "../components/form-access/FormAccess";
import { RoleFormType } from "./RealmRoleTabs";
import "./RealmRolesSection.css";
2020-12-02 14:48:06 +00:00
export type KeyValueType = { key: string; value: string };
2020-12-02 14:48:06 +00:00
type RoleAttributesProps = {
form: UseFormMethods<RoleFormType>;
save: (role: RoleFormType) => void;
2021-01-26 20:06:16 +00:00
reset: () => void;
array: {
fields: Partial<ArrayField<Record<string, any>, "id">>[];
append: (
value: Partial<Record<string, any>> | Partial<Record<string, any>>[],
shouldFocus?: boolean | undefined
) => void;
remove: (index?: number | number[] | undefined) => void;
};
2020-12-02 14:48:06 +00:00
};
export const RoleAttributes = ({
2021-01-28 19:16:11 +00:00
form: { handleSubmit, register, formState, errors, watch },
save,
array: { fields, append, remove },
2021-01-26 20:06:16 +00:00
reset,
}: RoleAttributesProps) => {
2020-12-02 14:48:06 +00:00
const { t } = useTranslation("roles");
2020-12-02 14:48:06 +00:00
const columns = ["Key", "Value"];
const watchFirstKey = watch("attributes[0].key", "");
return (
2020-12-02 14:48:06 +00:00
<>
<FormAccess role="manage-realm" onSubmit={handleSubmit(save)}>
2020-12-02 14:48:06 +00:00
<TableComposable
className="kc-role-attributes__table"
aria-label="Role attribute keys and values"
variant="compact"
borders={false}
>
<Thead>
<Tr>
<Th id="key" width={40}>
{columns[0]}
</Th>
<Th id="value" width={40}>
{columns[1]}
</Th>
</Tr>
</Thead>
<Tbody>
{fields.map((attribute, rowIndex) => (
<Tr key={attribute.id}>
<Td
key={`${attribute.id}-key`}
id={`text-input-${rowIndex}-key`}
dataLabel={columns[0]}
>
<TextInput
name={`attributes[${rowIndex}].key`}
ref={register({ required: true })}
2020-12-02 14:48:06 +00:00
aria-label="key-input"
defaultValue={attribute.key}
validated={
errors.attributes && errors.attributes[rowIndex]
? "error"
: "default"
}
2020-12-02 14:48:06 +00:00
/>
</Td>
<Td
key={`${attribute}-value`}
id={`text-input-${rowIndex}-value`}
dataLabel={columns[1]}
>
<TextInput
name={`attributes[${rowIndex}].value`}
ref={register()}
2020-12-02 14:48:06 +00:00
aria-label="value-input"
defaultValue={attribute.value}
validated={errors.description ? "error" : "default"}
2020-12-02 14:48:06 +00:00
/>
</Td>
{rowIndex !== fields.length - 1 && fields.length - 1 !== 0 && (
<Td
key="minus-button"
id={`kc-minus-button-${rowIndex}`}
dataLabel={columns[2]}
>
<Button
id={`minus-button-${rowIndex}`}
aria-label={`remove ${attribute.key} with value ${attribute.value} `}
variant="link"
className="kc-role-attributes__minus-icon"
onClick={() => remove(rowIndex)}
>
<MinusCircleIcon />
</Button>
</Td>
)}
{rowIndex === fields.length - 1 && (
<Td key="add-button" id="add-button" dataLabel={columns[2]}>
{fields[rowIndex].key === "" && (
<Button
id={`minus-button-${rowIndex}`}
aria-label={`remove ${attribute.key} with value ${attribute.value} `}
variant="link"
className="kc-role-attributes__minus-icon"
onClick={() => remove(rowIndex)}
>
<MinusCircleIcon />
</Button>
)}
2020-12-02 14:48:06 +00:00
<Button
aria-label={t("roles:addAttributeText")}
id="plus-icon"
variant="link"
className="kc-role-attributes__plus-icon"
onClick={() => append({ key: "", value: "" })}
2020-12-02 14:48:06 +00:00
icon={<PlusCircleIcon />}
isDisabled={!formState.isValid}
2020-12-02 14:48:06 +00:00
/>
</Td>
)}
</Tr>
))}
2020-12-02 14:48:06 +00:00
</Tbody>
</TableComposable>
<ActionGroup className="kc-role-attributes__action-group">
2021-01-28 19:16:11 +00:00
<Button variant="primary" type="submit" isDisabled={!watchFirstKey}>
2020-12-02 14:48:06 +00:00
{t("common:save")}
</Button>
2021-01-26 20:06:16 +00:00
<Button onClick={reset} variant="link">
{t("common:reload")}
</Button>
2020-12-02 14:48:06 +00:00
</ActionGroup>
</FormAccess>
</>
);
2021-01-26 20:06:16 +00:00
};