Skip to content
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

Fix BasePicker's onDismiss behavior from changing if getTextFromItem provided #14302

Merged
merged 3 commits into from
Aug 12, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions change/office-ui-fabric-react-2020-08-03-01-23-54-master.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "minor",
"comment": "BasePicker's onDismiss can now return a boolean to decide if an item is selected",
"packageName": "office-ui-fabric-react",
"email": "[email protected]",
"dependentChangeType": "patch",
"date": "2020-08-03T08:23:54.017Z"
}
Original file line number Diff line number Diff line change
Expand Up @@ -1640,7 +1640,7 @@ export interface IBasePickerProps<T> extends React.Props<any> {
itemLimit?: number;
onBlur?: React.FocusEventHandler<HTMLInputElement | Autofill>;
onChange?: (items?: T[]) => void;
onDismiss?: (ev?: any, selectedItem?: T) => void;
onDismiss?: (ev?: any, selectedItem?: T) => boolean | void;
17jlee2 marked this conversation as resolved.
Show resolved Hide resolved
// @deprecated
onEmptyInputFocus?: (selectedItems?: T[]) => T[] | PromiseLike<T[]>;
onEmptyResolveSuggestions?: (selectedItems?: T[]) => T[] | PromiseLike<T[]>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,22 @@ export class BasePicker<T, P extends IBasePickerProps<T>> extends React.Componen

public dismissSuggestions = (ev?: any): void => {
const selectItemFunction = () => {
let addItemOnDismimss: boolean | void = true;
if (this.props.onDismiss) {
this.props.onDismiss(
addItemOnDismimss = this.props.onDismiss(
xugao marked this conversation as resolved.
Show resolved Hide resolved
ev,
this.suggestionStore.currentSuggestion ? this.suggestionStore.currentSuggestion.item : undefined,
);
}

if (!ev || (ev && !ev.defaultPrevented)) {
// Select the first suggestion if one is available when user leaves.
if (this.canAddItems() && this.suggestionStore.hasSelectedSuggestion() && this.state.suggestedDisplayValue) {
// Select the first suggestion if one is available and permitted by onDismiss when user leaves.
if (
addItemOnDismimss !== false &&
17jlee2 marked this conversation as resolved.
Show resolved Hide resolved
this.canAddItems() &&
this.suggestionStore.hasSelectedSuggestion() &&
this.state.suggestedDisplayValue
) {
this.addItemByIndex(0);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,11 @@ export interface IBasePickerProps<T> extends React.Props<any> {
onInputChange?: (input: string) => string;

/**
* A callback to override the default behavior of adding the selected suggestion on dismiss.
* A callback to override the default behavior of adding the selected suggestion on dismiss. If it returns true or
17jlee2 marked this conversation as resolved.
Show resolved Hide resolved
* nothing, the selected item will be added on dismiss. If false, the selected item will not be added on dismiss.
*
*/
onDismiss?: (ev?: any, selectedItem?: T) => void;
onDismiss?: (ev?: any, selectedItem?: T) => boolean | void;

/**
* Adds an additional alert for the currently selected suggestion. This prop should be set to true for IE11 and below,
Expand Down