Skip to content

Commit d941b5f

Browse files
committed
refactor(mosaic): type onValueChange of choice components by their values
1 parent d75b38c commit d941b5f

12 files changed

Lines changed: 224 additions & 50 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { describe, expectTypeOf, test } from 'vitest';
2+
3+
import { Radio } from './radio';
4+
5+
declare const access: 'all' | 'selected';
6+
7+
describe('Radio.Group onValueChange', () => {
8+
test('gives a string when the group has no value', () => {
9+
void (
10+
<Radio.Group
11+
onValueChange={value => {
12+
expectTypeOf(value).toEqualTypeOf<string>();
13+
}}
14+
/>
15+
);
16+
});
17+
18+
test('gives the type of value', () => {
19+
void (
20+
<Radio.Group
21+
value={access}
22+
onValueChange={value => {
23+
expectTypeOf(value).toEqualTypeOf<'all' | 'selected'>();
24+
}}
25+
/>
26+
);
27+
});
28+
29+
test('gives the type of defaultValue', () => {
30+
void (
31+
<Radio.Group
32+
defaultValue={access}
33+
onValueChange={value => {
34+
expectTypeOf(value).toEqualTypeOf<'all' | 'selected'>();
35+
}}
36+
/>
37+
);
38+
});
39+
40+
test('rejects a value the handler does not accept', () => {
41+
void (
42+
<Radio.Group
43+
// @ts-expect-error -- The handler does not accept this value
44+
value='other'
45+
onValueChange={(value: 'all' | 'selected') => value}
46+
/>
47+
);
48+
});
49+
});

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ 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
13+
export interface RadioGroupProps<T extends string = string>
1414
extends Omit<React.ComponentPropsWithoutRef<'div'>, 'defaultValue' | 'className' | 'style'>, MosaicStyleProps {
15-
value?: string;
16-
defaultValue?: string;
17-
onValueChange?: (value: string) => void;
15+
value?: T;
16+
defaultValue?: T;
17+
onValueChange?: (value: T) => void;
1818
name?: string;
1919
disabled?: boolean;
2020
}
@@ -29,6 +29,7 @@ interface RadioGroupContextValue {
2929

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

32+
// 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.
3233
const RadioGroup = React.forwardRef<HTMLDivElement, RadioGroupProps>(function MosaicRadioGroup(
3334
{ value: valueProp, defaultValue, onValueChange, name: nameProp, disabled, xstyle, ...rest },
3435
ref,
@@ -54,7 +55,9 @@ const RadioGroup = React.forwardRef<HTMLDivElement, RadioGroupProps>(function Mo
5455
/>
5556
</RadioGroupContext.Provider>
5657
);
57-
});
58+
}) as <T extends string = string>(
59+
props: RadioGroupProps<T> & { ref?: React.Ref<HTMLDivElement> },
60+
) => React.ReactElement;
5861

5962
export interface RadioItemProps
6063
extends
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { describe, expectTypeOf, test } from 'vitest';
2+
3+
import { SegmentedControl } from './segmented-control';
4+
5+
declare const access: 'all' | 'selected';
6+
7+
describe('SegmentedControl.Root onValueChange', () => {
8+
test('gives a string when the group has no value', () => {
9+
void (
10+
<SegmentedControl.Root
11+
onValueChange={value => {
12+
expectTypeOf(value).toEqualTypeOf<string>();
13+
}}
14+
/>
15+
);
16+
});
17+
18+
test('gives the type of value', () => {
19+
void (
20+
<SegmentedControl.Root
21+
value={access}
22+
onValueChange={value => {
23+
expectTypeOf(value).toEqualTypeOf<'all' | 'selected'>();
24+
}}
25+
/>
26+
);
27+
});
28+
29+
test('gives the type of defaultValue', () => {
30+
void (
31+
<SegmentedControl.Root
32+
defaultValue={access}
33+
onValueChange={value => {
34+
expectTypeOf(value).toEqualTypeOf<'all' | 'selected'>();
35+
}}
36+
/>
37+
);
38+
});
39+
40+
test('rejects a value the handler does not accept', () => {
41+
void (
42+
<SegmentedControl.Root
43+
// @ts-expect-error -- The handler does not accept this value
44+
value='other'
45+
onValueChange={(value: 'all' | 'selected') => value}
46+
/>
47+
);
48+
});
49+
});

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

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

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

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

20+
// 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.
2021
const Root = React.forwardRef<HTMLDivElement, SegmentedControlRootProps>(function MosaicSegmentedControlRoot(
2122
{ xstyle, children, ...rest },
2223
ref,
@@ -32,7 +33,9 @@ const Root = React.forwardRef<HTMLDivElement, SegmentedControlRootProps>(functio
3233
{children}
3334
</Primitive.Root>
3435
);
35-
});
36+
}) as <T extends string = string>(
37+
props: SegmentedControlRootProps<T> & { ref?: React.Ref<HTMLDivElement> },
38+
) => React.ReactElement;
3639

3740
const Item = React.forwardRef<HTMLButtonElement, SegmentedControlItemProps>(function MosaicSegmentedControlItem(
3841
{ xstyle, children, ...rest },
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { describe, expectTypeOf, test } from 'vitest';
2+
3+
import { Select } from './select';
4+
5+
declare const label: string;
6+
7+
const expirations = [
8+
{ value: '1d', label: '1 day' },
9+
{ value: '7d', label: '7 days' },
10+
] as const;
11+
12+
describe('Select.Root onValueChange', () => {
13+
test('gives a string when the items carry strings', () => {
14+
void (
15+
<Select.Root
16+
items={[{ value: label, label }]}
17+
onValueChange={value => {
18+
expectTypeOf(value).toEqualTypeOf<string>();
19+
}}
20+
>
21+
{null}
22+
</Select.Root>
23+
);
24+
});
25+
26+
test('gives the type of the item values', () => {
27+
void (
28+
<Select.Root
29+
items={expirations.map(item => ({ ...item }))}
30+
onValueChange={value => {
31+
expectTypeOf(value).toEqualTypeOf<'1d' | '7d'>();
32+
}}
33+
>
34+
{null}
35+
</Select.Root>
36+
);
37+
});
38+
39+
test('gives an array of the item values when multiple', () => {
40+
void (
41+
<Select.Root
42+
multiple
43+
items={expirations.map(item => ({ ...item }))}
44+
onValueChange={value => {
45+
expectTypeOf(value).toEqualTypeOf<Array<'1d' | '7d'>>();
46+
}}
47+
>
48+
{null}
49+
</Select.Root>
50+
);
51+
});
52+
53+
test('rejects a value that is not an item', () => {
54+
void (
55+
<Select.Root
56+
items={expirations.map(item => ({ ...item }))}
57+
// @ts-expect-error -- No item carries this value
58+
value='30d'
59+
>
60+
{null}
61+
</Select.Root>
62+
);
63+
});
64+
});

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import { scrollAreaRoot, scrollAreaViewport } from '../scroll-area';
2424
import { selectOptionScope } from './select.markers.stylex';
2525
import * as slots from './select.styles';
2626

27-
export interface SelectItem {
28-
value: string;
27+
export interface SelectItem<T extends string = string> {
28+
value: T;
2929
/** Shown in the row and in the trigger once selected; also drives typeahead. */
3030
label: string;
3131
/**
@@ -36,23 +36,28 @@ export interface SelectItem {
3636
disabled?: boolean;
3737
}
3838

39-
interface SelectItemsProps {
39+
interface SelectItemsProps<T extends string> {
4040
/**
4141
* Every choice. Required, unlike the primitive's: the options only mount while the listbox is
4242
* open, so this is what lets the closed trigger show the selected label. `Select.Popup`
4343
* renders them as `Select.Option`s when it is given no children.
4444
*/
45-
items: SelectItem[];
45+
items: SelectItem<T>[];
4646
}
4747

48-
export type SelectProps =
49-
| (Omit<PrimitiveSelectSingleProps, 'items'> & SelectItemsProps)
50-
| (Omit<PrimitiveSelectMultipleProps, 'items'> & SelectItemsProps);
48+
export type SelectProps<T extends string = string> =
49+
| (Omit<PrimitiveSelectSingleProps<T>, 'items'> & SelectItemsProps<T>)
50+
| (Omit<PrimitiveSelectMultipleProps<T>, 'items'> & SelectItemsProps<T>);
5151

5252
const ItemsContext = React.createContext<SelectItem[]>([]);
5353

5454
/** Holds the selection and the items; renders no element of its own. */
55-
export function SelectRoot({ items, alignItemWithTrigger, children, ...rest }: SelectProps): React.ReactElement {
55+
export function SelectRoot<T extends string = string>({
56+
items,
57+
alignItemWithTrigger,
58+
children,
59+
...rest
60+
}: SelectProps<T>): React.ReactElement {
5661
const described = items.some(item => item.description !== undefined);
5762

5863
return (

‎packages/mosaic/src/features/api-keys/create-api-key.dialog.tsx‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,7 @@ function CreateKeyStep(props: CreateAPIKeyDialogProps & { inputRef: Ref<HTMLInpu
129129
<Select.Root
130130
items={expirationValues.map(value => ({ value, label: m.expirationOptions[value] }))}
131131
value={expiration ?? undefined}
132-
onValueChange={value =>
133-
form.setValue('expiration', expirationValues.find(option => option === value) ?? null)
134-
}
132+
onValueChange={value => form.setValue('expiration', value)}
135133
>
136134
<Select.Trigger placeholder={m.expirationPlaceholder} />
137135
<Select.Popup />

‎packages/mosaic/src/features/oauth-consent/oauth-consent.view.tsx‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,7 @@ export function OAuthConsentView(props: OAuthConsentViewProps) {
7878
items={props.organizations}
7979
value={organizationId ?? undefined}
8080
alignItemWithTrigger={false}
81-
onValueChange={value => {
82-
if (typeof value === 'string') {
83-
props.onOrganizationChange(value);
84-
}
85-
}}
81+
onValueChange={props.onOrganizationChange}
8682
>
8783
<Select.Trigger />
8884
<OrganizationPopup organizations={props.organizations} />
@@ -279,13 +275,11 @@ function OrganizationAccess({
279275
aria-labelledby={labelId}
280276
value={selection.access}
281277
disabled={disabled}
282-
onValueChange={value => {
283-
if (value === 'selected') {
284-
selection.onAccessChange('selected');
285-
return;
278+
onValueChange={access => {
279+
selection.onAccessChange(access);
280+
if (access === 'all') {
281+
selection.onSelectedChange([]);
286282
}
287-
selection.onAccessChange('all');
288-
selection.onSelectedChange([]);
289283
}}
290284
>
291285
<Field.Root orientation='horizontal'>

‎packages/mosaic/src/primitives/segmented-control/segmented-control-root.tsx‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ import {
1212
type SegmentedControlDirection,
1313
} from './segmented-control-context';
1414

15-
export interface SegmentedControlRootProps extends Omit<ComponentProps<'div'>, 'defaultValue'> {
16-
value?: string;
17-
defaultValue?: string;
18-
onValueChange?: (value: string) => void;
15+
export interface SegmentedControlRootProps<T extends string = string> extends Omit<
16+
ComponentProps<'div'>,
17+
'defaultValue'
18+
> {
19+
value?: T;
20+
defaultValue?: T;
21+
onValueChange?: (value: T) => void;
1922
disabled?: boolean;
2023
}
2124

25+
// 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.
2226
export const SegmentedControlRoot = React.forwardRef<HTMLDivElement, SegmentedControlRootProps>(
2327
function SegmentedControlRoot(props, ref) {
2428
const { render, value: valueProp, defaultValue, onValueChange, disabled = false, children, ...otherProps } = props;
@@ -96,4 +100,6 @@ export const SegmentedControlRoot = React.forwardRef<HTMLDivElement, SegmentedCo
96100
</SegmentedControlContext.Provider>
97101
);
98102
},
99-
);
103+
) as <T extends string = string>(
104+
props: SegmentedControlRootProps<T> & { ref?: React.Ref<HTMLDivElement> },
105+
) => React.ReactElement;

0 commit comments

Comments
 (0)