pacocoursey / cmdk

Fast, unstyled command menu React component.
https://cmdk.paco.me
MIT License
9.68k stars 279 forks source link

feat: add keywords prop to the item component #158

Closed itaikeren closed 8 months ago

itaikeren commented 1 year ago

This PR adds the keywords prop to the Item component.

Closes #146

vercel[bot] commented 1 year ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
cmdk-website ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 18, 2023 9:19am
TheMikeyRoss commented 8 months ago

half a year later

lucsmachado commented 8 months ago

While this is pending review, here's a workaround I'm using:

<Command
  filter={(value, search) => {
    const extendValue = `${value} ${getKeywords(value).join(' ')}`;
    if (extendValue.includes(search)) return 1
    return 0
  }}
/>

Then you can define getKeywords(value: string): string[] as desired. Example for my usecase:

const getKeywords = (value: string) => {
  const keywords: string[] = [];
  const foundItem = items.find(
    // `toLowerCase()` is needed because `value` is normalized as lowercase (per the documentation)
    (item) => item.name.toLowerCase() === value,
  );
  if (foundItem) {
    keywords.push(foundItem.cpf);
  }
  return keywords;
};
petrkrejcik commented 7 months ago

@pacocoursey FYI this feature isn't released. The release v0.2.1 was made right before merging this PR.