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..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,8 @@ 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` */ diff --git a/packages/react/components/select/__tests__/select.test.tsx b/packages/react/components/select/__tests__/select.test.tsx index f5f6d1b00..d9062932d 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,109 @@ 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('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( + + ); + 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..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,15 +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; 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]; - }, [value, items, multiple]); + return getItem(value as string); + }, [value, multiple, getItem]); if (!hasValue) { return (