Skip to content

Commit 249a846

Browse files
committed
refactor(mosaic): make choice primitives value-agnostic, following Base UI
1 parent d941b5f commit 249a846

24 files changed

Lines changed: 322 additions & 188 deletions

‎packages/mosaic/src/components/pagination/pagination.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export const Pagination = React.forwardRef<HTMLElement, PaginationProps>(functio
8989
const sizes = new Set(
9090
[...pageSizeOptions, itemsPerPage].filter(size => Number.isFinite(size) && size >= 1).map(Math.floor),
9191
);
92-
return [...sizes].sort((a, b) => a - b).map(size => ({ value: String(size), label: String(size) }));
92+
return [...sizes].sort((a, b) => a - b).map(size => ({ value: size, label: String(size) }));
9393
}, [pageSizeOptions, itemsPerPage]);
9494
const isFirst = current <= 1;
9595
const isLast = current >= pageCount;
@@ -126,8 +126,8 @@ export const Pagination = React.forwardRef<HTMLElement, PaginationProps>(functio
126126
</span>
127127
<Select.Root
128128
items={pageSizeItems}
129-
value={String(itemsPerPage)}
130-
onValueChange={value => onPageSizeChange(Number(value))}
129+
value={itemsPerPage}
130+
onValueChange={onPageSizeChange}
131131
>
132132
<Select.Trigger
133133
aria-label={pageSizeLabel}

‎packages/mosaic/src/components/radio/radio.test-d.tsx‎

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,82 @@ import { describe, expectTypeOf, test } from 'vitest';
22

33
import { Radio } from './radio';
44

5-
declare const access: 'all' | 'selected';
5+
type Access = 'all' | 'selected';
6+
7+
declare const access: Access;
8+
declare const onAccessChange: (access: Access) => void;
69

710
describe('Radio.Group onValueChange', () => {
8-
test('gives a string when the group has no value', () => {
11+
test('takes the value type from a typed value', () => {
912
void (
1013
<Radio.Group
14+
value={access}
1115
onValueChange={value => {
12-
expectTypeOf(value).toEqualTypeOf<string>();
16+
expectTypeOf(value).toEqualTypeOf<Access>();
1317
}}
1418
/>
1519
);
1620
});
1721

18-
test('gives the type of value', () => {
22+
test('gives a string when defaultValue is a literal', () => {
1923
void (
2024
<Radio.Group
21-
value={access}
25+
defaultValue='all'
2226
onValueChange={value => {
23-
expectTypeOf(value).toEqualTypeOf<'all' | 'selected'>();
27+
expectTypeOf(value).toEqualTypeOf<string>();
2428
}}
2529
/>
2630
);
2731
});
2832

29-
test('gives the type of defaultValue', () => {
33+
test('gives a string when nothing is typed', () => {
3034
void (
3135
<Radio.Group
32-
defaultValue={access}
3336
onValueChange={value => {
34-
expectTypeOf(value).toEqualTypeOf<'all' | 'selected'>();
37+
expectTypeOf(value).toEqualTypeOf<string>();
3538
}}
3639
/>
3740
);
3841
});
3942

40-
test('rejects a value the handler does not accept', () => {
43+
test('takes the value type from a typed handler', () => {
4144
void (
4245
<Radio.Group
43-
// @ts-expect-error -- The handler does not accept this value
44-
value='other'
45-
onValueChange={(value: 'all' | 'selected') => value}
46+
defaultValue='all'
47+
onValueChange={(value: Access) => {
48+
expectTypeOf(value).toEqualTypeOf<Access>();
49+
}}
4650
/>
4751
);
4852
});
53+
54+
test('accepts values that are not strings', () => {
55+
void (
56+
<Radio.Group
57+
defaultValue={1}
58+
onValueChange={value => {
59+
expectTypeOf(value).toEqualTypeOf<number>();
60+
}}
61+
>
62+
<Radio.Item value={1} />
63+
</Radio.Group>
64+
);
65+
});
66+
67+
test('rejects a value the handler does not accept', () => {
68+
void (
69+
<>
70+
<Radio.Group
71+
// @ts-expect-error -- The handler does not accept this value
72+
value='other'
73+
onValueChange={onAccessChange}
74+
/>
75+
<Radio.Group
76+
// @ts-expect-error -- The handler does not accept this value
77+
defaultValue='other'
78+
onValueChange={onAccessChange}
79+
/>
80+
</>
81+
);
82+
});
4983
});

‎packages/mosaic/src/components/radio/radio.tsx‎

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,32 @@ import { useOptionalFieldContext, useOptionalFieldControlProps } from '../field/
1010
import { radioInputMarker } from './radio.markers.stylex';
1111
import { firstLine, indicatorSizes, sizes, styles } from './radio.styles';
1212

13-
export interface RadioGroupProps<T extends string = string>
13+
export interface RadioGroupProps<Value = string>
1414
extends Omit<React.ComponentPropsWithoutRef<'div'>, 'defaultValue' | 'className' | 'style'>, MosaicStyleProps {
15-
value?: T;
16-
defaultValue?: T;
17-
onValueChange?: (value: T) => void;
15+
value?: Value;
16+
defaultValue?: Value;
17+
onValueChange?: (value: Value) => void;
1818
name?: string;
1919
disabled?: boolean;
2020
}
2121

2222
interface RadioGroupContextValue {
2323
name: string;
2424
submits: boolean;
25-
value: string | undefined;
26-
setValue: (value: string) => void;
25+
value: unknown;
26+
setValue: (value: unknown) => void;
2727
disabled: boolean | undefined;
2828
}
2929

3030
const RadioGroupContext = React.createContext<RadioGroupContextValue | null>(null);
3131

3232
// eslint-disable-next-line typescript/consistent-type-assertions -- The ref wrapper drops the value type parameter; the group only hands back values its caller passed in.
33-
const RadioGroup = React.forwardRef<HTMLDivElement, RadioGroupProps>(function MosaicRadioGroup(
33+
const RadioGroup = React.forwardRef<HTMLDivElement, RadioGroupProps<unknown>>(function MosaicRadioGroup(
3434
{ value: valueProp, defaultValue, onValueChange, name: nameProp, disabled, xstyle, ...rest },
3535
ref,
3636
) {
3737
const generatedName = React.useId();
38-
const [value, setValue] = useControllableState<string | undefined>(valueProp, defaultValue, next => {
39-
if (next !== undefined) {
40-
onValueChange?.(next);
41-
}
42-
});
38+
const [value, setValue] = useControllableState(valueProp, defaultValue, onValueChange);
4339
const context = React.useMemo<RadioGroupContextValue>(
4440
() => ({ name: nameProp ?? generatedName, submits: nameProp !== undefined, value, setValue, disabled }),
4541
[nameProp, generatedName, value, setValue, disabled],
@@ -55,16 +51,14 @@ const RadioGroup = React.forwardRef<HTMLDivElement, RadioGroupProps>(function Mo
5551
/>
5652
</RadioGroupContext.Provider>
5753
);
58-
}) as <T extends string = string>(
59-
props: RadioGroupProps<T> & { ref?: React.Ref<HTMLDivElement> },
60-
) => React.ReactElement;
54+
}) as <Value = string>(props: RadioGroupProps<Value> & { ref?: React.Ref<HTMLDivElement> }) => React.ReactElement;
6155

6256
export interface RadioItemProps
6357
extends
6458
Omit<React.ComponentPropsWithoutRef<'input'>, 'type' | 'size' | 'value' | 'className' | 'style'>,
6559
MosaicStyleProps {
6660
size?: 'sm' | 'md';
67-
value?: string;
61+
value?: unknown;
6862
}
6963

7064
const RadioItem = React.forwardRef<HTMLInputElement, RadioItemProps>(function MosaicRadioItem(
@@ -94,7 +88,7 @@ const RadioItem = React.forwardRef<HTMLInputElement, RadioItemProps>(function Mo
9488
});
9589
const group = React.useContext(RadioGroupContext);
9690
const disabled = (fieldProps?.disabled ?? disabledProp) || group?.disabled;
97-
const checked = group && value !== undefined ? group.value === value : checkedProp;
91+
const checked = group && value !== undefined ? Object.is(group.value, value) : checkedProp;
9892

9993
return (
10094
<span
@@ -106,7 +100,7 @@ const RadioItem = React.forwardRef<HTMLInputElement, RadioItemProps>(function Mo
106100
<input
107101
ref={ref}
108102
type='radio'
109-
value={value}
103+
value={typeof value === 'string' ? value : undefined}
110104
name={group?.name ?? nameProp}
111105
form={group?.submits === false ? '' : undefined}
112106
checked={checked}

‎packages/mosaic/src/components/segmented-control/segmented-control.test-d.tsx‎

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,82 @@ import { describe, expectTypeOf, test } from 'vitest';
22

33
import { SegmentedControl } from './segmented-control';
44

5-
declare const access: 'all' | 'selected';
5+
type Access = 'all' | 'selected';
6+
7+
declare const access: Access;
8+
declare const onAccessChange: (access: Access) => void;
69

710
describe('SegmentedControl.Root onValueChange', () => {
8-
test('gives a string when the group has no value', () => {
11+
test('takes the value type from a typed value', () => {
912
void (
1013
<SegmentedControl.Root
14+
value={access}
1115
onValueChange={value => {
12-
expectTypeOf(value).toEqualTypeOf<string>();
16+
expectTypeOf(value).toEqualTypeOf<Access>();
1317
}}
1418
/>
1519
);
1620
});
1721

18-
test('gives the type of value', () => {
22+
test('gives a string when defaultValue is a literal', () => {
1923
void (
2024
<SegmentedControl.Root
21-
value={access}
25+
defaultValue='all'
2226
onValueChange={value => {
23-
expectTypeOf(value).toEqualTypeOf<'all' | 'selected'>();
27+
expectTypeOf(value).toEqualTypeOf<string>();
2428
}}
2529
/>
2630
);
2731
});
2832

29-
test('gives the type of defaultValue', () => {
33+
test('gives a string when nothing is typed', () => {
3034
void (
3135
<SegmentedControl.Root
32-
defaultValue={access}
3336
onValueChange={value => {
34-
expectTypeOf(value).toEqualTypeOf<'all' | 'selected'>();
37+
expectTypeOf(value).toEqualTypeOf<string>();
3538
}}
3639
/>
3740
);
3841
});
3942

40-
test('rejects a value the handler does not accept', () => {
43+
test('takes the value type from a typed handler', () => {
4144
void (
4245
<SegmentedControl.Root
43-
// @ts-expect-error -- The handler does not accept this value
44-
value='other'
45-
onValueChange={(value: 'all' | 'selected') => value}
46+
defaultValue='all'
47+
onValueChange={(value: Access) => {
48+
expectTypeOf(value).toEqualTypeOf<Access>();
49+
}}
4650
/>
4751
);
4852
});
53+
54+
test('accepts values that are not strings', () => {
55+
void (
56+
<SegmentedControl.Root
57+
defaultValue={1}
58+
onValueChange={value => {
59+
expectTypeOf(value).toEqualTypeOf<number>();
60+
}}
61+
>
62+
<SegmentedControl.Item value={1} />
63+
</SegmentedControl.Root>
64+
);
65+
});
66+
67+
test('rejects a value the handler does not accept', () => {
68+
void (
69+
<>
70+
<SegmentedControl.Root
71+
// @ts-expect-error -- The handler does not accept this value
72+
value='other'
73+
onValueChange={onAccessChange}
74+
/>
75+
<SegmentedControl.Root
76+
// @ts-expect-error -- The handler does not accept this value
77+
defaultValue='other'
78+
onValueChange={onAccessChange}
79+
/>
80+
</>
81+
);
82+
});
4983
});

‎packages/mosaic/src/components/segmented-control/segmented-control.tsx‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ import { styles } from './segmented-control.styles';
1414

1515
type StyledProps<Props> = Omit<Props, 'className' | 'style'> & MosaicStyleProps;
1616

17-
export type SegmentedControlRootProps<T extends string = string> = StyledProps<HeadlessSegmentedControlRootProps<T>>;
17+
export type SegmentedControlRootProps<Value = string> = StyledProps<HeadlessSegmentedControlRootProps<Value>>;
1818
export type SegmentedControlItemProps = StyledProps<HeadlessSegmentedControlItemProps>;
1919

2020
// eslint-disable-next-line typescript/consistent-type-assertions -- The ref wrapper drops the value type parameter; the root only hands back values its caller passed in.
21-
const Root = React.forwardRef<HTMLDivElement, SegmentedControlRootProps>(function MosaicSegmentedControlRoot(
21+
const Root = React.forwardRef<HTMLDivElement, SegmentedControlRootProps<unknown>>(function MosaicSegmentedControlRoot(
2222
{ xstyle, children, ...rest },
2323
ref,
2424
) {
@@ -33,8 +33,8 @@ const Root = React.forwardRef<HTMLDivElement, SegmentedControlRootProps>(functio
3333
{children}
3434
</Primitive.Root>
3535
);
36-
}) as <T extends string = string>(
37-
props: SegmentedControlRootProps<T> & { ref?: React.Ref<HTMLDivElement> },
36+
}) as <Value = string>(
37+
props: SegmentedControlRootProps<Value> & { ref?: React.Ref<HTMLDivElement> },
3838
) => React.ReactElement;
3939

4040
const Item = React.forwardRef<HTMLButtonElement, SegmentedControlItemProps>(function MosaicSegmentedControlItem(

0 commit comments

Comments
 (0)