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

[core,popover2,select] fix: make some ref handlers more robust #4877

Merged
merged 1 commit into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion packages/core/src/components/tag-input/tagInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export interface ITagInputProps extends IntentProps, Props {
inputProps?: HTMLInputProps;

/** Ref handler for the `<input>` element. */
inputRef?: (input: HTMLInputElement | null) => void;
inputRef?: IRef<HTMLInputElement>;

/** Controlled value of the `<input>` element. This is shorthand for `inputProps={{ value }}`. */
inputValue?: string;
Expand Down
3 changes: 2 additions & 1 deletion packages/popover2/src/popover2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
mergeRefs,
Overlay,
Utils,
IRef,
} from "@blueprintjs/core";

import * as Classes from "./classes";
Expand Down Expand Up @@ -88,7 +89,7 @@ export interface IPopover2Props<TProps = React.HTMLProps<HTMLElement>> extends P
/**
* Ref supplied to the `Classes.POPOVER` element.
*/
popoverRef?: (ref: HTMLElement | null) => void;
popoverRef?: IRef<HTMLElement>;

/**
* Popper.js positioning strategy.
Expand Down
24 changes: 17 additions & 7 deletions packages/select/src/components/select/multiSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import {
Position,
TagInput,
TagInputAddMethod,
refHandler,
setRef,
} from "@blueprintjs/core";

import { Classes, IListItemsProps } from "../../common";
Expand Down Expand Up @@ -113,18 +115,26 @@ export class MultiSelect<T> extends AbstractPureComponent2<MultiSelectProps<T>,

private TypedQueryList = QueryList.ofType<T>();

private input: HTMLInputElement | null = null;
public input: HTMLInputElement | null = null;

private queryList: QueryList<T> | null = null;
public queryList: QueryList<T> | null = null;

private refHandlers = {
input: (ref: HTMLInputElement | null) => {
this.input = ref;
this.props.tagInputProps?.inputRef?.(ref);
},
private refHandlers: {
input: React.RefCallback<HTMLInputElement>;
queryList: React.RefCallback<QueryList<T>>;
} = {
input: refHandler(this, "input", this.props.tagInputProps?.inputRef),
queryList: (ref: QueryList<T> | null) => (this.queryList = ref),
};

public componentDidUpdate(prevProps: MultiSelectProps<T>) {
if (prevProps.tagInputProps?.inputRef !== this.props.tagInputProps?.inputRef) {
setRef(prevProps.tagInputProps?.inputRef, null);
this.refHandlers.input = refHandler(this, "input", this.props.tagInputProps?.inputRef);
setRef(this.props.tagInputProps?.inputRef, this.input);
}
}

public render() {
// omit props specific to this component, spread the rest.
const { openOnKeyDown, popoverProps, tagInputProps, ...restProps } = this.props;
Expand Down