Skip to content

Commit

Permalink
fix: allow matches on words far in the label
Browse files Browse the repository at this point in the history
The current settings passed to fuse.js, make it unable to match something appearing very far in the label. In other words, if the label is made of 10 words and the query of the user aim to select on the 10th word, you'll never get the item.

With this PR I suggest to relax this constraint by asking fuse.js to ignore the location when performing its searches.

It should fixes the issue #337.
  • Loading branch information
dubzzz authored and timc1 committed Jan 3, 2024
1 parent 07f04bc commit 4e1354d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/__tests__/useMatches.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,25 @@ function WithPriorityComponent() {
);
}

function WithLongNamesComponent() {
const action1 = createAction({
name: "Action: This is a long name ending by toto",
});
const action2 = createAction({
name: "Action: This is a long name also ending by toto",
});
const action3 = createAction({
name: "Action: This is a long name ending by titi",
});

return (
<KBarProvider actions={[action1, action2, action3]}>
<Search />
<Results />
</KBarProvider>
);
}

const setup = (Component: React.ComponentType) => {
const utils = render(<Component />);
const input = utils.getByLabelText("search-input");
Expand Down Expand Up @@ -143,4 +162,23 @@ describe("useMatches", () => {
expect(utils.queryAllByText(/Section 1/i));
});
});
describe("With long names", () => {
let utils: Utils;
beforeEach(() => {
utils = setup(WithLongNamesComponent);
});

it("returns result matching the query even if match is on a word far in the name", () => {
const { input } = utils;
fireEvent.change(input, { target: { value: "toto" } });
const results = utils.getAllByText(/Action/i);
expect(results.length).toEqual(2);
expect(results[0].textContent).toEqual(
"Action: This is a long name ending by toto"
);
expect(results[1].textContent).toEqual(
"Action: This is a long name also ending by toto"
);
});
});
});
1 change: 1 addition & 0 deletions src/useMatches.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const fuseOptions: Fuse.IFuseOptions<ActionImpl> = {
},
"subtitle",
],
ignoreLocation: true,
includeScore: true,
includeMatches: true,
threshold: 0.2,
Expand Down

1 comment on commit 4e1354d

@vercel
Copy link

@vercel vercel bot commented on 4e1354d Jan 3, 2024

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

kbar – ./

kbar.vercel.app
kbar-timc.vercel.app
kbar-git-main-timc.vercel.app

Please sign in to comment.