MultivaluedScopesComponent.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import React, {useState} from "../../_snowpack/pkg/react.js";
  2. import {useTranslation} from "../../_snowpack/pkg/react-i18next.js";
  3. import {Controller, useFormContext} from "../../_snowpack/pkg/react-hook-form.js";
  4. import {
  5. Button,
  6. Chip,
  7. ChipGroup,
  8. FormGroup,
  9. TextInput
  10. } from "../../_snowpack/pkg/@patternfly/react-core.js";
  11. import {HelpItem} from "../help-enabler/HelpItem.js";
  12. import {AddScopeDialog} from "../../clients/scopes/AddScopeDialog.js";
  13. import {useAdminClient, useFetch} from "../../context/auth/AdminClient.js";
  14. import {useParams} from "../../_snowpack/pkg/react-router.js";
  15. export const MultivaluedScopesComponent = ({
  16. defaultValue,
  17. name
  18. }) => {
  19. const {t} = useTranslation("dynamic");
  20. const {control} = useFormContext();
  21. const {conditionName} = useParams();
  22. const adminClient = useAdminClient();
  23. const [open, setOpen] = useState(false);
  24. const [clientScopes, setClientScopes] = useState([]);
  25. useFetch(() => adminClient.clientScopes.find(), (clientScopes2) => {
  26. setClientScopes(clientScopes2);
  27. }, []);
  28. const toggleModal = () => {
  29. setOpen(!open);
  30. };
  31. return /* @__PURE__ */ React.createElement(FormGroup, {
  32. label: t("realm-settings:clientScopesCondition"),
  33. id: "expected-scopes",
  34. labelIcon: /* @__PURE__ */ React.createElement(HelpItem, {
  35. helpText: t("realm-settings-help:clientScopesConditionTooltip"),
  36. fieldLabelId: "realm-settings:clientScopesCondition"
  37. }),
  38. fieldId: name
  39. }, /* @__PURE__ */ React.createElement(Controller, {
  40. name: `config.scopes`,
  41. control,
  42. defaultValue: [defaultValue],
  43. rules: {required: true},
  44. render: ({onChange, value}) => {
  45. return /* @__PURE__ */ React.createElement(React.Fragment, null, open && /* @__PURE__ */ React.createElement(AddScopeDialog, {
  46. clientScopes: clientScopes.filter((scope) => !value.includes(scope.name)),
  47. isClientScopesConditionType: true,
  48. open,
  49. toggleDialog: () => setOpen(!open),
  50. onAdd: (scopes) => {
  51. onChange([
  52. ...value,
  53. ...scopes.map((scope) => scope.scope).map((item) => item.name)
  54. ]);
  55. }
  56. }), value.length === 0 && !conditionName && /* @__PURE__ */ React.createElement(TextInput, {
  57. type: "text",
  58. id: "kc-scopes",
  59. value,
  60. "data-testid": "client-scope-input",
  61. name: "config.client-scopes",
  62. isDisabled: true
  63. }), /* @__PURE__ */ React.createElement(ChipGroup, {
  64. className: "kc-client-scopes-chip-group",
  65. isClosable: true,
  66. onClick: () => {
  67. onChange([]);
  68. }
  69. }, value.map((currentChip) => /* @__PURE__ */ React.createElement(Chip, {
  70. key: currentChip,
  71. onClick: () => {
  72. onChange(value.filter((item) => item !== currentChip));
  73. }
  74. }, currentChip))), /* @__PURE__ */ React.createElement(Button, {
  75. "data-testid": "select-scope-button",
  76. variant: "secondary",
  77. onClick: () => {
  78. toggleModal();
  79. }
  80. }, t("common:select")));
  81. }
  82. }));
  83. };