Skip to content
Open
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
3 changes: 3 additions & 0 deletions packages/pivotkit-react/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ All notable changes to `pivotkit-react` are documented in this file. The format
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed

- The menu search says when it shows only the first 8 results.

## [0.10.0] - 2026-11-24

Expand Down
28 changes: 19 additions & 9 deletions packages/pivotkit-react/src/levels.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { ClearInput } from "./clear-input.jsx";
import { HelpContext } from "./help.js";
import { Group } from "./popover.jsx";
import { SettingInfo } from "./section.jsx";
import { rowName, searchEntries, searchHits, stepIndex, typeAheadIndex } from "./menu-topics.js";
import { rowName, searchEntries, searchHits, SEARCH_HITS, stepIndex, typeAheadIndex } from "./menu-topics.js";

/**
* A scrolling part of a menu (the first level's list, a topic's level), marked while more of it lies
Expand Down Expand Up @@ -458,7 +458,7 @@ function TopicFirst({ topic, labels, onPick, onOpen }) {
* The search of a menu: every choice of every level, by its text and its path. Enter runs only a hit
* the arrow keys moved to; nothing found says so.
*/
function SearchHits({ hits, labels, onPick, onOpen, close }) {
function SearchHits({ hits, labels, onPick, onOpen, close, hasMore }) {
if (!hits.length) {
return (
<p className="pk-hint pk-level-nothing" role="status" data-testid="menu-search-nothing">
Expand All @@ -467,11 +467,18 @@ function SearchHits({ hits, labels, onPick, onOpen, close }) {
);
}
return (
<MenuList label={labels.searchResults} close={close} testId="menu-search-hits" lang={labels.lang}>
{hits.map((h) => (
<MenuItem key={h.id} id={`hit-${h.id}`} text={h.choice ? h.text : `${h.text}…`} plain={h.text} checked={h.choice?.checked} icon={h.choice ? h.topic.icon : "chevronRight"} hint={h.path || undefined} haspopup={h.choice ? undefined : "dialog"} onSelect={() => (h.choice ? onPick(h.choice) : onOpen(h.topic.id, undefined, h.focus))} />
))}
</MenuList>
<>
<MenuList label={labels.searchResults} close={close} testId="menu-search-hits" lang={labels.lang}>
{hits.map((h) => (
<MenuItem key={h.id} id={`hit-${h.id}`} text={h.choice ? h.text : `${h.text}…`} plain={h.text} checked={h.choice?.checked} icon={h.choice ? h.topic.icon : "chevronRight"} hint={h.path || undefined} haspopup={h.choice ? undefined : "dialog"} onSelect={() => (h.choice ? onPick(h.choice) : onOpen(h.topic.id, undefined, h.focus))} />
))}
</MenuList>
{hasMore ? (
<p className="pk-hint" role="status" data-testid="menu-search-more">
{fill(labels.searchMore, { count: SEARCH_HITS })}
</p>
) : null}
</>
);
}

Expand Down Expand Up @@ -574,7 +581,10 @@ export function LevelMenu({ name, labels, topics, close, onPick, panel, head, fo
const first = () => {
const typed = search && query.trim() ? query : "";
const shown = typed && filter ? filter(typed) : topics;
const hits = typed && !filter ? searchHits(searchEntries(topics, query), query, labels.lang) : null;
const entries = typed && !filter ? searchEntries(topics, query) : null;
const result = entries ? searchHits(entries, query, labels.lang) : null;
const hits = result?.hits ?? null;
const hasMore = result?.hasMore ?? false;
const none = typed && filter && !shown.some((t) => !t.hidden && (t.groups ?? []).some((g) => g.choices.length));
return (
<>
Expand Down Expand Up @@ -608,7 +618,7 @@ export function LevelMenu({ name, labels, topics, close, onPick, panel, head, fo
) : null}
<Scroller className="pk-level-first">
{hits || none ? (
<SearchHits hits={hits ?? []} labels={labels} onPick={pick} onOpen={open} close={close} />
<SearchHits hits={hits ?? []} labels={labels} onPick={pick} onOpen={open} close={close} hasMore={hasMore} />
) : (
<MenuList label={name} close={close} testId="level-first" lang={labels.lang}>
{shown.map((t) => (
Expand Down
1 change: 1 addition & 0 deletions packages/pivotkit-react/src/menu-labels-de.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const de = {
searchMenuShort: "In diesem Menü suchen (Taste /)",
searchResults: "Treffer",
searchNothing: "Nichts gefunden. Ein anderes Wort versuchen oder die Suche leeren.",
searchMore: "Nur die ersten {count} Treffer. Mehr Wörter grenzen die Suche ein.",
addAlready: "Bereits in {zone}.",
addMoves: "Verschiebt es aus {zone}.",
addSecond: "Fügt einen zweiten Wert davon hinzu.",
Expand Down
1 change: 1 addition & 0 deletions packages/pivotkit-react/src/menu-labels.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export const en = {
searchMenuShort: "Search in this menu (key /)",
searchResults: "Results",
searchNothing: "Nothing found. Try another word, or clear the search.",
searchMore: "Only the first {count} results are shown. Type more to narrow the search.",
// the menu of "+ Add" in a zone: a field there already, one that moves, a second value
addAlready: "Already in {zone}.",
addMoves: "Moves it from {zone}.",
Expand Down
16 changes: 13 additions & 3 deletions packages/pivotkit-react/src/menu-topics.js
Original file line number Diff line number Diff line change
Expand Up @@ -847,25 +847,35 @@ export const SEARCH_HITS = 8;
* @param {SearchEntry[]} entries
* @param {string} query
* @param {string} [locale]
* @returns {SearchEntry[]}
* @returns {{ hits: SearchEntry[], hasMore: boolean }}
*/
export function searchHits(entries, query, locale) {
const lower = (s) => s.toLocaleLowerCase(locale);
const words = lower(query).split(/\s+/).filter(Boolean);

if (!words.length) {
return [];
return { hits: [], hasMore: false };
}

const found = entries.filter((e) => {
const text = lower(`${e.text} ${e.path} ${e.topic.keywords ?? ""} ${e.choice?.keywords ?? e.keywords ?? ""}`);
return words.every((w) => text.includes(w));
});

// a stable sort: the entries with the first word among their own keywords ("percent": the shares
// of Show as), then those whose text starts with it, in their order, then the rest
const own = (/** @type {SearchEntry} */ e) => lower(e.choice?.keywords ?? "").split(/\s+/).includes(words[0]);
const rank = (/** @type {SearchEntry} */ e) => (own(e) ? 0 : lower(e.text).startsWith(words[0]) ? 1 : 2);
return [0, 1, 2].flatMap((r) => found.filter((e) => rank(e) === r)).slice(0, SEARCH_HITS);

const all = [0, 1, 2].flatMap((r) => found.filter((e) => rank(e) === r));

return {
hits: all.slice(0, SEARCH_HITS),
hasMore: all.length > SEARCH_HITS,
};
}


/**
* A row's accessible name: "Filter, 3 of 12, changed" (`changedWord`: ", changed").
* @param {string} title
Expand Down
18 changes: 9 additions & 9 deletions packages/pivotkit-react/test/filter-rules.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ test("the data's share of a range names no day from today on: they have no data

test("search: quick choices with the count typed, a year compared, and the rules by name; they apply at once", () => {
const topics = axisTopics({ view: byYear, zone: "rows", index: 0, field: YEAR, types, labels: MENU.en, locale: "en-US", encoded: data });
const hits = (q) => searchHits(searchEntries(topics, q), q, "en-US");
const hits = (q) => searchHits(searchEntries(topics, q), q, "en-US").hits;
const years = relativeDateRange("year", 2);
const last2 = hits("last 2 years")[0];
// the words of the chip, with a capital (the data's share of the range included)
Expand All @@ -285,12 +285,12 @@ test("search: quick choices with the count typed, a year compared, and the rules
assert.deepEqual(filtersOf(last2.choice.change().view, YEAR, RULE_TYPES), [{ field: YEAR, type: "relative", unit: "year", count: 2, not: false, blanks: "exclude" }]);
// a range counted back from the latest record (2025): the search's choices count from it too
const anchored = axisTopics({ view: setQuickRange(byYear, YEAR, { unit: "year", count: 1, anchor: "latest" }), zone: "rows", index: 0, field: YEAR, types, labels: MENU.en, locale: "en-US", encoded: data });
const hit = searchHits(searchEntries(anchored, "last 3 years"), "last 3 years", "en-US")[0];
const hit = searchHits(searchEntries(anchored, "last 3 years"), "last 3 years", "en-US").hits[0];
assert.equal(hit.text, chip({ unit: "year", count: 3, anchor: "latest" }).replace(/^l/, "L"));
assert.deepEqual(filtersOf(hit.choice.change().view, YEAR, RULE_TYPES), [{ field: YEAR, type: "relative", unit: "year", count: 3, anchor: "latest", not: false, blanks: "exclude" }]);
// a cyclic grouping has no quick choices to find
const cyclic = axisTopics({ view: byCyclic, zone: "rows", index: 0, field: CYCLIC, types, labels: MENU.en, locale: "en-US", encoded: data });
assert.equal(searchHits(searchEntries(cyclic, "last 2 years"), "last 2 years", "en-US").length, 0);
assert.equal(searchHits(searchEntries(cyclic, "last 2 years"), "last 2 years", "en-US").hits.length, 0);
// a year grouping: no months
assert.equal(hits("last 2 months").length, 0);
const from = hits("from 2022")[0];
Expand All @@ -302,18 +302,18 @@ test("search: quick choices with the count typed, a year compared, and the rules
assert.ok(hits("rule").some((e) => e.id === "open-filter-value"));
// a date field: months too, and a year as its first or last day
const raw = axisTopics({ view: norm({ rows: ["Released"] }), zone: "rows", index: 0, field: "Released", types, labels: MENU.en, locale: "en-US", encoded: data });
const rawHits = (q) => searchHits(searchEntries(raw, q), q, "en-US");
const rawHits = (q) => searchHits(searchEntries(raw, q), q, "en-US").hits;
assert.equal(rawHits("last 6 months").length, 1);
// a date field compares days: no year entry, whose chip would say a day ("from Jan 1, 2022")
assert.equal(rawHits("up to 2022").length, 0);
assert.equal(rawHits("from 2022").length, 0);
// German words
const de = axisTopics({ view: byYear, zone: "rows", index: 0, field: YEAR, types, labels: MENU.de, locale: "de-DE", encoded: data });
assert.equal(searchHits(searchEntries(de, "ab 2022"), "ab 2022", "de-DE")[0].text, "Ab 2022");
assert.ok(searchHits(searchEntries(de, "letzte 3 jahre"), "letzte 3 jahre", "de-DE")[0].text.startsWith("Letzte 3 Jahre ("));
assert.equal(searchHits(searchEntries(de, "ab 2022"), "ab 2022", "de-DE").hits[0].text, "Ab 2022");
assert.ok(searchHits(searchEntries(de, "letzte 3 jahre"), "letzte 3 jahre", "de-DE").hits[0].text.startsWith("Letzte 3 Jahre ("));
// a text field gets none of them; nothing typed, nothing made
const text = axisTopics({ view: norm({ rows: ["Region"] }), zone: "rows", index: 0, field: "Region", types, labels: MENU.en, locale: "en-US", encoded: data });
assert.equal(searchHits(searchEntries(text, "last 2 years"), "last 2 years", "en-US").length, 0);
assert.equal(searchHits(searchEntries(text, "last 2 years"), "last 2 years", "en-US").hits.length, 0);
assert.equal(searchEntries(topics).some((e) => e.id.startsWith("quick-")), false);
});

Expand Down Expand Up @@ -550,7 +550,7 @@ test("search: a quick choice found takes the place of the one set, keeps a range
const view = norm(setRule(setQuickRange(byYear, YEAR, { unit: "year", count: 5, blanks: "include" }, null), YEAR, null, rule));
assert.equal(quickChoice(view, YEAR, fk).count, 5);
const topics = axisTopics({ view, zone: "rows", index: 0, field: YEAR, types, labels: MENU.en, locale: "en-US", encoded: data });
const hit = searchHits(searchEntries(topics, "last 3 years"), "last 3 years", "en-US")[0];
const hit = searchHits(searchEntries(topics, "last 3 years"), "last 3 years", "en-US").hits[0];
const ranges = filtersOf(hit.choice.change().view, YEAR, ["relative"]);
assert.deepEqual(ranges.map((f) => [f.count, f.not, f.blanks]), [[3, false, "include"], [1, true, "include"]]);
});
Expand Down Expand Up @@ -594,7 +594,7 @@ test("Counted back from: every relative range of the field takes the reference,
test("search: the rule on the name is found by its operators, \"does not begin with\" among them (review)", () => {
for (const [L, locale, q] of [[MENU.en, "en-US", "does not begin"], [MENU.en, "en-US", "not end with"], [MENU.de, "de-DE", "beginnt nicht"]]) {
const topics = axisTopics({ view: norm({ rows: ["Region"] }), zone: "rows", index: 0, field: "Region", types, labels: L, locale, encoded: data });
assert.deepEqual(searchHits(searchEntries(topics, q), q, locale).map((e) => e.id), ["open-filter-text"], q);
assert.deepEqual(searchHits(searchEntries(topics, q), q, locale).hits.map((e) => e.id), ["open-filter-text"], q);
}
});

Expand Down
15 changes: 9 additions & 6 deletions packages/pivotkit-react/test/menu-topics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,21 +200,24 @@ test("the words of folding the groups of Rows: the fields' names, the plain word
test("search: every choice of every level with its path, and the panels; all words must match; the start of a text first", () => {
const view = norm({ rows: ["Region"], values: [units] });
const entries = searchEntries(valueTopics({ view, index: 0, types, labels: MENU.en, locale: "en-US" }));
const hit = (q) => searchHits(entries, q, "en-US").map((e) => `${e.text} | ${e.path}`);
const hit = (q) => searchHits(entries, q, "en-US").hits.map((e) => `${e.text} | ${e.path}`);
assert.deepEqual(hit("currency"), ["Currency | Number format"]);
assert.deepEqual(hit("compact"), ["Compact (1.2K) | Number format › More formats"]);
// the path counts: "format" finds the formats, the words in any order
assert.ok(hit("format whole").includes("Number, no decimals | Number format"));
assert.deepEqual(hit("name"), ["Name | "]);
assert.deepEqual(hit(" "), []);
assert.deepEqual(hit("nothing like this"), []);
assert.ok(searchHits(entries, "o", "en-US").length <= SEARCH_HITS);
assert.ok(searchHits(entries, "o", "en-US").hits.length <= SEARCH_HITS);
assert.equal(searchHits(entries, "o", "en-US").hasMore, true);
assert.equal(searchHits(entries, "currency", "en-US").hasMore, false);
assert.equal(searchHits(entries, " ", "en-US").hasMore, false);
// a field's: the Filter panel is an entry that opens it
const field = searchEntries(axis(view));
// the Filter panel's own entries (its rules) are found by its title too
// ("Filter by name" and "Filter by total" start with the words, the rules have them in their path)
assert.deepEqual(searchHits(field, "filt", "en-US").map((e) => [e.id, Boolean(e.choice)]), [["open-filter", false], ["open-filter-text", false], ["open-filter-value", false], ["open-filter-rules", false], ["move-filters", true]]);
assert.deepEqual(searchHits(field, "move col", "en-US").map((e) => e.id), ["move-cols"]);
assert.deepEqual(searchHits(field, "filt", "en-US").hits.map((e) => [e.id, Boolean(e.choice)]), [["open-filter", false], ["open-filter-text", false], ["open-filter-value", false], ["open-filter-rules", false], ["move-filters", true]]);
assert.deepEqual(searchHits(field, "move col", "en-US").hits.map((e) => e.id), ["move-cols"]);
});

test("the keys of a menu: arrows around the ends, Home and End; type-ahead moves on with one letter, stays with more", () => {
Expand Down Expand Up @@ -259,13 +262,13 @@ test("the Views menu's entries: ids by group, name and repeat; after a delete th
test("search: the shares of Show as come first for percent and %; the conditions and Top 5 are entries; rename finds Name", () => {
const view = norm({ rows: ["Region"], values: [units] });
const value = searchEntries(valueTopics({ view, index: 0, types, labels: MENU.en, locale: "en-US" }));
const first = (q) => searchHits(value, q, "en-US").map((e) => e.text);
const first = (q) => searchHits(value, q, "en-US").hits.map((e) => e.text);
assert.equal(first("percent")[0], "% of grand total");
assert.ok(first("percent").includes("Percent"), "the format of that name is still found");
assert.equal(first("%")[0], "% of grand total");
assert.deepEqual(first("rename"), ["Name"]);
const field = searchEntries(axis(view));
const hits = (q) => searchHits(field, q, "en-US");
const hits = (q) => searchHits(field, q, "en-US").hits;
assert.deepEqual(hits("filter by name").map((e) => [e.id, e.focus]), [["open-filter-text", "conditions"]]);
assert.ok(hits("filter by total").some((e) => e.id === "open-filter-value" && e.focus === "conditions"));
// "rule" finds the rules, and opens the level on them
Expand Down