RoleAttributes.js 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import React from "../_snowpack/pkg/react.js";
  2. import {useTranslation} from "../_snowpack/pkg/react-i18next.js";
  3. import {ActionGroup, Button, TextInput} from "../_snowpack/pkg/@patternfly/react-core.js";
  4. import {
  5. TableComposable,
  6. Tbody,
  7. Td,
  8. Th,
  9. Thead,
  10. Tr
  11. } from "../_snowpack/pkg/@patternfly/react-table.js";
  12. import {MinusCircleIcon, PlusCircleIcon} from "../_snowpack/pkg/@patternfly/react-icons.js";
  13. import {FormAccess} from "../components/form-access/FormAccess.js";
  14. import "./RealmRolesSection.css";
  15. export const RoleAttributes = ({
  16. form: {register, formState, errors, watch},
  17. save,
  18. array: {fields, append, remove},
  19. reset
  20. }) => {
  21. const {t} = useTranslation("roles");
  22. const columns = ["Key", "Value"];
  23. const watchFirstKey = watch("attributes[0].key", "");
  24. return /* @__PURE__ */ React.createElement(FormAccess, {
  25. role: "manage-realm"
  26. }, /* @__PURE__ */ React.createElement(TableComposable, {
  27. className: "kc-role-attributes__table",
  28. "aria-label": "Role attribute keys and values",
  29. variant: "compact",
  30. borders: false
  31. }, /* @__PURE__ */ React.createElement(Thead, null, /* @__PURE__ */ React.createElement(Tr, null, /* @__PURE__ */ React.createElement(Th, {
  32. id: "key",
  33. width: 40
  34. }, columns[0]), /* @__PURE__ */ React.createElement(Th, {
  35. id: "value",
  36. width: 40
  37. }, columns[1]))), /* @__PURE__ */ React.createElement(Tbody, null, fields.map((attribute, rowIndex) => /* @__PURE__ */ React.createElement(Tr, {
  38. key: attribute.id
  39. }, /* @__PURE__ */ React.createElement(Td, {
  40. key: `${attribute.id}-key`,
  41. id: `text-input-${rowIndex}-key`,
  42. dataLabel: columns[0]
  43. }, /* @__PURE__ */ React.createElement(TextInput, {
  44. name: `attributes[${rowIndex}].key`,
  45. ref: register(),
  46. "aria-label": "key-input",
  47. defaultValue: attribute.key,
  48. validated: errors.attributes?.[rowIndex] ? "error" : "default"
  49. })), /* @__PURE__ */ React.createElement(Td, {
  50. key: `${attribute}-value`,
  51. id: `text-input-${rowIndex}-value`,
  52. dataLabel: columns[1]
  53. }, /* @__PURE__ */ React.createElement(TextInput, {
  54. name: `attributes[${rowIndex}].value`,
  55. ref: register(),
  56. "aria-label": "value-input",
  57. defaultValue: attribute.value,
  58. validated: errors.description ? "error" : "default"
  59. })), rowIndex !== fields.length - 1 && fields.length - 1 !== 0 && /* @__PURE__ */ React.createElement(Td, {
  60. key: "minus-button",
  61. id: `kc-minus-button-${rowIndex}`,
  62. dataLabel: columns[2]
  63. }, /* @__PURE__ */ React.createElement(Button, {
  64. id: `minus-button-${rowIndex}`,
  65. "aria-label": `remove ${attribute.key} with value ${attribute.value} `,
  66. variant: "link",
  67. className: "kc-role-attributes__minus-icon",
  68. onClick: () => remove(rowIndex)
  69. }, /* @__PURE__ */ React.createElement(MinusCircleIcon, null))), rowIndex === fields.length - 1 && /* @__PURE__ */ React.createElement(Td, {
  70. key: "add-button",
  71. id: "add-button",
  72. dataLabel: columns[2]
  73. }, fields[rowIndex].key === "" && /* @__PURE__ */ React.createElement(Button, {
  74. id: `minus-button-${rowIndex}`,
  75. "aria-label": `remove ${attribute.key} with value ${attribute.value} `,
  76. variant: "link",
  77. className: "kc-role-attributes__minus-icon",
  78. onClick: () => remove(rowIndex)
  79. }, /* @__PURE__ */ React.createElement(MinusCircleIcon, null)), /* @__PURE__ */ React.createElement(Button, {
  80. "aria-label": t("roles:addAttributeText"),
  81. id: "plus-icon",
  82. variant: "link",
  83. className: "kc-role-attributes__plus-icon",
  84. onClick: () => append({key: "", value: ""}),
  85. icon: /* @__PURE__ */ React.createElement(PlusCircleIcon, null),
  86. isDisabled: !formState.isValid
  87. })))))), /* @__PURE__ */ React.createElement(ActionGroup, {
  88. className: "kc-role-attributes__action-group"
  89. }, /* @__PURE__ */ React.createElement(Button, {
  90. "data-testid": "realm-roles-save-button",
  91. variant: "primary",
  92. isDisabled: !watchFirstKey,
  93. onClick: save
  94. }, t("common:save")), /* @__PURE__ */ React.createElement(Button, {
  95. onClick: reset,
  96. variant: "link"
  97. }, t("common:reload"))));
  98. };