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
2 changes: 2 additions & 0 deletions apps/www/src/content/docs/components/select/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<auto-type-table path="./props.ts" name="SelectValueProps" />

### Content
Expand Down
3 changes: 2 additions & 1 deletion apps/www/src/content/docs/components/select/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
*/
Expand Down
107 changes: 107 additions & 0 deletions packages/react/components/select/__tests__/select.test.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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(<BasicSelect multiple {...props} />);
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(
<BasicSelect multiple autocomplete defaultValue={['apple', 'banana']} />
);
expect(getValue(container)).toHaveTextContent('Apple');
});

it('shows the raw value for a value with no matching item', () => {
const { container } = render(
<BasicSelect multiple defaultValue={['zzz', 'apple']} />
);
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(
<BasicSelect multiple defaultValue={['constructor', 'apple']} />
);
const chips = container.querySelectorAll('[data-slot="chip"]');
expect(chips[0]).toHaveTextContent('constructor');
});

it('renders preset multiple values on the server', () => {
const html = renderToString(
<BasicSelect multiple defaultValue={['apple', 'banana']} />
);
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(
<Select multiple defaultValue={['apple', 'banana']}>
<Select.Trigger>
<Select.Value>
{
// SelectValueProps intersects span children with the function type.
renderValue as unknown as ReactNode
}
</Select.Value>
</Select.Trigger>
<Select.Content>
{FRUIT_OPTIONS.map(option => (
<Select.Item key={option.value} value={option.value}>
{option.label}
</Select.Item>
))}
</Select.Content>
</Select>
);
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(
<Select defaultValue='apple'>
<Select.Trigger>
<Select.Value>{renderValue as unknown as ReactNode}</Select.Value>
</Select.Trigger>
<Select.Content>
{FRUIT_OPTIONS.map(option => (
<Select.Item key={option.value} value={option.value}>
{option.label}
</Select.Item>
))}
</Select.Content>
</Select>
);
expect(renderValue).toHaveBeenCalled();
for (const [arg] of renderValue.mock.calls) {
expect(arg).toBeDefined();
}
});

it('shows one preset multiple value as text', () => {
const { container } = render(
<BasicSelect multiple defaultValue={['apple']} />
);
expect(getValue(container)).toHaveTextContent('Apple');
expect(container.querySelector('[data-slot="chip"]')).toBeNull();
});
});
});
14 changes: 10 additions & 4 deletions packages/react/components/select/select-value.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 (
Expand Down
Loading