Repository navigation
refactor(mosaic): type onValueChange of choice components by their values #10675
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a7bc5e0
refactor(mosaic): type onValueChange of choice components by their va…
alexcarpenter 8d46ddb
refactor(mosaic): make choice primitives value-agnostic, following Ba…
alexcarpenter 3524d3c
docs(swingset): update Select and SegmentedControl value types in sto…
alexcarpenter 18a2066
docs(mosaic): document SelectItem<Value> and the String(value) label …
alexcarpenter 6a20e6a
fix(mosaic): submit non-string Radio.Group values as JSON
alexcarpenter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| --- | ||
| --- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import { describe, expectTypeOf, test } from 'vitest'; | ||
|
|
||
| import { Radio } from './radio'; | ||
|
|
||
| type Access = 'all' | 'selected'; | ||
|
|
||
| declare const access: Access; | ||
| declare const onAccessChange: (access: Access) => void; | ||
|
|
||
| describe('Radio.Group onValueChange', () => { | ||
| test('takes the value type from a typed value', () => { | ||
| void ( | ||
| <Radio.Group | ||
| value={access} | ||
| onValueChange={value => { | ||
| expectTypeOf(value).toEqualTypeOf<Access>(); | ||
| }} | ||
| /> | ||
| ); | ||
| }); | ||
|
|
||
| test('gives a string when defaultValue is a literal', () => { | ||
| void ( | ||
| <Radio.Group | ||
| defaultValue='all' | ||
| onValueChange={value => { | ||
| expectTypeOf(value).toEqualTypeOf<string>(); | ||
| }} | ||
| /> | ||
| ); | ||
| }); | ||
|
|
||
| test('gives a string when nothing is typed', () => { | ||
| void ( | ||
| <Radio.Group | ||
| onValueChange={value => { | ||
| expectTypeOf(value).toEqualTypeOf<string>(); | ||
| }} | ||
| /> | ||
| ); | ||
| }); | ||
|
|
||
| test('takes the value type from a typed handler', () => { | ||
| void ( | ||
| <Radio.Group | ||
| defaultValue='all' | ||
| onValueChange={(value: Access) => { | ||
| expectTypeOf(value).toEqualTypeOf<Access>(); | ||
| }} | ||
| /> | ||
| ); | ||
| }); | ||
|
|
||
| test('accepts values that are not strings', () => { | ||
| void ( | ||
| <Radio.Group | ||
| defaultValue={1} | ||
| onValueChange={value => { | ||
| expectTypeOf(value).toEqualTypeOf<number>(); | ||
| }} | ||
| > | ||
| <Radio.Item value={1} /> | ||
| </Radio.Group> | ||
| ); | ||
| }); | ||
|
|
||
| test('rejects a value the handler does not accept', () => { | ||
| void ( | ||
| <> | ||
| <Radio.Group | ||
| // @ts-expect-error -- The handler does not accept this value | ||
| value='other' | ||
| onValueChange={onAccessChange} | ||
| /> | ||
| <Radio.Group | ||
| // @ts-expect-error -- The handler does not accept this value | ||
| defaultValue='other' | ||
| onValueChange={onAccessChange} | ||
| /> | ||
| </> | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
packages/mosaic/src/components/segmented-control/segmented-control.test-d.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import { describe, expectTypeOf, test } from 'vitest'; | ||
|
|
||
| import { SegmentedControl } from './segmented-control'; | ||
|
|
||
| type Access = 'all' | 'selected'; | ||
|
|
||
| declare const access: Access; | ||
| declare const onAccessChange: (access: Access) => void; | ||
|
|
||
| describe('SegmentedControl.Root onValueChange', () => { | ||
| test('takes the value type from a typed value', () => { | ||
| void ( | ||
| <SegmentedControl.Root | ||
| value={access} | ||
| onValueChange={value => { | ||
| expectTypeOf(value).toEqualTypeOf<Access>(); | ||
| }} | ||
| /> | ||
| ); | ||
| }); | ||
|
|
||
| test('gives a string when defaultValue is a literal', () => { | ||
| void ( | ||
| <SegmentedControl.Root | ||
| defaultValue='all' | ||
| onValueChange={value => { | ||
| expectTypeOf(value).toEqualTypeOf<string>(); | ||
| }} | ||
| /> | ||
| ); | ||
| }); | ||
|
|
||
| test('gives a string when nothing is typed', () => { | ||
| void ( | ||
| <SegmentedControl.Root | ||
| onValueChange={value => { | ||
| expectTypeOf(value).toEqualTypeOf<string>(); | ||
| }} | ||
| /> | ||
| ); | ||
| }); | ||
|
|
||
| test('takes the value type from a typed handler', () => { | ||
| void ( | ||
| <SegmentedControl.Root | ||
| defaultValue='all' | ||
| onValueChange={(value: Access) => { | ||
| expectTypeOf(value).toEqualTypeOf<Access>(); | ||
| }} | ||
| /> | ||
| ); | ||
| }); | ||
|
|
||
| test('accepts values that are not strings', () => { | ||
| void ( | ||
| <SegmentedControl.Root | ||
| defaultValue={1} | ||
| onValueChange={value => { | ||
| expectTypeOf(value).toEqualTypeOf<number>(); | ||
| }} | ||
| > | ||
| <SegmentedControl.Item value={1} /> | ||
| </SegmentedControl.Root> | ||
| ); | ||
| }); | ||
|
|
||
| test('rejects a value the handler does not accept', () => { | ||
| void ( | ||
| <> | ||
| <SegmentedControl.Root | ||
| // @ts-expect-error -- The handler does not accept this value | ||
| value='other' | ||
| onValueChange={onAccessChange} | ||
| /> | ||
| <SegmentedControl.Root | ||
| // @ts-expect-error -- The handler does not accept this value | ||
| defaultValue='other' | ||
| onValueChange={onAccessChange} | ||
| /> | ||
| </> | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.