From 6003359971373095c5ac38cde39de98b5ae66c34 Mon Sep 17 00:00:00 2001 From: Shreyag02 Date: Sun, 15 Nov 2026 22:02:47 +0530 Subject: [PATCH 1/5] fix: [select] show preset multiple values without crashing Select.Value looked values up in the item registry, which Select.Item fills in a layout effect. On the first client render, on the server and for values with no matching item, the multiple path passed undefined entries to SelectMultipleValue, which threw on item.leadingIcon. Resolve every value through one lookup that falls back to { value, children: value }. The lookup compares the stored value, so Object.prototype keys such as `constructor` also fall back. --- .../content/docs/components/select/index.mdx | 2 + .../content/docs/components/select/props.ts | 1 + .../select/__tests__/select.test.tsx | 85 +++++++++++++++++++ .../react/components/select/select-value.tsx | 8 +- 4 files changed, 94 insertions(+), 2 deletions(-) diff --git a/apps/www/src/content/docs/components/select/index.mdx b/apps/www/src/content/docs/components/select/index.mdx index 32d3865b8..4531ef748 100644 --- a/apps/www/src/content/docs/components/select/index.mdx +++ b/apps/www/src/content/docs/components/select/index.mdx @@ -126,6 +126,8 @@ The button that triggers the Select. Shows the selected item's label and icon in the trigger, or the placeholder when no item is selected. +A value with no matching `Select.Item` shows its raw value. + ### Content diff --git a/apps/www/src/content/docs/components/select/props.ts b/apps/www/src/content/docs/components/select/props.ts index e89c80015..69e3a804c 100644 --- a/apps/www/src/content/docs/components/select/props.ts +++ b/apps/www/src/content/docs/components/select/props.ts @@ -108,6 +108,7 @@ export interface SelectValueProps { /** * Content shown in place of the selected item's label. * A function receives the selected item, or an array of items when `multiple` is set. + * A value with no matching item is passed as `{ value, children: value }`. * * @remarks `ReactNode | function` */ diff --git a/packages/react/components/select/__tests__/select.test.tsx b/packages/react/components/select/__tests__/select.test.tsx index f5f6d1b00..e24d66c5f 100644 --- a/packages/react/components/select/__tests__/select.test.tsx +++ b/packages/react/components/select/__tests__/select.test.tsx @@ -1,5 +1,7 @@ import { fireEvent, render, screen, within } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; +import type { ReactNode } from 'react'; +import { renderToString } from 'react-dom/server'; import { describe, expect, it, vi } from 'vitest'; import labelStyles from '../../label/label.module.css'; import { Select } from '../select'; @@ -513,4 +515,87 @@ describe('Select', () => { expect(options[1]).toHaveTextContent('Pineapple'); }); }); + + describe('Preset Values', () => { + const getValue = (container: HTMLElement) => + container.querySelector('[data-slot="select-value"]'); + + it.each([ + { defaultValue: ['apple', 'banana'] }, + { value: ['apple', 'banana'] }, + { defaultValue: ['apple', 'banana'], defaultOpen: true } + ])('shows chips for preset multiple values (%o)', props => { + const { container } = render(); + const chips = container.querySelectorAll('[data-slot="chip"]'); + expect(chips[0]).toHaveTextContent('Apple'); + expect(getValue(container)).toHaveTextContent('+1'); + }); + + it('shows chips for preset multiple values in autocomplete mode', () => { + const { container } = render( + + ); + expect(getValue(container)).toHaveTextContent('Apple'); + }); + + it('shows the raw value for a value with no matching item', () => { + const { container } = render( + + ); + const chips = container.querySelectorAll('[data-slot="chip"]'); + expect(chips[0]).toHaveTextContent('zzz'); + }); + + it('shows the raw value for a value named like an Object.prototype key', () => { + const { container } = render( + + ); + const chips = container.querySelectorAll('[data-slot="chip"]'); + expect(chips[0]).toHaveTextContent('constructor'); + }); + + it('renders preset multiple values on the server', () => { + const html = renderToString( + + ); + expect(html).toContain('apple'); + expect(html).toContain('banana'); + }); + + it('passes an item for every value to a children function', () => { + const renderValue = vi.fn((_value?: unknown) => null); + render( + + ); + expect(renderValue).toHaveBeenCalled(); + for (const [arg] of renderValue.mock.calls) { + expect(Array.isArray(arg)).toBe(true); + expect(arg).not.toContain(undefined); + } + }); + + it('shows one preset multiple value as text', () => { + const { container } = render( + + ); + expect(getValue(container)).toHaveTextContent('Apple'); + expect(container.querySelector('[data-slot="chip"]')).toBeNull(); + }); + }); }); diff --git a/packages/react/components/select/select-value.tsx b/packages/react/components/select/select-value.tsx index 575b3bb13..6ed2c070e 100644 --- a/packages/react/components/select/select-value.tsx +++ b/packages/react/components/select/select-value.tsx @@ -29,12 +29,16 @@ export function SelectValue({ const item = useMemo(() => { if (!value) return undefined; + // Items register in a layout effect, so a value may have no item yet or none at all. + // Comparing `value` also skips Object.prototype keys such as `constructor`. + const getItem = (v: string): ItemType => + items[v]?.value === v ? items[v] : { value: v, children: v }; if (multiple && Array.isArray(value)) { - const itemValues = value.map(v => items[v]); + const itemValues = value.map(getItem); if (itemValues.length === 1) return itemValues[0]; return itemValues; } - return items[value as string]; + return getItem(value as string); }, [value, items, multiple]); if (!hasValue) { From 7c6f2404687b1f9b7591474caa80f1d05ff60bab Mon Sep 17 00:00:00 2001 From: Shreyag02 Date: Mon, 16 Nov 2026 00:42:50 +0530 Subject: [PATCH 2/5] fix: [select] match the unmatched-value docs to the function type --- apps/www/src/content/docs/components/select/props.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/www/src/content/docs/components/select/props.ts b/apps/www/src/content/docs/components/select/props.ts index 69e3a804c..6b65f703d 100644 --- a/apps/www/src/content/docs/components/select/props.ts +++ b/apps/www/src/content/docs/components/select/props.ts @@ -108,7 +108,7 @@ export interface SelectValueProps { /** * Content shown in place of the selected item's label. * A function receives the selected item, or an array of items when `multiple` is set. - * A value with no matching item is passed as `{ value, children: value }`. + * A value with no matching item is passed with its raw `value` and no `leadingIcon`. * * @remarks `ReactNode | function` */ From 0df1ce2e520120b402299932f9115e2993e81f2f Mon Sep 17 00:00:00 2001 From: Shreyag02 Date: Mon, 16 Nov 2026 01:22:10 +0530 Subject: [PATCH 3/5] test: [select] cover a single-mode children function with no item --- .../select/__tests__/select.test.tsx | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/packages/react/components/select/__tests__/select.test.tsx b/packages/react/components/select/__tests__/select.test.tsx index e24d66c5f..d9062932d 100644 --- a/packages/react/components/select/__tests__/select.test.tsx +++ b/packages/react/components/select/__tests__/select.test.tsx @@ -590,6 +590,28 @@ describe('Select', () => { } }); + it('passes an item to a single-mode children function', () => { + const renderValue = vi.fn((_value?: unknown) => null); + render( + + ); + expect(renderValue).toHaveBeenCalled(); + for (const [arg] of renderValue.mock.calls) { + expect(arg).toBeDefined(); + } + }); + it('shows one preset multiple value as text', () => { const { container } = render( From 4a25ea822377f31abf67d74a5de90b2755d98ccd Mon Sep 17 00:00:00 2001 From: Shreyag02 Date: Mon, 16 Nov 2026 11:34:24 +0530 Subject: [PATCH 4/5] fix: [select] say when Select.Value children get an array With multiple set, Select.Value passes a single item when one value is selected and an array only for two or more. --- apps/www/src/content/docs/components/select/props.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/www/src/content/docs/components/select/props.ts b/apps/www/src/content/docs/components/select/props.ts index 6b65f703d..7a05f2c77 100644 --- a/apps/www/src/content/docs/components/select/props.ts +++ b/apps/www/src/content/docs/components/select/props.ts @@ -107,7 +107,7 @@ export interface SelectValueProps { /** * Content shown in place of the selected item's label. - * A function receives the selected item, or an array of items when `multiple` is set. + * A function receives the selected item. With `multiple`, it receives an array when two or more items are selected. * A value with no matching item is passed with its raw `value` and no `leadingIcon`. * * @remarks `ReactNode | function` From 8f34db764c2fc6f5d25f2c32ea73e20af4d768a8 Mon Sep 17 00:00:00 2001 From: Shreyag02 Date: Thu, 19 Nov 2026 20:58:09 +0530 Subject: [PATCH 5/5] refactor: [select] memoize getItem in Select.Value --- packages/react/components/select/select-value.tsx | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/react/components/select/select-value.tsx b/packages/react/components/select/select-value.tsx index 6ed2c070e..27ab4a9f3 100644 --- a/packages/react/components/select/select-value.tsx +++ b/packages/react/components/select/select-value.tsx @@ -1,7 +1,7 @@ 'use client'; import { cx } from 'class-variance-authority'; -import { ComponentProps, ReactNode, useMemo } from 'react'; +import { ComponentProps, ReactNode, useCallback, useMemo } from 'react'; import styles from './select.module.css'; import { SelectMultipleValue } from './select-multiple-value'; import { useSelectContext } from './select-root'; @@ -27,19 +27,21 @@ export function SelectValue({ ? Array.isArray(value) && value.length > 0 : !!value; + const getItem = useCallback( + (v: string): ItemType => + items[v]?.value === v ? items[v] : { value: v, children: v }, + [items] + ); + const item = useMemo(() => { if (!value) return undefined; - // Items register in a layout effect, so a value may have no item yet or none at all. - // Comparing `value` also skips Object.prototype keys such as `constructor`. - const getItem = (v: string): ItemType => - items[v]?.value === v ? items[v] : { value: v, children: v }; if (multiple && Array.isArray(value)) { const itemValues = value.map(getItem); if (itemValues.length === 1) return itemValues[0]; return itemValues; } return getItem(value as string); - }, [value, items, multiple]); + }, [value, multiple, getItem]); if (!hasValue) { return (