This repository was archived by the owner on Jul 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 374
feature: Added resource group picker #6599
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e59c40f
Added resource group picker
GeoffCoxMSFT 8f07a88
PR updates
GeoffCoxMSFT 380d837
Manual pre-merge
GeoffCoxMSFT 3109cdd
Merge branch 'main' into gcox/feature5954
GeoffCoxMSFT 26dee6d
Updated strings.
GeoffCoxMSFT ac32e73
PR updates
GeoffCoxMSFT 083ea3e
setCurrentSubscription
GeoffCoxMSFT dc205b2
Turn back on first page
GeoffCoxMSFT 7da48d0
Merge branch 'main' into gcox/feature5954
GeoffCoxMSFT 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
204 changes: 204 additions & 0 deletions
204
extensions/azurePublish/src/components/ResourceGroupPicker.tsx
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,204 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| import * as React from 'react'; | ||
| import formatMessage from 'format-message'; | ||
| import { Dropdown, IDropdownOption } from 'office-ui-fabric-react/lib/Dropdown'; | ||
| import { Icon } from 'office-ui-fabric-react/lib/Icon'; | ||
| import { Stack } from 'office-ui-fabric-react/lib/Stack'; | ||
| import { TextField } from 'office-ui-fabric-react/lib/TextField'; | ||
| import { FluentTheme } from '@uifabric/fluent-theme'; | ||
| import { TooltipHost } from 'office-ui-fabric-react/lib/Tooltip'; | ||
|
|
||
| import { useDebounce } from './useDebounce'; | ||
|
|
||
| const stackStyles = { root: { marginBottom: '8px' } }; | ||
| const dropdownStyles = { root: { marginBottom: '8px' }, dropdown: { width: '100%' } }; | ||
| const itemIconStyles = { marginRight: '8px' }; | ||
| const newNameTextFileStyles = { root: { marginTop: '10px' } }; | ||
|
|
||
| const getInfoIconStyle = (required) => { | ||
| return { | ||
| root: { | ||
| selectors: { | ||
| '&::before': { | ||
| content: required ? " '*'" : '', | ||
| color: FluentTheme.palette.red, | ||
| paddingRight: 3, | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
| }; | ||
|
|
||
| const CREATE_NEW_KEY = 'CREATE_NEW'; | ||
|
|
||
| const createNewOption: IDropdownOption = { | ||
| key: CREATE_NEW_KEY, | ||
| data: { iconName: 'Add' }, | ||
| text: formatMessage('Create new'), | ||
| }; | ||
|
|
||
| type ResourceGroupItemChoice = { | ||
| name: string; | ||
| isNew: boolean; | ||
| errorMessage?: string; | ||
| }; | ||
|
|
||
| type Props = { | ||
| /** | ||
| * The resource groups to choose from. | ||
| * Set to undefined to disable this picker. | ||
| */ | ||
| resourceGroupNames?: string[]; | ||
| /** | ||
| * The selected name of the existing resource. | ||
| * When undefined, the 'Create new' option will be selected. | ||
| */ | ||
| selectedResourceGroupName?: string; | ||
| /** | ||
| * The name chosen for a new resource group. | ||
| * Used when the 'Create new' option is selected. | ||
| */ | ||
| newResourceGroupName?: string; | ||
| /** | ||
| * Called when the selection or new resource name changes. | ||
| */ | ||
| onChange: (choice: ResourceGroupItemChoice) => void; | ||
| }; | ||
|
|
||
| const onRenderLabel = (props) => { | ||
| return ( | ||
| <div | ||
| style={{ | ||
| display: 'flex', | ||
| flexDirection: 'row', | ||
| marginBottom: '5px', | ||
| }} | ||
| > | ||
| <div | ||
| style={{ | ||
| marginRight: '5px', | ||
| fontWeight: 600, | ||
| fontSize: '14px', | ||
| }} | ||
| > | ||
| {` ${props.label} `} | ||
| </div> | ||
| <TooltipHost content={props.ariaLabel}> | ||
| <Icon iconName="Info" styles={getInfoIconStyle(props.required)} /> | ||
| </TooltipHost> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export const ResourceGroupPicker = ({ | ||
| resourceGroupNames, | ||
| selectedResourceGroupName: controlledSelectedName, | ||
| newResourceGroupName: controlledNewName, | ||
| onChange, | ||
| }: Props) => { | ||
| // ----- Hooks -----// | ||
| const [selectedName, setSelectedName] = React.useState(controlledSelectedName || CREATE_NEW_KEY); | ||
| const [newName, setNewName] = React.useState(controlledNewName); | ||
| const debouncedNewName = useDebounce<string>(newName, 300); | ||
| const [newNameErrorMessage, setNewNameErrorMessage] = React.useState(''); | ||
|
|
||
| React.useEffect(() => { | ||
| setSelectedName(controlledSelectedName || CREATE_NEW_KEY); | ||
| }, [controlledSelectedName]); | ||
|
|
||
| React.useEffect(() => { | ||
| setNewName(controlledNewName || ''); | ||
| }, [controlledNewName]); | ||
|
|
||
| React.useEffect(() => { | ||
| const alreadyExists = resourceGroupNames?.some((name) => name === debouncedNewName); | ||
|
|
||
| if (debouncedNewName && !debouncedNewName.match(/^[-\w._()]+$/)) { | ||
| setNewNameErrorMessage( | ||
| formatMessage( | ||
| 'Resource group names only allow alphanumeric characters, periods, underscores, hyphens and parenthesis and cannot end in a period.' | ||
| ) | ||
| ); | ||
| } else if (alreadyExists) { | ||
| setNewNameErrorMessage(formatMessage('A resource with this name already exists.')); | ||
| } else { | ||
| setNewNameErrorMessage(undefined); | ||
| } | ||
| }, [debouncedNewName, resourceGroupNames]); | ||
|
|
||
| React.useEffect(() => { | ||
| const isNew = selectedName === CREATE_NEW_KEY; | ||
| onChange({ | ||
| isNew, | ||
| name: isNew ? debouncedNewName : selectedName, | ||
| errorMessage: isNew ? newNameErrorMessage : undefined, | ||
| }); | ||
| }, [selectedName, debouncedNewName, newNameErrorMessage]); | ||
|
|
||
| const options = React.useMemo(() => { | ||
| const optionsList: IDropdownOption[] = | ||
| resourceGroupNames?.map((p) => { | ||
| return { key: p, text: p }; | ||
| }) || []; | ||
|
|
||
| optionsList.unshift(createNewOption); | ||
| return optionsList; | ||
| }, [resourceGroupNames]); | ||
|
|
||
| // ----- Render -----// | ||
|
|
||
| const loading = resourceGroupNames === undefined; | ||
|
|
||
| const onRenderOption = (option) => { | ||
| return ( | ||
| <div> | ||
| {option.data?.iconName && ( | ||
| <Icon aria-hidden="true" iconName={option.data.iconName} style={itemIconStyles} title={option.text} /> | ||
| )} | ||
| <span>{option.text}</span> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| return ( | ||
| <Stack styles={stackStyles}> | ||
| <Dropdown | ||
| required | ||
| ariaLabel={formatMessage( | ||
| 'A resource group is a collection of resources that share the same lifecycle, permissions, and policies' | ||
| )} | ||
| disabled={loading} | ||
| label={formatMessage('Resource group')} | ||
| options={options} | ||
| placeholder={formatMessage('Select one')} | ||
| selectedKey={selectedName} | ||
| styles={dropdownStyles} | ||
| onChange={(_, opt) => { | ||
| setSelectedName(opt.key as string); | ||
| }} | ||
| onRenderLabel={onRenderLabel} | ||
| onRenderOption={onRenderOption} | ||
| /> | ||
| {selectedName === CREATE_NEW_KEY && ( | ||
| <TextField | ||
| required | ||
| ariaLabel={formatMessage('Enter a name for the new resource group')} | ||
| data-testid={'newResourceGroupName'} | ||
| disabled={loading} | ||
| errorMessage={newNameErrorMessage} | ||
| id={'newResourceGroupName'} | ||
| label={formatMessage('Resource group name')} | ||
| placeholder={formatMessage('New resource group')} | ||
| styles={newNameTextFileStyles} | ||
| value={newName} | ||
| onChange={(e, val) => { | ||
| setNewName(val || ''); | ||
| }} | ||
| onRenderLabel={onRenderLabel} | ||
| /> | ||
| )} | ||
| </Stack> | ||
| ); | ||
| }; | ||
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.