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

[select] fix: do not call itemRenderer when Select is disabled #3629

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
37 changes: 24 additions & 13 deletions packages/select/src/components/query-list/queryList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ export interface IQueryListProps<T> extends IListItemsProps<T> {
* Receives an object with props that should be applied to elements as necessary.
*/
renderer: (listProps: IQueryListRendererProps<T>) => JSX.Element;

/**
* Whether the list is disabled.
* @default false
*/
disabled?: boolean;
}

/**
Expand Down Expand Up @@ -127,6 +133,7 @@ export class QueryList<T> extends React.Component<IQueryListProps<T>, IQueryList
public static displayName = `${DISPLAYNAME_PREFIX}.QueryList`;

public static defaultProps = {
disabled: false,
resetOnQuery: true,
};

Expand Down Expand Up @@ -304,19 +311,23 @@ export class QueryList<T> extends React.Component<IQueryListProps<T>, IQueryList

/** wrapper around `itemRenderer` to inject props */
private renderItem = (item: T, index: number) => {
const { activeItem, query } = this.state;
const matchesPredicate = this.state.filteredItems.indexOf(item) >= 0;
const modifiers: IItemModifiers = {
active: executeItemsEqual(this.props.itemsEqual, getActiveItem(activeItem), item),
disabled: isItemDisabled(item, index, this.props.itemDisabled),
matchesPredicate,
};
return this.props.itemRenderer(item, {
handleClick: e => this.handleItemSelect(item, e),
index,
modifiers,
query,
});
if (this.props.disabled !== true) {
const { activeItem, query } = this.state;
const matchesPredicate = this.state.filteredItems.indexOf(item) >= 0;
const modifiers: IItemModifiers = {
active: executeItemsEqual(this.props.itemsEqual, getActiveItem(activeItem), item),
disabled: isItemDisabled(item, index, this.props.itemDisabled),
matchesPredicate,
};
return this.props.itemRenderer(item, {
handleClick: e => this.handleItemSelect(item, e),
index,
modifiers,
query,
});
}

return null;
};

private renderCreateItemMenuItem = (query: string) => {
Expand Down
1 change: 1 addition & 0 deletions packages/select/src/components/select/select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export interface ISelectProps<T> extends IListItemsProps<T> {

/**
* Whether the component is non-interactive.
* If true, the list's item renderer will not be called.
* Note that you'll also need to disable the component's children, if appropriate.
* @default false
*/
Expand Down
10 changes: 10 additions & 0 deletions packages/select/test/selectTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ describe("<Select>", () => {
assert.strictEqual(wrapper.find(Popover).prop("disabled"), true);
});

it("disabled=true doesn't call itemRenderer", () => {
select({ disabled: true });
assert.equal(handlers.itemRenderer.callCount, 0);
});

it("disabled=false calls itemRenderer", () => {
select({ disabled: false });
assert.equal(handlers.itemRenderer.callCount, 100);
});

it("inputProps value and onChange are ignored", () => {
const inputProps = { value: "nailed it", onChange: sinon.spy() };
const input = select({ inputProps }).find("input");
Expand Down