Skip to content
Merged
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
14 changes: 14 additions & 0 deletions .changeset/36479-lookup-preview-link-free.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
'@object-ui/fields': patch
---

fix(fields): a tap anywhere on a lookup picker option selects it, including on a record chip in its preview

Clause-②: no
Metadata-protocol: none

A lookup picker's option shows a preview of its other columns under its label. A column that references another record drew a link to that record there (on a record page, its chip), and a tap or a click on it opened that record instead of selecting the option, leaving the edit behind. An email, URL or phone column in the preview did the same with the mail client, the browser or the dialer.

The preview now draws every column the way the "browse all records" picker table already did: a reference keeps its face, the record page's chip included, without the link, and an email, URL, phone or file column shows its text. A tap or a click anywhere on the option selects it, on a phone and on a desktop. Both picker surfaces now take this rule from the one renderer they share.

What does not change: the read cells outside the picker (record page fields, list and grid cells) keep their links. No metadata key, value or default changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/**
* ObjectUI
* Copyright (c) 2024-present ObjectStack Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

/**
* A tap anywhere on a lookup quick-picker option selects that option —
* objectstack#36479.
*
* Seen on the showcase Field Zoo record at 375 px: editing the Master-Detail
* field (target: Project) and tapping the record chip `Account: Fabrikam`
* inside an option's preview opened the Fabrikam record mid-edit; the option
* was not selected. The preview draws each column through the read cell, and
* the read cell draws a reference as a link to its record whenever the host
* answers `recordHref`. The link's own click handler stops propagation and
* hands the click to the host's `openRecord`, so the option row never heard it
* — whatever the pointer: a mouse click on a desktop did the same.
*
* The rule is the picker table's (objectui#11903, point 7), now held by the
* one renderer both picker surfaces call (`renderLookupColumnValue`): a
* candidate's column never renders as a navigational link. A reference keeps
* its face (the record page's chip) without the anchor, and a type that draws
* its own anchor (`mailto:`, a URL, `tel:`, a file) is drawn with the text face.
*/

import * as React from 'react';
import { describe, it, expect, vi, afterEach } from 'vitest';
import { render, screen, cleanup, waitFor, act, fireEvent, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import '@testing-library/jest-dom';
import { RelatedRecordActionsProvider } from '@object-ui/react';
import { LookupField } from './LookupField';
// Installs the cell-renderer resolver the preview draws its values with, and
// the record page's chip context.
import { ReferenceChipContext } from '../index';

/** The referenced object (`showcase_project`) as `getObjectSchema` serves it. */
const FIELDS: Record<string, Record<string, unknown>> = {
name: { type: 'text', label: 'Project Name' },
account: { type: 'lookup', label: 'Account', reference: 'showcase_account' },
contact_email: { type: 'email', label: 'Contact Email' },
};

const ROW = {
id: 'prj_1',
name: 'Website Relaunch',
// As the candidate query serves it, `$expand`-ed (objectui#10223).
account: { id: 'acc_9', name: 'Fabrikam' },
contact_email: 'pm@fabrikam.example.com',
};

function makeDataSource() {
return {
find: vi.fn(async () => ({ data: [ROW], total: 1 })),
findOne: vi.fn(async () => ROW),
getObjectSchema: vi.fn(async (objectName: string) => ({ name: objectName, fields: FIELDS })),
};
}

/** The record page's host: it routes every object, and opens records in-app. */
function makeHost() {
return {
recordHref: (objectName: string, recordId: string | number) =>
`/apps/showcase/${objectName}/record/${encodeURIComponent(String(recordId))}`,
openRecord: vi.fn(),
};
}

async function openPicker(columns: string[]) {
const host = makeHost();
const onChange = vi.fn();
render(
// The record page's field area: references are drawn as chips (objectstack#36147).
<RelatedRecordActionsProvider value={host as never}>
<ReferenceChipContext.Provider value>
<LookupField
value={undefined}
onChange={onChange}
dataSource={makeDataSource() as never}
field={{
reference: 'showcase_project',
displayField: 'name',
lookupColumns: columns.map((field) => ({ field, label: FIELDS[field].label as string })),
} as never}
/>
</ReferenceChipContext.Provider>
</RelatedRecordActionsProvider>,
);
await act(async () => {
fireEvent.click(screen.getByTestId('lookup-trigger'));
});
await waitFor(() => expect(screen.getByText('Website Relaunch')).toBeInTheDocument());
return { host, onChange };
}

/** The value one previewed column renders. */
function previewValue(field: string): HTMLElement {
const el = document.querySelector<HTMLElement>(`[data-lookup-preview="${field}"]`);
if (!el) throw new Error(`no previewed value for ${field}`);
return el;
}

afterEach(() => {
cleanup();
vi.restoreAllMocks();
});

describe('LookupField option preview: a record chip selects its option (objectstack#36479)', () => {
// Touch (the phone the card was read on) and a mouse (a desktop) take the
// same path: the defect was the link's click handler, not the pointer.
it.each(['[TouchA]', '[MouseLeft]'])('%s on the chip selects the option and opens no record', async (keys) => {
const { host, onChange } = await openPicker(['account']);
const chip = await waitFor(() => {
const el = previewValue('account').querySelector<HTMLElement>('[data-record-chip]');
if (!el) throw new Error('the account column drew no chip');
return el;
});
expect(chip).toHaveTextContent('Fabrikam');

const user = userEvent.setup();
await user.pointer({ keys, target: chip });

expect(host.openRecord).not.toHaveBeenCalled();
expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange.mock.calls[0][0]).toBe('prj_1');
});

it('draws the chip with no anchor, so the option is the one click target', async () => {
await openPicker(['account']);
await waitFor(() =>
expect(previewValue('account').querySelector('[data-record-chip]')).toHaveTextContent('Fabrikam'),
);
expect(screen.getByRole('option').querySelector('a')).toBeNull();
});

it('draws a self-linking column with its text face, so a tap on it selects too', async () => {
const { onChange } = await openPicker(['contact_email']);
const value = previewValue('contact_email');
await waitFor(() => expect(value).toHaveTextContent('pm@fabrikam.example.com'));

// Read before the pick: a pick closes the popover.
const anchor = screen.getByRole('option').querySelector('a');

const user = userEvent.setup();
// The element that carries the address's text: the anchor, before the fix.
await user.click(within(value).getByText('pm@fabrikam.example.com'));

expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange.mock.calls[0][0]).toBe('prj_1');
expect(anchor).toBeNull();
});
});
56 changes: 54 additions & 2 deletions ui/packages/fields/src/widgets/lookupColumnDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

import React from 'react';
import { formatDate, getRecordDisplayName } from '@object-ui/core';
import { RelatedRecordActionsProvider, useRelatedRecordActions } from '@object-ui/react';
import type { LookupColumnDef } from '@object-ui/types';
import { BooleanValueText } from './booleanValueLabel.js';

Expand Down Expand Up @@ -167,9 +168,54 @@ export interface LookupColumnRenderContext {
displayLocale?: string;
}

/**
* Cell renderer keys that draw their value as their OWN anchor without asking
* the host — `mailto:`, a URL, `tel:`, and the file family's download link. A
* candidate's column draws them with {@link PICKER_TEXT_FACE} instead: the
* address, the URL, the number, the file's name.
*/
const PICKER_SELF_LINKING_CELL_TYPES: ReadonlySet<string> = new Set([
'email',
'url',
'phone',
'file',
'video',
'audio',
]);

/** The cell renderer key a self-linking value is drawn with in a picker. */
const PICKER_TEXT_FACE = 'text';

/**
* The reference family (the lookup cell's `ReferencedRecordLink`, its hover
* card) draws an anchor only when the host answers `recordHref`; under this
* wrapper the host answers no record destination, which the context documents
* as "render the plain value". Every other member of the host passes through;
* with no host there is nothing to withhold.
*/
function WithoutRecordLinks({ children }: { children: React.ReactNode }): React.ReactElement {
const host = useRelatedRecordActions();
if (!host) return <>{children}</>;
return (
<RelatedRecordActionsProvider value={{ ...host, recordHref: undefined, openRecord: undefined }}>
{children}
</RelatedRecordActionsProvider>
);
}

/**
* Render one column of one candidate record.
*
* A candidate's column is never a navigational link, on either picker surface:
* the option row (the dropdown) or the table row (the browse-all dialog) is the
* one click target, and a link inside it is a second one that competes with
* the pick — a record chip opening its record, a `mailto:` opening the mail
* client, instead of selecting the candidate (objectui#11903,
* objectstack#36479). So a self-linking type is drawn with the text face
* ({@link PICKER_SELF_LINKING_CELL_TYPES}), and every cell renders under a host
* with no record destination ({@link WithoutRecordLinks}): a reference keeps
* its face — the record page's chip included — without the anchor.
*
* Order:
* 1. the record title, for the display column only and only when the
* referenced object's schema is given: `getRecordDisplayName` with the
Expand Down Expand Up @@ -213,9 +259,15 @@ export function renderLookupColumnValue(
// schema field's type via `fieldsMeta`) and a resolver are available.
const descriptor = descriptors[col.field];
if (descriptor && cellRenderer) {
const Renderer = cellRenderer(descriptor.type);
const Renderer = cellRenderer(
PICKER_SELF_LINKING_CELL_TYPES.has(descriptor.type) ? PICKER_TEXT_FACE : descriptor.type,
);
if (Renderer) {
return <Renderer value={val} field={descriptor} />;
return (
<WithoutRecordLinks>
<Renderer value={val} field={descriptor} />
</WithoutRecordLinks>
);
}
}

Expand Down
Loading