-
Notifications
You must be signed in to change notification settings - Fork 594
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8ef3b38
commit 6e1f224
Showing
27 changed files
with
914 additions
and
32 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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
87 changes: 87 additions & 0 deletions
87
frontend/packages/console-app/src/components/network-policies/NADsSelector.tsx
This file contains 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,87 @@ | ||
import * as React from 'react'; | ||
import { Alert, AlertVariant, FormGroup } from '@patternfly/react-core'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { | ||
getGroupVersionKindForModel, | ||
useK8sWatchResource, | ||
} from '@console/dynamic-plugin-sdk/src/lib-core'; | ||
import { Loading } from '@console/internal/components/utils'; | ||
import { NetworkAttachmentDefinitionModel } from '@console/network-attachment-definition-plugin/src'; | ||
import { NetworkAttachmentDefinitionKind } from '@console/network-attachment-definition-plugin/src/types'; | ||
import { getName, getNamespace } from '@console/shared/src'; | ||
import { NetworkPolicy } from './network-policy-model'; | ||
import SelectMultiTypeahead from './SelectMultiTypeahead/SelectMultiTypeahead'; | ||
|
||
type NADsSelectorProps = { | ||
namespace: string; | ||
networkPolicy: NetworkPolicy; | ||
onPolicyChange: (policy: NetworkPolicy) => void; | ||
}; | ||
|
||
const NetworkAttachmentDefinitionModelGroupVersionKind = getGroupVersionKindForModel( | ||
NetworkAttachmentDefinitionModel, | ||
); | ||
|
||
const NADsSelector: React.FC<NADsSelectorProps> = ({ | ||
namespace, | ||
networkPolicy, | ||
onPolicyChange, | ||
}) => { | ||
const { t } = useTranslation(); | ||
|
||
const [nads, loaded, loadError] = useK8sWatchResource<NetworkAttachmentDefinitionKind[]>({ | ||
groupVersionKind: NetworkAttachmentDefinitionModelGroupVersionKind, | ||
isList: true, | ||
namespace, | ||
}); | ||
|
||
const [nadsDefault, loadedDefaultNads, loadErrorDefaultNads] = useK8sWatchResource< | ||
NetworkAttachmentDefinitionKind[] | ||
>( | ||
namespace !== 'default' | ||
? { | ||
groupVersionKind: NetworkAttachmentDefinitionModelGroupVersionKind, | ||
isList: true, | ||
namespace: 'default', | ||
} | ||
: null, | ||
); | ||
|
||
const nadsOptions = React.useMemo(() => { | ||
const allNads = [...(nads || []), ...(nadsDefault || [])]; | ||
|
||
return allNads.map((nad) => ({ | ||
value: `${getNamespace(nad)}/${getName(nad)}`, | ||
})); | ||
}, [nads, nadsDefault]); | ||
|
||
const onChange = (newNADs: string[]) => { | ||
onPolicyChange({ ...networkPolicy, policyFor: newNADs }); | ||
}; | ||
|
||
if (!loaded || !loadedDefaultNads) return <Loading />; | ||
|
||
if (loadError || loadErrorDefaultNads) | ||
return ( | ||
<Alert title={t('Error')} variant={AlertVariant.danger}> | ||
{loadError} | ||
</Alert> | ||
); | ||
|
||
return ( | ||
<FormGroup | ||
fieldId="multi-networkpolicy-policyfor" | ||
isRequired | ||
label={t('console-app~Policy for')} | ||
> | ||
<SelectMultiTypeahead | ||
options={nadsOptions} | ||
placeholder={t('console-app~Select one or more NetworkAttachmentDefinitions')} | ||
selected={networkPolicy.policyFor || []} | ||
setSelected={onChange} | ||
/> | ||
</FormGroup> | ||
); | ||
}; | ||
|
||
export default NADsSelector; |
This file contains 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
91 changes: 91 additions & 0 deletions
91
...console-app/src/components/network-policies/SelectMultiTypeahead/SelectMultiTypeahead.tsx
This file contains 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,91 @@ | ||
import * as React from 'react'; | ||
import { | ||
MenuToggleElement, | ||
Select, | ||
SelectList, | ||
SelectOption, | ||
SelectOptionProps, | ||
} from '@patternfly/react-core'; | ||
import { NO_RESULTS_VALUE } from './constants'; | ||
import Toggle from './Toggle'; | ||
import { filterOptions } from './utils'; | ||
|
||
type SelectMultiTypeaheadProps = { | ||
options: SelectOptionProps[]; | ||
placeholder: string; | ||
selected: string[]; | ||
setSelected: (newSelection: string[]) => void; | ||
}; | ||
|
||
const SelectMultiTypeahead: React.FC<SelectMultiTypeaheadProps> = ({ | ||
options, | ||
placeholder, | ||
selected, | ||
setSelected, | ||
}) => { | ||
const [isOpen, setIsOpen] = React.useState(false); | ||
const [inputValue, setInputValue] = React.useState<string>(''); | ||
const [focusedItemIndex, setFocusedItemIndex] = React.useState<null | number>(null); | ||
const textInputRef = React.useRef<HTMLInputElement>(); | ||
|
||
const selectOptions = inputValue ? filterOptions(options, inputValue) : options; | ||
|
||
const onSelect = (value: string) => { | ||
if (value && value !== NO_RESULTS_VALUE) { | ||
setSelected( | ||
selected.includes(value) | ||
? selected.filter((selection) => selection !== value) | ||
: [...selected, value], | ||
); | ||
|
||
setIsOpen(true); | ||
} | ||
|
||
textInputRef.current?.focus(); | ||
}; | ||
|
||
return ( | ||
<Select | ||
id="multi-typeahead-select" | ||
isOpen={isOpen} | ||
onOpenChange={() => setIsOpen(false)} | ||
onSelect={(event?: React.MouseEvent<Element, MouseEvent>, selection?: string | number) => | ||
onSelect(selection as string) | ||
} | ||
selected={selected} | ||
toggle={(toggleRef: React.Ref<MenuToggleElement>) => ( | ||
<Toggle | ||
focusedItemIndex={focusedItemIndex} | ||
inputValue={inputValue} | ||
isOpen={isOpen} | ||
onSelect={onSelect} | ||
placeholder={placeholder} | ||
selected={selected} | ||
selectOptions={selectOptions} | ||
setFocusedItemIndex={setFocusedItemIndex} | ||
setInputValue={setInputValue} | ||
setIsOpen={setIsOpen} | ||
setSelected={setSelected} | ||
textInputRef={textInputRef} | ||
toggleRef={toggleRef} | ||
/> | ||
)} | ||
> | ||
<SelectList id="select-multi-typeahead-listbox" isAriaMultiselectable> | ||
{selectOptions.map((option, index) => ( | ||
<SelectOption | ||
className={option.className} | ||
id={`select-multi-typeahead-${option.value.replace(' ', '-')}`} | ||
isFocused={focusedItemIndex === index} | ||
key={option.value || option.children} | ||
{...option} | ||
> | ||
{option.children || option.value} | ||
</SelectOption> | ||
))} | ||
</SelectList> | ||
</Select> | ||
); | ||
}; | ||
|
||
export default SelectMultiTypeahead; |
Oops, something went wrong.