The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Use native buttons for JSON and avatar actions (#26997)
Raphaël Bosicb75378ed98e
diff --git a/packages/twenty-docs/ui/components/json-tree.mdx b/packages/twenty-docs/ui/components/json-tree.mdxindex 2a152ef262b..f7ef9d726e1 100644--- a/packages/twenty-docs/ui/components/json-tree.mdx+++ b/packages/twenty-docs/ui/components/json-tree.mdx@@ -26,6 +26,8 @@ Each entry has an `id`, `label`, and JSON `value`. Use entries when display labe`getNodeHighlighting(keyPath)` returns `blue`, `red`, `partial-blue`, or `undefined`. `onNodeValueClick` receives the displayed text: leaf values as strings, and the matching empty label for empty strings, arrays, and objects. The tree displays data and does not edit it.+When `onNodeValueClick` is provided, values are native buttons named by their displayed text. Tab moves focus to a value; Enter, Space, or a pointer click activates it. Without the callback, values remain non-interactive text. Value buttons do not submit enclosing forms.+## Props<JsonTreeReference />diff --git a/packages/twenty-front-component-renderer/src/__stories__/TwentyUiGallery.stories.tsx b/packages/twenty-front-component-renderer/src/__stories__/TwentyUiGallery.stories.tsxindex 15a15e1d94c..9ac7cc70324 100644--- a/packages/twenty-front-component-renderer/src/__stories__/TwentyUiGallery.stories.tsx+++ b/packages/twenty-front-component-renderer/src/__stories__/TwentyUiGallery.stories.tsx@@ -1,3 +1,4 @@+import { jsonTreeTest } from '@/__stories__/twenty-ui-gallery/utils/jsonTreeTest';import { inlineBannerSandboxTest } from '@/__stories__/twenty-ui-gallery/utils/inlineBannerSandboxTest';import { themeTokenTest } from '@/__stories__/twenty-ui-gallery/utils/themeTokenTest';import { inputTest } from '@/__stories__/twenty-ui-gallery/utils/inputTest';@@ -114,12 +115,12 @@ export const InputPreact: Story = createGalleryStory({export const JsonVisualizerReact: Story = createGalleryStory({frontComponentBundleName: 'twenty-ui-json-visualizer-gallery',runtime: 'react',- play: galleryRenderTest,+ play: jsonTreeTest,});export const JsonVisualizerPreact: Story = createGalleryStory({frontComponentBundleName: 'twenty-ui-json-visualizer-gallery',runtime: 'preact',- play: galleryRenderTest,+ play: jsonTreeTest,});export const LayoutReact: Story = createGalleryStory({diff --git a/packages/twenty-front-component-renderer/src/__stories__/twenty-ui-gallery/twenty-ui-json-visualizer-gallery.front-component.tsx b/packages/twenty-front-component-renderer/src/__stories__/twenty-ui-gallery/twenty-ui-json-visualizer-gallery.front-component.tsxindex d32318ffbc2..da46aa3e29d 100644--- a/packages/twenty-front-component-renderer/src/__stories__/twenty-ui-gallery/twenty-ui-json-visualizer-gallery.front-component.tsx+++ b/packages/twenty-front-component-renderer/src/__stories__/twenty-ui-gallery/twenty-ui-json-visualizer-gallery.front-component.tsx@@ -1,5 +1,8 @@+import { useState } from 'react';+import { Button } from 'twenty-ui/primitives/input';import { defineFrontComponent } from 'twenty-sdk/define';import { JsonTree } from 'twenty-ui/components';+import 'twenty-ui/style.css';import { ThemeProvider } from 'twenty-ui/theme';import {@@ -24,14 +27,60 @@ const JSON_VISUALIZER_ENTRIES: GalleryEntry[] = [},];-const JsonVisualizerGallery = () => (- <ThemeProvider colorScheme="light">- <ComponentGallery- title="twenty-ui/components: JsonTree"- entries={JSON_VISUALIZER_ENTRIES}- />- </ThemeProvider>-);+const JsonVisualizerGallery = () => {+ const [activations, setActivations] = useState(0);+ const [parentClicks, setParentClicks] = useState(0);+ const [submissions, setSubmissions] = useState(0);+ const [lastValue, setLastValue] = useState('');+ const onNodeValueClick = (value: string) => {+ setActivations((count) => count + 1);+ setLastValue(value);+ };++ return (+ <ThemeProvider colorScheme="light">+ <ComponentGallery+ title="twenty-ui/components: JsonTree"+ entries={JSON_VISUALIZER_ENTRIES}+ />+ <Button>Before values</Button>+ <form+ onClick={() => setParentClicks((count) => count + 1)}+ onSubmit={(event) => {+ event.preventDefault();+ setSubmissions((count) => count + 1);+ }}+ >+ <JsonTree+ value="Copy this value"+ emptyArrayLabel="[empty array]"+ emptyObjectLabel="[empty object]"+ emptyStringLabel="[empty string]"+ arrowButtonCollapsedLabel="Expand"+ arrowButtonExpandedLabel="Collapse"+ onNodeValueClick={onNodeValueClick}+ />+ <fieldset disabled>+ <legend>Unavailable values</legend>+ <JsonTree+ value="Disabled value"+ emptyArrayLabel="[empty array]"+ emptyObjectLabel="[empty object]"+ emptyStringLabel="[empty string]"+ arrowButtonCollapsedLabel="Expand"+ arrowButtonExpandedLabel="Collapse"+ onNodeValueClick={onNodeValueClick}+ />+ </fieldset>+ </form>+ <Button>After values</Button>+ <output aria-label="Value activations">{activations}</output>+ <output aria-label="Parent clicks">{parentClicks}</output>+ <output aria-label="Form submissions">{submissions}</output>+ <output aria-label="Last value">{lastValue}</output>+ </ThemeProvider>+ );+};export default defineFrontComponent({universalIdentifier: 'test-20ui0-0000-0000-0000-000000000106',diff --git a/packages/twenty-front-component-renderer/src/__stories__/twenty-ui-gallery/utils/jsonTreeTest.ts b/packages/twenty-front-component-renderer/src/__stories__/twenty-ui-gallery/utils/jsonTreeTest.tsnew file mode 100644index 00000000000..a3958249688--- /dev/null+++ b/packages/twenty-front-component-renderer/src/__stories__/twenty-ui-gallery/utils/jsonTreeTest.ts@@ -0,0 +1,54 @@+import { expect, userEvent, waitFor, within } from 'storybook/test';++import { type TwentyUiGalleryPlayFunction } from '@/__stories__/twenty-ui-gallery/types/TwentyUiGalleryPlayFunction';++export const jsonTreeTest: TwentyUiGalleryPlayFunction = async ({+ canvasElement,+}) => {+ const canvas = within(canvasElement);+ const user = userEvent.setup();+ const value = await canvas.findByRole(+ 'button',+ { name: 'Copy this value' },+ { timeout: 10000 },+ );+ const activations = canvas.getByLabelText('Value activations');+ const parentClicks = canvas.getByLabelText('Parent clicks');++ await user.click(canvas.getByRole('button', { name: 'Before values' }));+ await user.tab();+ expect(value).toHaveFocus();+ expect(value).toHaveAttribute('type', 'button');+ expect(getComputedStyle(value).outlineStyle).toBe('solid');+ expect(value.getBoundingClientRect().height).toBe(24);+ await user.keyboard('{Enter}');+ await waitFor(() => expect(activations).toHaveTextContent('1'));+ await user.keyboard('[Space>]');+ expect(activations).toHaveTextContent('1');+ await user.keyboard('[/Space]');+ await waitFor(() => expect(activations).toHaveTextContent('2'));+ await user.click(value);+ await waitFor(() => {+ expect(activations).toHaveTextContent('3');+ expect(parentClicks).toHaveTextContent('3');+ expect(canvas.getByLabelText('Last value')).toHaveTextContent(+ 'Copy this value',+ );+ });+ expect(value).toHaveFocus();+ const disabledValue = canvas.getByRole('button', { name: 'Disabled value' });+ expect(disabledValue).toBeDisabled();+ await user.click(disabledValue);+ value.focus();+ await user.tab();+ expect(canvas.getByRole('button', { name: 'After values' })).toHaveFocus();+ expect(activations).toHaveTextContent('3');+ expect(canvas.getByLabelText('Form submissions')).toHaveTextContent('0');++ expect(+ canvas.queryByRole('button', { name: 'Twenty' }),+ ).not.toBeInTheDocument();+ const collapseButtons = canvas.getAllByRole('button', { name: 'Collapse' });+ await user.click(collapseButtons[0]);+ await canvas.findByRole('button', { name: 'Expand' });+};diff --git a/packages/twenty-front/src/modules/ui/field/display/components/internal/AvatarOrIcon/AvatarOrIcon.tsx b/packages/twenty-front/src/modules/ui/field/display/components/internal/AvatarOrIcon/AvatarOrIcon.tsxindex 9f93c4065b0..60d3364031b 100644--- a/packages/twenty-front/src/modules/ui/field/display/components/internal/AvatarOrIcon/AvatarOrIcon.tsx+++ b/packages/twenty-front/src/modules/ui/field/display/components/internal/AvatarOrIcon/AvatarOrIcon.tsx@@ -1,7 +1,6 @@import { css } from '@linaria/core';import { isNonEmptyString } from '@sniptt/guards';-import { handleClickableElementKeyDown } from 'twenty-ui/primitives/accessibility';import { Avatar } from 'twenty-ui/primitives/data-display';import { useTheme } from 'twenty-ui/theme';import { isDefined } from 'twenty-ui/utilities';@@ -28,7 +27,21 @@ const styles = {display: flex;}&[data-clickable] {+ background: none;+ border: 0;+ border-radius: var(--t-border-radius-sm);+ color: inherit;cursor: pointer;+ font: inherit;+ margin: 0;+ padding: 0;+ }+ &:focus-visible {+ outline: 2px solid var(--t-color-blue);+ outline-offset: 1px;+ }+ &:disabled {+ cursor: inherit;}`,};@@ -63,7 +76,7 @@ export const AvatarOrIcon = ({const iconContent =isIconInverted || isDefined(IconBackgroundColor) ? (- <div+ <spanclassName={styles.iconWithBackgroundContainer}style={isDefined(IconBackgroundColor)@@ -79,7 +92,7 @@ export const AvatarOrIcon = ({stroke={theme.icon.stroke.sm}aria-hidden/>- </div>+ </span>) : (<Iconsize={theme.icon.size.sm}@@ -91,17 +104,15 @@ export const AvatarOrIcon = ({if (isDefined(onClick)) {return (- <div+ <button+ type="button"className={styles.wrapper}data-clickable={true}- role="button"- tabIndex={0}aria-label={accessibleLabel}onClick={onClick}- onKeyDown={handleClickableElementKeyDown}>{iconContent}- </div>+ </button>);}diff --git a/packages/twenty-front/src/modules/ui/field/display/components/internal/AvatarOrIcon/__stories__/AvatarOrIcon.stories.tsx b/packages/twenty-front/src/modules/ui/field/display/components/internal/AvatarOrIcon/__stories__/AvatarOrIcon.stories.tsxindex 9ef8db60b1f..bb881c2477c 100644--- a/packages/twenty-front/src/modules/ui/field/display/components/internal/AvatarOrIcon/__stories__/AvatarOrIcon.stories.tsx+++ b/packages/twenty-front/src/modules/ui/field/display/components/internal/AvatarOrIcon/__stories__/AvatarOrIcon.stories.tsx@@ -1,4 +1,8 @@import { type Meta, type StoryObj } from '@storybook/react-vite';+import { type FormEvent } from 'react';+import { expect, fn, userEvent, within } from 'storybook/test';+import { Chip } from 'twenty-ui/primitives/data-display';+import { Button } from 'twenty-ui/primitives/input';import { IconBuildingSkyscraper, IconUser } from 'twenty-ui/icon';import { AVATAR_URL_MOCK, ComponentDecorator } from 'twenty-ui/testing';import { AvatarOrIcon } from '@/ui/field/display/components/internal/AvatarOrIcon/AvatarOrIcon';@@ -47,19 +51,157 @@ export const WithInvertedIcon: Story = {},};+const onContainerClick = fn();+const onContainerKeyDown = fn();+const onFormSubmit = fn((event: FormEvent) => event.preventDefault());++const clickableStory: Story = {+ beforeEach: () => {+ onContainerClick.mockClear();+ onContainerKeyDown.mockClear();+ onFormSubmit.mockClear();+ },+ render: (args) => (+ <>+ <Button>Before avatar</Button>+ <form+ onClick={onContainerClick}+ onKeyDown={onContainerKeyDown}+ onSubmit={onFormSubmit}+ >+ <AvatarOrIcon {...args} />+ </form>+ </>+ ),+ play: async ({ canvasElement, args }) => {+ const canvas = within(canvasElement);+ const user = userEvent.setup();+ const button = canvas.getByRole('button', { name: args.name ?? 'Avatar' });++ await user.click(canvas.getByRole('button', { name: 'Before avatar' }));+ await user.tab();+ expect(button).toHaveFocus();+ expect(button).toHaveAttribute('type', 'button');+ expect(getComputedStyle(button).outlineStyle).toBe('solid');+ expect(button.getBoundingClientRect().width).toBe(14);+ expect(button.getBoundingClientRect().height).toBe(14);++ await user.keyboard('{Enter}');+ expect(args.onClick).toHaveBeenCalledTimes(1);+ await user.keyboard('[Space>]');+ expect(args.onClick).toHaveBeenCalledTimes(1);+ await user.keyboard('[/Space]');+ expect(args.onClick).toHaveBeenCalledTimes(2);+ expect(onContainerKeyDown).toHaveBeenCalledTimes(2);+ await user.click(button);+ expect(args.onClick).toHaveBeenCalledTimes(3);+ expect(onContainerClick).toHaveBeenCalledTimes(3);+ expect(button).toHaveFocus();+ expect(onFormSubmit).not.toHaveBeenCalled();+ },+};+export const Clickable: Story = {+ ...clickableStory,args: {name: 'JD',colorSeed: 'John Doe',- onClick: () => alert('AvatarOrIcon clicked'),+ onClick: fn(),},};export const ClickableIcon: Story = {+ ...clickableStory,args: {Icon: IconBuildingSkyscraper,isIconInverted: true,name: 'Company',- onClick: () => alert('Icon AvatarOrIcon clicked'),+ onClick: fn(),+ },+};++export const ClickablePlainIcon: Story = {+ ...clickableStory,+ args: {+ Icon: IconUser,+ onClick: fn(),+ },+};++export const FileActionPropagation: Story = {+ args: {+ Icon: IconUser,+ name: 'Remove file',+ onClick: fn(),+ },+ beforeEach: () => onContainerClick.mockClear(),+ render: (args) => (+ <div onClick={onContainerClick}>+ <Chip+ endElement={+ <span onClick={(event) => event.stopPropagation()}>+ <AvatarOrIcon {...args} />+ </span>+ }+ >+ File preview+ </Chip>+ </div>+ ),+ play: async ({ canvasElement, args }) => {+ const canvas = within(canvasElement);+ const user = userEvent.setup();+ const removeButton = canvas.getByRole('button', { name: 'Remove file' });++ expect(canvas.getAllByRole('button')).toHaveLength(1);+ await user.click(removeButton);+ await user.keyboard('{Enter} ');+ expect(args.onClick).toHaveBeenCalledTimes(3);+ expect(onContainerClick).not.toHaveBeenCalled();+ },+};++export const DisabledFieldset: Story = {+ args: { onClick: fn() },+ render: (args) => (+ <>+ <Button>Before disabled avatars</Button>+ <fieldset disabled>+ <legend>Unavailable avatars</legend>+ <AvatarOrIcon {...args} name="Person" />+ <AvatarOrIcon {...args} name="Company" Icon={IconBuildingSkyscraper} />+ </fieldset>+ <Button>After disabled avatars</Button>+ </>+ ),+ play: async ({ canvasElement, args }) => {+ const canvas = within(canvasElement);+ const user = userEvent.setup();+ await user.click(+ canvas.getByRole('button', { name: 'Before disabled avatars' }),+ );+ await user.tab();+ expect(+ canvas.getByRole('button', { name: 'After disabled avatars' }),+ ).toHaveFocus();+ for (const name of ['Person', 'Company']) {+ const button = canvas.getByRole('button', { name });+ expect(button).toBeDisabled();+ await user.click(button);+ }+ expect(args.onClick).not.toHaveBeenCalled();+ },+};++export const DecorativeIconInButton: Story = {+ render: () => (+ <Button startIcon={<AvatarOrIcon Icon={IconUser} />}>Open person</Button>+ ),+ play: async ({ canvasElement }) => {+ const canvas = within(canvasElement);+ const user = userEvent.setup();+ expect(canvas.getAllByRole('button')).toHaveLength(1);+ await user.tab();+ expect(canvas.getByRole('button', { name: 'Open person' })).toHaveFocus();},};diff --git a/packages/twenty-ui/src/components/data-display/JsonTree/__stories__/JsonTree.stories.tsx b/packages/twenty-ui/src/components/data-display/JsonTree/__stories__/JsonTree.stories.tsxindex 1f0167d00ba..3b7337bd31b 100644--- a/packages/twenty-ui/src/components/data-display/JsonTree/__stories__/JsonTree.stories.tsx+++ b/packages/twenty-ui/src/components/data-display/JsonTree/__stories__/JsonTree.stories.tsx@@ -1,6 +1,8 @@import { type Meta, type StoryObj } from '@storybook/react-vite';import { JsonTree } from '@ui/components/data-display/JsonTree/JsonTree';import { isTwoFirstDepths } from '@ui/components/data-display/JsonTree/internal/utils/isTwoFirstDepths';+import { Button } from '@ui/primitives/input/Button/Button';+import { type FormEvent } from 'react';import { A11Y_DEFER_COLOR_CONTRAST } from '@ui/testing';import {expect,@@ -40,6 +42,7 @@ export const String: Story = {const node = await canvas.findByText('Hello');expect(node).toBeVisible();+ expect(canvas.queryByRole('button')).not.toBeInTheDocument();},};@@ -648,3 +651,74 @@ export const GroupedRoots: Story = {onNodeValueClick: fn(),},};++const onValueContainerClick = fn();+const onValueContainerKeyDown = fn();+const onValueFormSubmit = fn((event: FormEvent) => event.preventDefault());++export const NativeValueActions: Story = {+ parameters: { a11y: A11Y_DEFER_COLOR_CONTRAST },+ args: {+ value: 'Copy this value',+ onNodeValueClick: fn(),+ },+ beforeEach: () => {+ onValueContainerClick.mockClear();+ onValueContainerKeyDown.mockClear();+ onValueFormSubmit.mockClear();+ },+ render: (args) => (+ <>+ <Button>Before values</Button>+ <form+ onClick={onValueContainerClick}+ onKeyDown={onValueContainerKeyDown}+ onSubmit={onValueFormSubmit}+ >+ <JsonTree {...args} />+ <fieldset disabled>+ <legend>Unavailable values</legend>+ <JsonTree {...args} entries={undefined} value="Disabled value" />+ </fieldset>+ </form>+ <Button>After values</Button>+ </>+ ),+ play: async ({ canvasElement, args }) => {+ const canvas = within(canvasElement);+ const user = userEvent.setup();+ const value = canvas.getByRole('button', { name: 'Copy this value' });+ const disabledValue = canvas.getByRole('button', {+ name: 'Disabled value',+ });++ await user.click(canvas.getByRole('button', { name: 'Before values' }));+ await user.tab();+ expect(value).toHaveFocus();+ expect(value).toHaveAttribute('type', 'button');+ expect(getComputedStyle(value).outlineStyle).toBe('solid');+ expect(value.getBoundingClientRect().height).toBe(24);+ expect(getComputedStyle(value).paddingInlineStart).toBe('0px');++ await user.keyboard('{Enter}');+ expect(args.onNodeValueClick).toHaveBeenCalledTimes(1);+ await user.keyboard('[Space>]');+ expect(args.onNodeValueClick).toHaveBeenCalledTimes(1);+ await user.keyboard('[/Space]');+ expect(args.onNodeValueClick).toHaveBeenCalledTimes(2);+ expect(onValueContainerKeyDown).toHaveBeenCalledTimes(2);+ await user.click(value);+ expect(args.onNodeValueClick).toHaveBeenCalledTimes(3);+ expect(args.onNodeValueClick).toHaveBeenLastCalledWith('Copy this value');+ expect(onValueContainerClick).toHaveBeenCalledTimes(3);+ expect(value).toHaveFocus();++ await user.click(disabledValue);+ expect(disabledValue).toBeDisabled();+ expect(args.onNodeValueClick).toHaveBeenCalledTimes(3);+ value.focus();+ await user.tab();+ expect(canvas.getByRole('button', { name: 'After values' })).toHaveFocus();+ expect(onValueFormSubmit).not.toHaveBeenCalled();+ },+};diff --git a/packages/twenty-ui/src/components/data-display/JsonTree/internal/JsonNodeValue.module.scss b/packages/twenty-ui/src/components/data-display/JsonTree/internal/JsonNodeValue.module.scssindex 3552c2bf0d7..a89e8b11480 100644--- a/packages/twenty-ui/src/components/data-display/JsonTree/internal/JsonNodeValue.module.scss+++ b/packages/twenty-ui/src/components/data-display/JsonTree/internal/JsonNodeValue.module.scss@@ -14,3 +14,20 @@.red {color: var(--t-color-red8);}++.button {+ @include focus-ring;++ background: none;+ border: 0;+ cursor: pointer;+ font: inherit;+ line-height: 1;+ margin: 0;+ padding: 0;+ text-align: inherit;++ &:disabled {+ cursor: inherit;+ }+}diff --git a/packages/twenty-ui/src/components/data-display/JsonTree/internal/JsonNodeValue.module.scss.d.ts b/packages/twenty-ui/src/components/data-display/JsonTree/internal/JsonNodeValue.module.scss.d.tsindex da5e6e7c227..39036585171 100644--- a/packages/twenty-ui/src/components/data-display/JsonTree/internal/JsonNodeValue.module.scss.d.ts+++ b/packages/twenty-ui/src/components/data-display/JsonTree/internal/JsonNodeValue.module.scss.d.ts@@ -2,5 +2,6 @@ declare const classNames: {readonly text: 'text';readonly blue: 'blue';readonly red: 'red';+ readonly button: 'button';};export default classNames;diff --git a/packages/twenty-ui/src/components/data-display/JsonTree/internal/JsonNodeValue.tsx b/packages/twenty-ui/src/components/data-display/JsonTree/internal/JsonNodeValue.tsxindex cd890834931..b1f34c2ad88 100644--- a/packages/twenty-ui/src/components/data-display/JsonTree/internal/JsonNodeValue.tsx+++ b/packages/twenty-ui/src/components/data-display/JsonTree/internal/JsonNodeValue.tsx@@ -1,6 +1,5 @@import { useJsonTreeContextOrThrow } from '@ui/components/data-display/JsonTree/internal/hooks/useJsonTreeContextOrThrow';import { type JsonNodeHighlighting } from '@ui/components/data-display/JsonTree/types/JsonNodeHighlighting';-import { handleClickableElementKeyDown } from '@ui/primitives/accessibility/utils/handleClickableElementKeyDown';import { isDefined } from '@ui/utilities/utils/isDefined';import { clsx } from 'clsx';@@ -29,15 +28,13 @@ export const JsonNodeValue = ({if (isInteractive) {return (- <span- className={valueClassName}- role="button"- tabIndex={0}+ <button+ className={clsx(valueClassName, styles.button)}+ type="button"onClick={handleClick}- onKeyDown={handleClickableElementKeyDown}>{valueAsString}- </span>+ </button>);}