Introduced picker dialog (#4344)

This commit is contained in:
Erik Jan de Wit 2023-02-10 17:51:16 +01:00 committed by GitHub
parent fe2ed2c680
commit f184e57f1a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 78 additions and 32 deletions

View file

@ -5,9 +5,6 @@ import {
AlertVariant, AlertVariant,
Button, Button,
ButtonVariant, ButtonVariant,
Dropdown,
DropdownItem,
DropdownToggle,
InputGroup, InputGroup,
PageSection, PageSection,
TextInput, TextInput,
@ -30,6 +27,7 @@ import { KEY_PROVIDER_TYPE } from "../../util";
import useToggle from "../../utils/useToggle"; import useToggle from "../../utils/useToggle";
import { ProviderType, toKeyProvider } from "../routes/KeyProvider"; import { ProviderType, toKeyProvider } from "../routes/KeyProvider";
import { KeyProviderModal } from "./key-providers/KeyProviderModal"; import { KeyProviderModal } from "./key-providers/KeyProviderModal";
import { KeyProvidersPicker } from "./key-providers/KeyProvidersPicker";
import "../realm-settings-section.css"; import "../realm-settings-section.css";
@ -65,9 +63,8 @@ export const KeysProvidersTab = ({
const serverInfo = useServerInfo(); const serverInfo = useServerInfo();
const keyProviderComponentTypes = const keyProviderComponentTypes =
serverInfo.componentTypes?.[KEY_PROVIDER_TYPE] ?? []; serverInfo.componentTypes?.[KEY_PROVIDER_TYPE] ?? [];
const providerTypes = keyProviderComponentTypes.map((item) => item.id);
const [providerDropdownOpen, setProviderDropdownOpen] = useState(false); const [providerOpen, toggleProviderOpen] = useToggle();
const [defaultUIDisplayName, setDefaultUIDisplayName] = const [defaultUIDisplayName, setDefaultUIDisplayName] =
useState<ProviderType>(); useState<ProviderType>();
@ -140,6 +137,16 @@ export const KeysProvidersTab = ({
return ( return (
<> <>
{providerOpen && (
<KeyProvidersPicker
onClose={() => toggleProviderOpen()}
onConfirm={(provider) => {
handleModalToggle();
setDefaultUIDisplayName(provider as ProviderType);
toggleProviderOpen();
}}
/>
)}
{isCreateModalOpen && defaultUIDisplayName && ( {isCreateModalOpen && defaultUIDisplayName && (
<KeyProviderModal <KeyProviderModal
providerType={defaultUIDisplayName} providerType={defaultUIDisplayName}
@ -175,35 +182,13 @@ export const KeysProvidersTab = ({
</InputGroup> </InputGroup>
</ToolbarItem> </ToolbarItem>
<ToolbarItem> <ToolbarItem>
<Dropdown <Button
data-testid="addProviderDropdown" data-testid="addProviderDropdown"
className="add-provider-dropdown" className="add-provider-dropdown"
isOpen={providerDropdownOpen} onClick={() => toggleProviderOpen()}
toggle={ >
<DropdownToggle {t("addProvider")}
onToggle={(val) => setProviderDropdownOpen(val)} </Button>
isPrimary
>
{t("addProvider")}
</DropdownToggle>
}
dropdownItems={[
providerTypes.map((item) => (
<DropdownItem
onClick={() => {
handleModalToggle();
setProviderDropdownOpen(false);
setDefaultUIDisplayName(item as ProviderType);
}}
data-testid={`option-${item}`}
key={item}
>
{item}
</DropdownItem>
)),
]}
/>
</ToolbarItem> </ToolbarItem>
</ToolbarGroup> </ToolbarGroup>
</Toolbar> </Toolbar>

View file

@ -0,0 +1,61 @@
import {
DataList,
DataListCell,
DataListItem,
DataListItemCells,
DataListItemRow,
Modal,
} from "@patternfly/react-core";
import { useTranslation } from "react-i18next";
import { useServerInfo } from "../../../context/server-info/ServerInfoProvider";
import { KEY_PROVIDER_TYPE } from "../../../util";
type KeyProvidersPickerProps = {
onConfirm: (provider: string) => void;
onClose: () => void;
};
export const KeyProvidersPicker = ({
onConfirm,
onClose,
}: KeyProvidersPickerProps) => {
const { t } = useTranslation("realm-settings");
const serverInfo = useServerInfo();
const keyProviderComponentTypes =
serverInfo.componentTypes?.[KEY_PROVIDER_TYPE] ?? [];
return (
<Modal variant="medium" title={t("addProvider")} isOpen onClose={onClose}>
<DataList
onSelectDataListItem={(id) => {
onConfirm(id);
}}
aria-label={t("addPredefinedMappers")}
isCompact
>
{keyProviderComponentTypes.map((provider) => (
<DataListItem
aria-label={provider.id}
key={provider.id}
id={provider.id}
>
<DataListItemRow>
<DataListItemCells
dataListCells={[
<DataListCell
key={`name-${provider.id}`}
data-testid={`option-${provider.id}`}
>
{provider.id}
</DataListCell>,
<DataListCell width={2} key={`helpText-${provider.helpText}`}>
{provider.helpText}
</DataListCell>,
]}
/>
</DataListItemRow>
</DataListItem>
))}
</DataList>
</Modal>
);
};