Skip to content

Commit

Permalink
[select] fix(QueryList): trim whitespace when creating new items (#3832)
Browse files Browse the repository at this point in the history
  • Loading branch information
adidahiya authored Apr 9, 2020
1 parent ff17d0f commit db66df8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
16 changes: 11 additions & 5 deletions packages/select/src/components/query-list/queryList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export class QueryList<T> extends AbstractComponent2<IQueryListProps<T>, IQueryL
handleKeyDown: this.handleKeyDown,
handleKeyUp: this.handleKeyUp,
handlePaste: this.handlePaste,
handleQueryChange: this.handleQueryChange,
handleQueryChange: this.handleInputQueryChange,
itemList: itemListRenderer({
...spreadableState,
items,
Expand Down Expand Up @@ -278,9 +278,13 @@ export class QueryList<T> extends AbstractComponent2<IQueryListProps<T>, IQueryL
Utils.safeInvoke(props.onQueryChange, query);
}

const filteredItems = getFilteredItems(query, props);
// Leading and trailing whitespace can be confusing to display, so we remove it when passing it
// to functions dealing with data, like createNewItemFromQuery. But we need the unaltered user-typed
// query to remain in state to be able to render controlled text inputs properly.
const trimmedQuery = query.trim();
const filteredItems = getFilteredItems(trimmedQuery, props);
const createNewItem =
createNewItemFromQuery != null && query !== "" ? createNewItemFromQuery(query) : undefined;
createNewItemFromQuery != null && trimmedQuery !== "" ? createNewItemFromQuery(trimmedQuery) : undefined;
this.setState({ createNewItem, filteredItems, query });

// always reset active item if it's now filtered or disabled
Expand Down Expand Up @@ -317,7 +321,9 @@ export class QueryList<T> extends AbstractComponent2<IQueryListProps<T>, IQueryL
// omit noResults if createNewItemFromQuery and createNewItemRenderer are both supplied, and query is not empty
const maybeNoResults = this.isCreateItemRendered() ? null : noResults;
const menuContent = renderFilteredItems(listProps, maybeNoResults, initialContent);
const createItemView = this.isCreateItemRendered() ? this.renderCreateItemMenuItem(this.state.query) : null;
const createItemView = this.isCreateItemRendered()
? this.renderCreateItemMenuItem(this.state.query.trim())
: null;
if (menuContent == null && createItemView == null) {
return null;
}
Expand Down Expand Up @@ -483,7 +489,7 @@ export class QueryList<T> extends AbstractComponent2<IQueryListProps<T>, IQueryL
Utils.safeInvoke(onKeyUp, event);
};

private handleQueryChange = (event?: React.ChangeEvent<HTMLInputElement>) => {
private handleInputQueryChange = (event?: React.ChangeEvent<HTMLInputElement>) => {
const query = event == null ? "" : event.target.value;
this.setQuery(query);
Utils.safeInvoke(this.props.onQueryChange, query, event);
Expand Down
29 changes: 29 additions & 0 deletions packages/select/test/queryListTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -315,4 +315,33 @@ describe("<QueryList>", () => {
assert.deepEqual(filmQueryList.state().query, "");
});
});

describe("query", () => {
it("trims leading and trailing whitespace when creating new items", () => {
let triggerInputQueryChange: ((e: any) => void) | undefined;
const createNewItemFromQuerySpy = sinon.spy();
const createNewItemRendererSpy = sinon.spy();
// we must supply our own renderer so that we can hook into IQueryListRendererProps#handleQueryChange
const renderer = sinon.spy((props: IQueryListRendererProps<IFilm>) => {
triggerInputQueryChange = props.handleQueryChange;
return <div>{props.itemList}</div>;
});
shallow(
<FilmQueryList
{...testProps}
renderer={renderer}
createNewItemFromQuery={createNewItemFromQuerySpy}
createNewItemRenderer={createNewItemRendererSpy}
/>,
);

const untrimmedQuery = " foo ";
const trimmedQuery = untrimmedQuery.trim();

assert.isDefined(triggerInputQueryChange, "query list should render with input change callbacks");
triggerInputQueryChange!({ target: { value: untrimmedQuery } });
assert.isTrue(createNewItemFromQuerySpy.calledWith(trimmedQuery));
assert.isTrue(createNewItemRendererSpy.calledWith(trimmedQuery));
});
});
});

1 comment on commit db66df8

@blueprint-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[select] fix(QueryList): trim whitespace when creating new items (#3832)

Previews: documentation | landing | table

Please sign in to comment.