From c3c5b8ccd249d14aa88923d46a0182106ab35b3f Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 16 Nov 2026 10:28:25 +0000 Subject: [PATCH] fix(fields): bound the lookup picker to its room, mark a cut row, wrap the preview The option list takes the room Radix reports on the side the popover opens to, fades the edge it continues past, and lets an option's preview wrap onto a second row so two text columns are not both cut to one row. Claude-Session: https://claude.ai/code/session_01ADHkmSg4bMFcuXXpNikRnL Co-authored-by: Claude --- .changeset/34362-lookup-picker-room.md | 14 ++ .../LookupField.previewBadges-13123.test.tsx | 179 ++++++++++++++++-- .../fields/src/widgets/LookupField.tsx | 140 ++++++++++++-- 3 files changed, 296 insertions(+), 37 deletions(-) create mode 100644 .changeset/34362-lookup-picker-room.md diff --git a/.changeset/34362-lookup-picker-room.md b/.changeset/34362-lookup-picker-room.md new file mode 100644 index 00000000000..efecc256c3f --- /dev/null +++ b/.changeset/34362-lookup-picker-room.md @@ -0,0 +1,14 @@ +--- +'@object-ui/fields': patch +--- + +fix(fields): the lookup picker's list fits the room it is given, marks a cut row as "scroll for more", and lets an option's secondary fields wrap + +Clause-②: no +Metadata-protocol: none + +The lookup quick-picker's option list had a fixed 16rem height. With a one-line preview under each option, that height ends part-way through the fifth row, so the last visible option showed its name and lost its preview, with nothing saying the list scrolls. On a short window the popover was also taller than the room on either side of the field and ran off the screen. Radix already flips the popover to the side with more room, inside a dialog too; the list simply ignored how much room that side had. + +- The popover is now no taller than the room Radix reports on the side it opens to. The search row keeps its height and the list takes what is left, up to the same 16rem. +- When options continue past the top or the bottom edge of the list, that edge fades out, so a row cut there reads as "scroll for more". The keyboard scrolls the active option clear of the fade. +- An option's preview wraps onto a second row when its columns do not fit side by side, so two text columns (an email and an account name, say) are no longer both cut to fit one row. A column that does not fit on two rows leaves whole, as before; a badge is never cut and a text value is never cut below its label. A text value that is still cut short shows its whole text on hover. diff --git a/ui/packages/fields/src/widgets/LookupField.previewBadges-13123.test.tsx b/ui/packages/fields/src/widgets/LookupField.previewBadges-13123.test.tsx index 6385135c35b..996b36b4558 100644 --- a/ui/packages/fields/src/widgets/LookupField.previewBadges-13123.test.tsx +++ b/ui/packages/fields/src/widgets/LookupField.previewBadges-13123.test.tsx @@ -173,26 +173,52 @@ describe('LookupField option preview: a badge is never cut (objectui#13123)', () }); /** - * The line's layout, modelled: each column has a width, the segments sit 4px - * apart, and a hidden segment takes no room. `lineWidth` is the line's box. + * The preview's layout, modelled: each column has a width, the segments sit + * 4px apart and wrap onto the next row (20px down) when they do not fit beside + * the one before, a hidden segment takes no room, and a badge wider than the + * line overflows it where a text column would shrink to it. `lineWidth` is the + * line's box. */ const COLUMN_WIDTH: Record = { status: 114, industry: 148, annual_revenue: 130 }; +const BADGES = new Set(['status', 'industry']); let lineWidth = 0; +const isLine = (el: Element | null): el is HTMLElement => (el as HTMLElement | null)?.dataset?.testid === 'lookup-option-preview'; +const fieldOf = (segment: Element) => + segment.querySelector('[data-lookup-preview]')?.getAttribute('data-lookup-preview') ?? ''; + +/** Each shown segment's row offset, and the widest badge the line must hold. */ +function layoutOf(line: HTMLElement): { tops: Map; widest: number } { + const tops = new Map(); + let x = 0; + let row = 0; + let widest = 0; + for (const segment of Array.from(line.children)) { + if ((segment as HTMLElement).hidden) continue; + const field = fieldOf(segment); + const natural = COLUMN_WIDTH[field] ?? 0; + const width = BADGES.has(field) ? natural : Math.min(natural, lineWidth); + if (BADGES.has(field)) widest = Math.max(widest, natural); + if (x > 0 && x + 4 + width > lineWidth) { + row += 1; + x = 0; + } + x += (x > 0 ? 4 : 0) + width; + tops.set(segment, row * 20); + } + return { tops, widest }; +} + function stubLineLayout(): void { - const isLine = (el: HTMLElement) => el.dataset.testid === 'lookup-option-preview'; vi.spyOn(HTMLElement.prototype, 'clientWidth', 'get').mockImplementation(function (this: HTMLElement) { return isLine(this) ? lineWidth : 0; }); vi.spyOn(HTMLElement.prototype, 'scrollWidth', 'get').mockImplementation(function (this: HTMLElement) { - if (!isLine(this)) return 0; - const shown = Array.from(this.children).filter((s) => !(s as HTMLElement).hidden); - const content = shown.reduce((sum, s) => { - const field = s.querySelector('[data-lookup-preview]')?.getAttribute('data-lookup-preview') ?? ''; - return sum + (COLUMN_WIDTH[field] ?? 0); - }, 0); // The browser reports the box's own width when the content is narrower. - return Math.max(lineWidth, content + 4 * Math.max(0, shown.length - 1)); + return isLine(this) ? Math.max(lineWidth, layoutOf(this).widest) : 0; + }); + vi.spyOn(HTMLElement.prototype, 'offsetTop', 'get').mockImplementation(function (this: HTMLElement) { + return isLine(this.parentElement) ? layoutOf(this.parentElement).tops.get(this) ?? 0 : 0; }); } @@ -216,11 +242,12 @@ function shownColumns(): string[] { return ['status', 'industry', 'annual_revenue'].filter((f) => !segmentOf(f).hidden); } -describe('LookupField option preview: a column the line cannot hold leaves whole (objectui#13123)', () => { +describe('LookupField option preview: a column the rows cannot hold leaves whole (objectui#13123)', () => { it.each([ - // The card's width: both badges whole, the revenue column dropped. - [280, ['status', 'industry']], - [180, ['status']], + // The card's width: the revenue column wraps onto the second row whole. + [280, ['status', 'industry', 'annual_revenue']], + // One column a row: a third row is one too many, so revenue leaves. + [180, ['status', 'industry']], [600, ['status', 'industry', 'annual_revenue']], // Not even the first badge fits: the line drops it rather than cut it. [100, []], @@ -244,15 +271,133 @@ describe('LookupField option preview: a column the line cannot hold leaves whole lineWidth = 180; stubLineLayout(); await openPicker(['name', 'status', 'industry', 'annual_revenue']); - expect(shownColumns()).toEqual(['status']); + expect(shownColumns()).toEqual(['status', 'industry']); expect(lineObservers.length).toBeGreaterThan(0); lineWidth = 600; act(() => lineObservers.forEach((fire) => fire())); expect(shownColumns()).toEqual(['status', 'industry', 'annual_revenue']); - lineWidth = 280; + lineWidth = 100; act(() => lineObservers.forEach((fire) => fire())); - expect(shownColumns()).toEqual(['status', 'industry']); + expect(shownColumns()).toEqual([]); + }); +}); + +/** The `·` that leads every column after the first. */ +function separatorOf(field: string): HTMLElement { + const sep = segmentOf(field).querySelector('[data-lookup-preview-separator]'); + if (!sep) throw new Error(`no separator before ${field}`); + return sep; +} + +describe('LookupField option preview: secondary columns stay told apart (objectstack#34362)', () => { + it('lets the preview wrap, so a column that does not fit opens the next row instead of cutting both', async () => { + await openPicker(['name', 'status', 'billing_email']); + const line = screen.getByTestId('lookup-option-preview'); + expect(line.classList.contains('flex-wrap'), 'the preview no longer wraps').toBe(true); + }); + + it('drops the separator of a column that opens a row, and keeps it beside the column before', async () => { + lineWidth = 280; + stubLineLayout(); + await openPicker(['name', 'status', 'industry', 'annual_revenue']); + // Row one: status, industry. Row two: revenue. + expect(separatorOf('industry').hidden).toBe(false); + expect(separatorOf('annual_revenue').hidden).toBe(true); + }); + + it('keeps every separator on one row', async () => { + lineWidth = 600; + stubLineLayout(); + await openPicker(['name', 'status', 'industry', 'annual_revenue']); + expect(separatorOf('industry').hidden).toBe(false); + expect(separatorOf('annual_revenue').hidden).toBe(false); + }); + + it('gives a text value cut short its whole text as its title, and a whole value none', async () => { + const CUT = 'billing_email'; + vi.spyOn(HTMLElement.prototype, 'scrollWidth', 'get').mockImplementation(function (this: HTMLElement) { + return this.getAttribute('data-lookup-preview') === CUT ? 300 : 0; + }); + vi.spyOn(HTMLElement.prototype, 'clientWidth', 'get').mockImplementation(function (this: HTMLElement) { + return this.getAttribute('data-lookup-preview') === CUT ? 120 : 0; + }); + await openPicker(['name', 'status', ...TEXT_COLUMNS]); + + expect(valueOf(CUT).getAttribute('title')).toBe(ROW.billing_email); + expect(valueOf('annual_revenue').hasAttribute('title')).toBe(false); + // A badge keeps its own title; the fit never writes one onto its value. + expect(valueOf('status').hasAttribute('title')).toBe(false); + }); +}); + +describe('LookupField picker popover: bound to the room it is given (objectstack#34362)', () => { + /** The listbox's scroll box, stubbed: `content` px of options in a `box` px list. */ + function stubListScroll(box: number, content: number): void { + const isList = (el: HTMLElement) => el.getAttribute('role') === 'listbox'; + vi.spyOn(HTMLElement.prototype, 'clientHeight', 'get').mockImplementation(function (this: HTMLElement) { + return isList(this) ? box : 0; + }); + vi.spyOn(HTMLElement.prototype, 'scrollHeight', 'get').mockImplementation(function (this: HTMLElement) { + return isList(this) ? content : 0; + }); + } + + it('is portaled out of the field, so no dialog body can clip it and Radix flips it against the viewport', async () => { + await openPicker(['name', 'status']); + const popover = screen.getByTestId('lookup-popover'); + expect(screen.getByTestId('lookup-trigger').parentElement?.contains(popover)).toBe(false); + expect(popover.parentElement?.hasAttribute('data-radix-popper-content-wrapper')).toBe(true); + }); + + it('caps the popover at the available height Radix reports, and the list takes what the search row leaves', async () => { + await openPicker(['name', 'status']); + const popover = screen.getByTestId('lookup-popover'); + for (const cls of ['flex', 'flex-col', 'max-h-(--radix-popover-content-available-height)']) { + expect(popover.classList.contains(cls), `popover lacks ${cls}`).toBe(true); + } + const search = screen.getByRole('combobox').closest('.p-2') as HTMLElement; + expect(search.classList.contains('shrink-0'), 'the search row gives up its height').toBe(true); + const list = screen.getByRole('listbox'); + // `min-h-0` lets the list shrink below its 16rem cap; the cap still holds where there is room. + for (const cls of ['min-h-0', 'max-h-64', 'overflow-y-auto']) { + expect(list.classList.contains(cls), `list lacks ${cls}`).toBe(true); + } + expect((list.parentElement as HTMLElement).classList.contains('min-h-0'), 'the list wrapper does not shrink').toBe(true); + }); + + it('marks the list edge with more options past it, so a row cut there reads as scroll for more', async () => { + stubListScroll(256, 600); + await openPicker(['name', 'status']); + const list = screen.getByRole('listbox'); + await waitFor(() => expect(screen.queryByTestId('lookup-list-more-below')).not.toBeNull()); + expect(screen.queryByTestId('lookup-list-more-above')).toBeNull(); + + act(() => { + list.scrollTop = 344; + fireEvent.scroll(list); + }); + expect(screen.queryByTestId('lookup-list-more-below')).toBeNull(); + expect(screen.queryByTestId('lookup-list-more-above')).not.toBeNull(); + + act(() => { + list.scrollTop = 100; + fireEvent.scroll(list); + }); + expect(screen.queryByTestId('lookup-list-more-below')).not.toBeNull(); + expect(screen.queryByTestId('lookup-list-more-above')).not.toBeNull(); + }); + + it('draws no edge marks when every option fits', async () => { + stubListScroll(256, 120); + await openPicker(['name', 'status']); + expect(screen.queryByTestId('lookup-list-more-below')).toBeNull(); + expect(screen.queryByTestId('lookup-list-more-above')).toBeNull(); + }); + + it('leaves room for the edge mark when the keyboard scrolls an option into view', async () => { + await openPicker(['name', 'status']); + expect(screen.getByRole('listbox').classList.contains('scroll-py-6')).toBe(true); }); }); diff --git a/ui/packages/fields/src/widgets/LookupField.tsx b/ui/packages/fields/src/widgets/LookupField.tsx index a833611d9c8..010d418f431 100644 --- a/ui/packages/fields/src/widgets/LookupField.tsx +++ b/ui/packages/fields/src/widgets/LookupField.tsx @@ -251,27 +251,64 @@ function mapFieldTypeToFilterType( } /** - * Fit one option's preview line to its one line (objectui#13123): show every - * segment, then hide the last one still shown for as long as the line - * overflows, so a column the line cannot hold leaves WHOLE, together with every - * column after it. A segment that draws a badge never shrinks and a text - * segment never shrinks below its label (`previewOf`), so this is the only way - * a column gives way once the text values are cut as far as they go. + * The rows an option's preview may wrap onto. A column that does not fit + * beside the one before it starts the next row whole, so two text columns are + * not both cut to fit one row (objectstack#34362); past this many rows a column + * leaves, and an option's height stays bounded for the list's keyboard scroll. + */ +const LOOKUP_PREVIEW_MAX_ROWS = 2; + +/** The rows the shown segments occupy, read off their rendered offsets. */ +function previewRowsOf(segments: HTMLElement[]): number { + return new Set(segments.filter((s) => !s.hidden).map((s) => s.offsetTop)).size; +} + +/** + * Fit one option's preview to its rows (objectui#13123, objectstack#34362): + * show every segment, then hide the last one still shown for as long as the + * line overflows its width or wraps past {@link LOOKUP_PREVIEW_MAX_ROWS}, so a + * column the line cannot hold leaves WHOLE, together with every column after + * it. A segment that draws a badge never shrinks and a text segment never + * shrinks below its label (`previewOf`), so this is the only way a column + * gives way once the text values are cut as far as they go. + * + * Then each shown segment that opens a row drops its leading separator, and a + * text value cut short carries its whole text as its `title`, so a value the + * row cannot hold stays readable. * * Read off the rendered box, never computed from field types: a cell renderer * draws what it draws, and only the browser knows how wide that is. jsdom has - * no layout, where both widths read 0 and nothing is hidden. + * no layout, where every width and offset reads 0 and nothing is hidden. */ function fitPreviewLine(line: HTMLElement): void { const segments = Array.from(line.children) as HTMLElement[]; - for (const segment of segments) segment.hidden = false; - for (let i = segments.length - 1; i >= 0 && line.scrollWidth > line.clientWidth; i -= 1) { + for (const segment of segments) { + segment.hidden = false; + const separator = segment.querySelector('[data-lookup-preview-separator]'); + if (separator) separator.hidden = false; + } + const overflows = () => + line.scrollWidth > line.clientWidth || previewRowsOf(segments) > LOOKUP_PREVIEW_MAX_ROWS; + for (let i = segments.length - 1; i >= 0 && overflows(); i -= 1) { segments[i].hidden = true; } + let rowTop: number | null = null; + for (const segment of segments) { + if (segment.hidden) continue; + const opensRow = rowTop !== null && segment.offsetTop !== rowTop; + const separator = segment.querySelector('[data-lookup-preview-separator]'); + if (separator && opensRow) separator.hidden = true; + rowTop = segment.offsetTop; + const text = segment.querySelector('[data-lookup-preview-text]'); + if (!text) continue; + if (text.scrollWidth > text.clientWidth) text.title = (text.textContent ?? '').trim(); + else text.removeAttribute('title'); + } } /** - * The line under an option's label: the segments `previewOf` builds, fitted by + * The preview under an option's label: the segments `previewOf` builds, + * wrapping onto at most {@link LOOKUP_PREVIEW_MAX_ROWS} rows, fitted by * {@link fitPreviewLine} after every commit (the segments may have changed) * and whenever the line's own box is resized (the popover follows its * trigger's width). @@ -291,7 +328,7 @@ function OptionPreviewLine({ children }: { children: React.ReactNode }) { return ( {children} @@ -348,6 +385,17 @@ export function LookupField({ value, onChange, field, readonly, error: fieldErro // Arrow-key active index (-1 = none) const [activeIndex, setActiveIndex] = useState(-1); const listRef = useRef(null); + // Which edges of the option list have more options past them: each draws a + // fade, so a row cut at the list's edge reads as "scroll for more" + // (objectstack#34362). + const [listMore, setListMore] = useState<{ above: boolean; below: boolean }>({ above: false, below: false }); + const readListMore = useCallback(() => { + const list = listRef.current; + const next = list + ? { above: list.scrollTop > 0, below: list.scrollTop + list.clientHeight < list.scrollHeight - 1 } + : { above: false, below: false }; + setListMore((prev) => (prev.above === next.above && prev.below === next.below ? prev : next)); + }, []); const lookupField = field as any; @@ -1462,8 +1510,10 @@ export function LookupField({ value, onChange, field, readonly, error: fieldErro * - any other column truncates its VALUE, never below its label (the * `minmax(0, max-content)` track gives the value nothing of the * segment's minimum width, so that minimum is the label); - * - a column the line still cannot hold leaves whole, with every column - * after it (`fitPreviewLine`). + * - a column that does not fit beside the one before it wraps whole onto + * the next row, and a column the rows still cannot hold leaves whole, + * with every column after it (`fitPreviewLine`); + * - a value cut short carries its whole text as its `title`. */ const previewOf = useCallback( (option: LookupOption): React.ReactNode => { @@ -1489,7 +1539,7 @@ export function LookupField({ value, onChange, field, readonly, error: fieldErro // gave way, and the row would grow. const lead = ( - {i > 0 && } + {i > 0 && } {`${col.label || fieldToLabel(col.field)}:`} ); @@ -1506,7 +1556,7 @@ export function LookupField({ value, onChange, field, readonly, error: fieldErro ) : ( {lead} - {value} + {value} ); }); @@ -1543,6 +1593,26 @@ export function LookupField({ value, onChange, field, readonly, error: fieldErro } }, [activeIndex]); + // The fades follow the list after every commit (options arrive, rows + // re-fit), and on a resize, which no commit announces: the list's box + // follows the room the popover is given, not React state. + React.useLayoutEffect(() => { + readListMore(); + }); + const attachList = useCallback( + (list: HTMLDivElement | null) => { + listRef.current = list; + if (!list || typeof ResizeObserver === 'undefined') return; + const observer = new ResizeObserver(() => readListMore()); + observer.observe(list); + return () => { + observer.disconnect(); + if (listRef.current === list) listRef.current = null; + }; + }, + [readListMore], + ); + if (readonly) { if (!selectedOptions.length) { if (hydrating) { @@ -1828,12 +1898,20 @@ export function LookupField({ value, onChange, field, readonly, error: fieldErro {triggerButton} - + {/* The popover never runs past the room Radix reports on the side it + opens to, and goes to the side with more room when its whole list + does not fit (Radix's default flip, kept): the search row keeps its + height and the list takes what is left (objectstack#34362). */} + {/* Search input — not offered on a target whose search resolves to no field (objectui#12966, see `searchOffered`); the options below are buttons, so the list stays reachable without it. */} {searchOffered && ( -
+
+

{error}