-
Notifications
You must be signed in to change notification settings - Fork 498
Improving the azure browse filter dropdown logic #18281
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9c4b359
consolidating filter logic
Benjin 793547b
Merge branch 'main' into dev/benjin/improveAzureFiltering
Benjin 1fa112b
checkpoint
Benjin 783db51
correcting filter autoselection logic
Benjin a6b3adf
cleanup
Benjin 42e56c1
Merge branch 'main' into dev/benjin/improveAzureFiltering
Benjin dcb8c37
resetting list of servers when subscription filter changes
Benjin 83fc6ca
swapping default selection flag for enum
Benjin 0069a0e
light refactoring
Benjin 8479ba5
updating comment
Benjin 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
/** Behavior for how the default selection is determined */ | ||
export enum DefaultSelectionMode { | ||
/** If there are any options, the first is always selected. Otherwise, selects nothing. */ | ||
SelectFirstIfAny, | ||
/** Always selects nothing, regardless of if there are available options */ | ||
AlwaysSelectNone, | ||
/** Selects the only option if there's only one. Otherwise (many or no options) selects nothing. */ | ||
SelectOnlyOrNone, | ||
} | ||
|
||
export function updateComboboxSelection( | ||
/** current selected (valid) option */ | ||
selected: string | undefined, | ||
/** callback to set the selected (valid) option */ | ||
setSelected: (s: string | undefined) => void, | ||
/** callback to set the displayed value (not guaranteed to be valid if the user has manually typed something) */ | ||
setValue: (v: string) => void, | ||
/** list of valid options */ | ||
optionList: string[], | ||
/** behavior for choosing the default selected value */ | ||
defaultSelectionMode: DefaultSelectionMode = DefaultSelectionMode.AlwaysSelectNone, | ||
) { | ||
// if there is no current selection or if the current selection is no longer in the list of options (due to filter changes), | ||
// then select the only option if there is only one option, then make a default selection according to specified `defaultSelectionMode` | ||
|
||
if ( | ||
selected === undefined || | ||
(selected && !optionList.includes(selected)) | ||
) { | ||
let optionToSelect: string | undefined = undefined; | ||
|
||
if (optionList.length > 0) { | ||
switch (defaultSelectionMode) { | ||
case DefaultSelectionMode.SelectFirstIfAny: | ||
optionToSelect = | ||
optionList.length > 0 ? optionList[0] : undefined; | ||
break; | ||
case DefaultSelectionMode.SelectOnlyOrNone: | ||
optionToSelect = | ||
optionList.length === 1 ? optionList[0] : undefined; | ||
break; | ||
case DefaultSelectionMode.AlwaysSelectNone: | ||
default: | ||
optionToSelect = undefined; | ||
} | ||
} | ||
|
||
setSelected(optionToSelect); // selected value's unselected state should be undefined | ||
setValue(optionToSelect ?? ""); // displayed value's unselected state should be an empty string | ||
} | ||
} |
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
132 changes: 132 additions & 0 deletions
132
src/reactviews/pages/ConnectionDialog/AzureFilterCombobox.component.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,132 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { | ||
Combobox, | ||
ComboboxProps, | ||
Field, | ||
makeStyles, | ||
OptionOnSelectData, | ||
SelectionEvents, | ||
Option, | ||
} from "@fluentui/react-components"; | ||
import { useFormStyles } from "../../common/forms/form.component"; | ||
import { useEffect, useState } from "react"; | ||
|
||
const useFieldDecorationStyles = makeStyles({ | ||
decoration: { | ||
display: "flex", | ||
alignItems: "center", | ||
columnGap: "4px", | ||
}, | ||
}); | ||
|
||
export const AzureFilterCombobox = ({ | ||
label, | ||
required, | ||
clearable, | ||
content, | ||
decoration, | ||
props, | ||
}: { | ||
label: string; | ||
required?: boolean; | ||
clearable?: boolean; | ||
content: { | ||
/** list of valid values for the combo box */ | ||
valueList: string[]; | ||
/** currently-selected value from `valueList` */ | ||
selection?: string; | ||
/** callback when the user has selected a value from `valueList` */ | ||
setSelection: (value: string | undefined) => void; | ||
/** currently-entered text in the combox, may not be a valid selection value if the user is typing */ | ||
value: string; | ||
/** callback when the user types in the combobox */ | ||
setValue: (value: string) => void; | ||
/** placeholder text for the combobox */ | ||
placeholder?: string; | ||
/** message displayed if focus leaves this combobox and `value` is not a valid value from `valueList` */ | ||
invalidOptionErrorMessage: string; | ||
}; | ||
decoration?: JSX.Element; | ||
props?: Partial<ComboboxProps>; | ||
}) => { | ||
const formStyles = useFormStyles(); | ||
const decorationStyles = useFieldDecorationStyles(); | ||
const [validationMessage, setValidationMessage] = useState<string>(""); | ||
|
||
// clear validation message as soon as value is valid | ||
useEffect(() => { | ||
if (content.valueList.includes(content.value)) { | ||
setValidationMessage(""); | ||
} | ||
}, [content.value]); | ||
|
||
// only display validation error if focus leaves the field and the value is not valid | ||
const onBlur = () => { | ||
if (content.value) { | ||
setValidationMessage( | ||
content.valueList.includes(content.value) | ||
? "" | ||
: content.invalidOptionErrorMessage, | ||
); | ||
} | ||
}; | ||
|
||
const onOptionSelect: ( | ||
_: SelectionEvents, | ||
data: OptionOnSelectData, | ||
) => void = (_, data: OptionOnSelectData) => { | ||
content.setSelection( | ||
data.selectedOptions.length > 0 ? data.selectedOptions[0] : "", | ||
); | ||
content.setValue(data.optionText ?? ""); | ||
}; | ||
|
||
function onInput(ev: React.ChangeEvent<HTMLInputElement>) { | ||
content.setValue(ev.target.value); | ||
} | ||
|
||
return ( | ||
<div className={formStyles.formComponentDiv}> | ||
<Field | ||
label={ | ||
decoration ? ( | ||
<div className={decorationStyles.decoration}> | ||
{label} | ||
{decoration} | ||
</div> | ||
) : ( | ||
label | ||
) | ||
} | ||
orientation="horizontal" | ||
required={required} | ||
validationMessage={validationMessage} | ||
onBlur={onBlur} | ||
> | ||
<Combobox | ||
{...props} | ||
value={content.value} | ||
selectedOptions={ | ||
content.selection ? [content.selection] : [] | ||
} | ||
onInput={onInput} | ||
onOptionSelect={onOptionSelect} | ||
placeholder={content.placeholder} | ||
clearable={clearable} | ||
> | ||
{content.valueList.map((val, idx) => { | ||
return ( | ||
<Option key={idx} value={val}> | ||
{val} | ||
</Option> | ||
); | ||
})} | ||
</Combobox> | ||
</Field> | ||
</div> | ||
); | ||
}; |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should create a helper function to track time. It would return another function that, when called, automatically measures the elapsed time and sends the action event with the mentioned properties.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this thought. I'll make an issue to add some util stuff like that.