-
Notifications
You must be signed in to change notification settings - Fork 26
Add 'Permissions' main page #1145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
duzda
wants to merge
2
commits into
freeipa:main
Choose a base branch
from
duzda:permissions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,344 @@ | ||
| import React, { useEffect, useRef, useState } from "react"; | ||
| import { | ||
| Select, | ||
| SelectOption, | ||
| SelectList, | ||
| SelectOptionProps, | ||
| MenuToggle, | ||
| MenuToggleElement, | ||
| TextInputGroup, | ||
| TextInputGroupMain, | ||
| TextInputGroupUtilities, | ||
| Button, | ||
| } from "@patternfly/react-core"; | ||
| import { CloseIcon } from "@patternfly/react-icons"; | ||
|
|
||
| type CreationProps<T> = { | ||
| // Whenever this property changes, the options will be reset | ||
| onChangeTarget: T; | ||
| }; | ||
|
|
||
| type TypeAheadWithCheckboxProps<T> = { | ||
| id: string; | ||
| dataCy: string; | ||
| options: SelectOptionProps[]; | ||
| selected: string[]; | ||
| setSelected: (selected: string[]) => void; | ||
| creationProps?: CreationProps<T>; | ||
| }; | ||
|
|
||
| const NO_RESULTS = "no results"; | ||
| const CREATE_NEW = "create"; | ||
|
|
||
| export const TypeAheadWithCheckbox = <T,>({ | ||
| id, | ||
| dataCy, | ||
| options, | ||
| selected, | ||
| setSelected, | ||
| creationProps, | ||
| }: TypeAheadWithCheckboxProps<T>) => { | ||
| // This is to allow mutations to options property | ||
| const [localOptions, setLocalOptions] = | ||
| useState<SelectOptionProps[]>(options); | ||
| // This one is actually shown, it can contain Create New options and no results | ||
| const [availableOptions, setAvailableOptions] = | ||
| useState<SelectOptionProps[]>(localOptions); | ||
| const [isOpen, setIsOpen] = useState(false); | ||
| const [inputValue, setInputValue] = useState<string>(""); | ||
| const [focusedItemIndex, setFocusedItemIndex] = useState<number | null>(null); | ||
| const [activeItemId, setActiveItemId] = useState<string | null>(null); | ||
| const textInputRef = useRef<HTMLInputElement>(undefined); | ||
|
|
||
| const [previousTarget, setPreviousTarget] = useState<T | null>(null); | ||
| const allowCreation = creationProps !== undefined; | ||
|
|
||
| if (allowCreation && previousTarget !== creationProps?.onChangeTarget) { | ||
| setPreviousTarget(creationProps.onChangeTarget); | ||
| setLocalOptions(options); | ||
| setInputValue(""); | ||
| } | ||
|
|
||
| useEffect(() => { | ||
| let newSelectOptions: SelectOptionProps[] = localOptions; | ||
|
|
||
| // Filter menu items based on the text input value when one exists | ||
| if (inputValue) { | ||
| newSelectOptions = localOptions.filter((menuItem) => | ||
| String(menuItem.children) | ||
| .toLowerCase() | ||
| .includes(inputValue.toLowerCase()) | ||
| ); | ||
|
|
||
| // If no option matches the filter exactly, display creation option | ||
| if (allowCreation) { | ||
| if (!localOptions.some((option) => option.value === inputValue)) { | ||
| newSelectOptions = [ | ||
| ...newSelectOptions, | ||
| { | ||
| children: `Create new option "${inputValue}"`, | ||
| value: CREATE_NEW, | ||
| "data-cy": `${dataCy}-create-new-option`, | ||
| }, | ||
| ]; | ||
| } | ||
| } | ||
|
|
||
| // When no options are found after filtering, display 'No results found' | ||
| if (newSelectOptions.length === 0) { | ||
| newSelectOptions = [ | ||
| { | ||
| "data-cy": `${dataCy}-no-results`, | ||
| isAriaDisabled: true, | ||
| children: `No results found for "${inputValue}"`, | ||
| value: NO_RESULTS, | ||
| hasCheckbox: false, | ||
| }, | ||
| ]; | ||
| } | ||
| } | ||
|
|
||
| // This sucks, but we're forced to, due to how isOpenCallback | ||
| // works, with memo the we onOpenChange is fired | ||
| // eslint-disable-next-line @eslint-react/hooks-extra/no-direct-set-state-in-use-effect | ||
| setAvailableOptions(newSelectOptions); | ||
| // We mutate options, if we don't, we end up with the same issue as above... | ||
| }, [inputValue, localOptions]); | ||
|
|
||
| const placeholder = `${selected.length} item${selected.length !== 1 ? "s" : ""} selected`; | ||
|
|
||
| const createItemId = (value: string) => | ||
| `select-multi-typeahead-${value.replace(" ", "-")}`; | ||
|
|
||
| const setActiveAndFocusedItem = (itemIndex: number) => { | ||
| setFocusedItemIndex(itemIndex); | ||
| const focusedItem = availableOptions[itemIndex]; | ||
| setActiveItemId(createItemId(focusedItem.value)); | ||
| }; | ||
|
|
||
| const resetActiveAndFocusedItem = () => { | ||
| setFocusedItemIndex(null); | ||
| setActiveItemId(null); | ||
| }; | ||
|
|
||
| const closeMenu = () => { | ||
| setIsOpen(false); | ||
| resetActiveAndFocusedItem(); | ||
| }; | ||
|
|
||
| const onInputClick = () => { | ||
| if (!isOpen) { | ||
| setIsOpen(true); | ||
| } else if (!inputValue) { | ||
| closeMenu(); | ||
| } | ||
| }; | ||
|
|
||
| const handleMenuArrowKeys = (key: string) => { | ||
| let indexToFocus = 0; | ||
|
|
||
| if (!isOpen) { | ||
| setIsOpen(true); | ||
| } | ||
|
|
||
| if (availableOptions.every((option) => option.isDisabled)) { | ||
| return; | ||
| } | ||
|
|
||
| if (key === "ArrowUp") { | ||
| // When no index is set or at the first index, focus to the last, otherwise decrement focus index | ||
| if (focusedItemIndex === null || focusedItemIndex === 0) { | ||
| indexToFocus = availableOptions.length - 1; | ||
| } else { | ||
| indexToFocus = focusedItemIndex - 1; | ||
| } | ||
|
|
||
| // Skip disabled options | ||
| while (availableOptions[indexToFocus].isDisabled) { | ||
| indexToFocus--; | ||
| if (indexToFocus === -1) { | ||
| indexToFocus = availableOptions.length - 1; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (key === "ArrowDown") { | ||
| // When no index is set or at the last index, focus to the first, otherwise increment focus index | ||
| if ( | ||
| focusedItemIndex === null || | ||
| focusedItemIndex === availableOptions.length - 1 | ||
| ) { | ||
| indexToFocus = 0; | ||
| } else { | ||
| indexToFocus = focusedItemIndex + 1; | ||
| } | ||
|
|
||
| // Skip disabled options | ||
| while (availableOptions[indexToFocus].isDisabled) { | ||
| indexToFocus++; | ||
| if (indexToFocus === availableOptions.length) { | ||
| indexToFocus = 0; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| setActiveAndFocusedItem(indexToFocus); | ||
| }; | ||
|
|
||
| const onInputKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => { | ||
| const focusedItem = | ||
| focusedItemIndex !== null ? availableOptions[focusedItemIndex] : null; | ||
|
|
||
| switch (event.key) { | ||
| case "Enter": | ||
| if ( | ||
| isOpen && | ||
| focusedItem && | ||
| focusedItem.value !== NO_RESULTS && | ||
| !focusedItem.isAriaDisabled | ||
| ) { | ||
| onSelect(focusedItem.value); | ||
| } | ||
|
|
||
| if (!isOpen) { | ||
| setIsOpen(true); | ||
| } | ||
|
|
||
| break; | ||
| case "ArrowUp": | ||
| case "ArrowDown": | ||
| event.preventDefault(); | ||
| handleMenuArrowKeys(event.key); | ||
| break; | ||
| } | ||
| }; | ||
|
|
||
| const onToggleClick = () => { | ||
| setIsOpen(!isOpen); | ||
| textInputRef?.current?.focus(); | ||
| }; | ||
|
|
||
| const onTextInputChange = ( | ||
| _event: React.FormEvent<HTMLInputElement>, | ||
| value: string | ||
| ) => { | ||
| setInputValue(value); | ||
| if (value !== "" && !isOpen) setIsOpen(true); | ||
| resetActiveAndFocusedItem(); | ||
| }; | ||
|
|
||
| const onSelect = (value: string) => { | ||
| if (value && value !== NO_RESULTS) { | ||
| if (value === CREATE_NEW) { | ||
| if (!availableOptions.some((item) => item.value === inputValue)) { | ||
| setLocalOptions([ | ||
| ...localOptions, | ||
| { | ||
| value: inputValue, | ||
| children: inputValue, | ||
| "data-cy": `${dataCy}-${inputValue}-create-new-option`, | ||
| }, | ||
| ]); | ||
| } | ||
| setSelected( | ||
| selected.includes(inputValue) | ||
| ? selected.filter((selection) => selection !== inputValue) | ||
| : [...selected, inputValue] | ||
| ); | ||
| resetActiveAndFocusedItem(); | ||
| } else { | ||
| setSelected( | ||
| selected.includes(value) | ||
| ? selected.filter((selection) => selection !== value) | ||
| : [...selected, value] | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| textInputRef.current?.focus(); | ||
| }; | ||
|
|
||
| const onClearButtonClick = () => { | ||
| setSelected([]); | ||
| setInputValue(""); | ||
| resetActiveAndFocusedItem(); | ||
| textInputRef?.current?.focus(); | ||
| }; | ||
|
|
||
| const toggle = (toggleRef: React.Ref<MenuToggleElement>) => ( | ||
| <MenuToggle | ||
| data-cy={`${dataCy}-multi-typeahead-checkbox-menu-toggle`} | ||
| variant="typeahead" | ||
| aria-label="Multi typeahead checkbox menu toggle" | ||
| onClick={onToggleClick} | ||
| innerRef={toggleRef} | ||
| isExpanded={isOpen} | ||
| isFullWidth | ||
| > | ||
| <TextInputGroup isPlain> | ||
| <TextInputGroupMain | ||
| value={inputValue} | ||
| onClick={onInputClick} | ||
| onChange={onTextInputChange} | ||
| onKeyDown={onInputKeyDown} | ||
| id={`${id}-multi-typeahead-select-checkbox-input`} | ||
| autoComplete="off" | ||
| innerRef={textInputRef} | ||
| placeholder={placeholder} | ||
| {...(activeItemId && { "aria-activedescendant": activeItemId })} | ||
| role="combobox" | ||
| isExpanded={isOpen} | ||
| aria-controls={`${id}-select-multi-typeahead-checkbox-listbox`} | ||
| /> | ||
| <TextInputGroupUtilities | ||
| {...(selected.length === 0 ? { style: { display: "none" } } : {})} | ||
| > | ||
| <Button | ||
| data-cy={`${dataCy}-multi-typeahead-checkbox-clear-button`} | ||
| variant="plain" | ||
| onClick={onClearButtonClick} | ||
| aria-label="Clear input value" | ||
| icon={<CloseIcon />} | ||
| /> | ||
| </TextInputGroupUtilities> | ||
| </TextInputGroup> | ||
| </MenuToggle> | ||
| ); | ||
|
|
||
| return ( | ||
| <Select | ||
| data-cy={`${dataCy}-multi-typeahead-checkbox-select`} | ||
| role="menu" | ||
| id={`${id}-multi-typeahead-checkbox-select`} | ||
| isOpen={isOpen} | ||
| selected={selected} | ||
| onSelect={(_event, selection) => onSelect(selection as string)} | ||
| onOpenChange={(isOpen) => { | ||
| if (!isOpen) closeMenu(); | ||
| }} | ||
| toggle={toggle} | ||
| variant="typeahead" | ||
| > | ||
| <SelectList | ||
| isAriaMultiselectable | ||
| id={`${id}-select-multi-typeahead-checkbox-listbox`} | ||
| > | ||
| {availableOptions.map((option, index) => ( | ||
| <SelectOption | ||
| key={option.value || option.children} | ||
| {...(!option.isDisabled && | ||
| !option.isAriaDisabled && { hasCheckbox: true })} | ||
| isSelected={selected.includes(option.value)} | ||
| isFocused={focusedItemIndex === index} | ||
| className={option.className} | ||
| id={`${id}-${createItemId(option.value)}`} | ||
| {...option} | ||
| ref={null} | ||
| > | ||
| {option.children} | ||
| </SelectOption> | ||
| ))} | ||
| </SelectList> | ||
| </Select> | ||
| ); | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.