-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
feat(react-utilities): make dictionaries strict and get rid of unused generics to improve DX and type safety #23019
Draft
Hotell
wants to merge
22
commits into
microsoft:master
Choose a base branch
from
Hotell:hotell/ts/improve-react-utils-types
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
88df0e6
feat(react-utilities): make dicionaries strict and get rid of unused β¦
Hotell e32a270
chore: add eslint-plugin-etc and enable no-missused-generics rule to β¦
Hotell 223cc42
fixup! feat(react-utilities): make dicionaries strict and get rid of β¦
Hotell 1967330
docs(react-utilities): re-generate api
Hotell dc36c46
chore(react-utilities): fix build:local task via migration-generator
Hotell 5b3d376
generate change file
Hotell f88fed4
fixup! chore: add eslint-plugin-etc and enable no-missused-generics rβ¦
Hotell 2c4e6fc
fixup! fixup! feat(react-utilities): make dicionaries strict and get β¦
Hotell 6458f80
fix: explicitly assert components getNativeElemProps calls that are iβ¦
Hotell 1f7742b
fixup! docs(react-utilities): re-generate api
Hotell 3fe93d4
fixup! fixup! fixup! feat(react-utilities): make dicionaries strict aβ¦
Hotell 2ad53fc
fixup! fixup! fixup! fixup! feat(react-utilities): make dicionaries sβ¦
Hotell 4af9e8c
fixup! fixup! docs(react-utilities): re-generate api
Hotell 2439478
generate change files for updated packages
Hotell 4fdb516
test(react-utils): add more tests
Hotell ee3513e
feat: final fantasy ?
Hotell 359e8fa
docs: re-generate api
Hotell 2411870
fixup! feat: final fantasy ?
Hotell 10aae1f
fixup! fixup! feat: final fantasy ?
Hotell c644123
fixup! fixup! fixup! feat: final fantasy ?
Hotell 5aa2409
fixup! fixup! fixup! fixup! feat: final fantasy ?
Hotell c82afa3
fixup! docs: re-generate api
Hotell 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
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-button-0096137a-3751-4300-8655-523baee276bb.json
This file contains 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,7 @@ | ||
{ | ||
"type": "none", | ||
"comment": "fix: explicitly assert components getNativeElemProps calls that are incompatible with Slot api", | ||
"packageName": "@fluentui/react-button", | ||
"email": "[email protected]", | ||
"dependentChangeType": "none" | ||
} |
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-link-c03b6435-131b-4d57-afed-794040e1e29b.json
This file contains 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,7 @@ | ||
{ | ||
"type": "none", | ||
"comment": "fix: explicitly assert components getNativeElemProps calls that are incompatible with Slot api", | ||
"packageName": "@fluentui/react-link", | ||
"email": "[email protected]", | ||
"dependentChangeType": "none" | ||
} |
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-menu-4259442d-2ab3-4223-af79-2d2528545b9b.json
This file contains 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,7 @@ | ||
{ | ||
"type": "none", | ||
"comment": "fix: explicitly assert components getNativeElemProps calls that are incompatible with Slot api", | ||
"packageName": "@fluentui/react-menu", | ||
"email": "[email protected]", | ||
"dependentChangeType": "none" | ||
} |
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-text-400681a3-c296-408b-8cf7-4968f358a69a.json
This file contains 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,7 @@ | ||
{ | ||
"type": "none", | ||
"comment": "fix: explicitly assert components getNativeElemProps calls that are incompatible with Slot api", | ||
"packageName": "@fluentui/react-text", | ||
"email": "[email protected]", | ||
"dependentChangeType": "none" | ||
} |
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-utilities-93110975-e438-4f2f-b983-ff54c25c6dfa.json
This file contains 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,7 @@ | ||
{ | ||
"type": "prerelease", | ||
"comment": "feat: make dictionaries and getNativeElementProps strict and get rid of unused generics to improve DX and type safety", | ||
"packageName": "@fluentui/react-utilities", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch" | ||
} |
This file contains 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 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 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 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 |
---|---|---|
|
@@ -62,15 +62,15 @@ export const useMenuPopover_unstable = (props: MenuPopoverProps, ref: React.Ref< | |
|
||
const { onMouseEnter: onMouseEnterOriginal, onKeyDown: onKeyDownOriginal } = rootProps; | ||
|
||
rootProps.onMouseEnter = useEventCallback((e: React.MouseEvent<HTMLElement>) => { | ||
rootProps.onMouseEnter = useEventCallback((e: React.MouseEvent<HTMLDivElement>) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. because we now have strict type checking and inference, we need to provide correct types instead of wide types |
||
if (openOnHover) { | ||
setOpen(e, { open: true, keyboard: false }); | ||
} | ||
|
||
onMouseEnterOriginal?.(e); | ||
}); | ||
|
||
rootProps.onKeyDown = useEventCallback((e: React.KeyboardEvent<HTMLElement>) => { | ||
rootProps.onKeyDown = useEventCallback((e: React.KeyboardEvent<HTMLDivElement>) => { | ||
const key = e.key; | ||
|
||
if (key === Escape || (isSubmenu && key === CloseArrowKey)) { | ||
|
This file contains 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 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 |
---|---|---|
@@ -1,4 +1,8 @@ | ||
{ | ||
"extends": ["plugin:@fluentui/eslint-plugin/react"], | ||
"root": true | ||
"plugins": ["etc"], | ||
"root": true, | ||
"rules": { | ||
"etc/no-misused-generics": "warn" | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
packages/react-components/react-utilities/config/api-extractor.local.json
This file contains 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json", | ||
"extends": "./api-extractor.json", | ||
"mainEntryPointFilePath": "<projectFolder>/dist/packages/react-components/<unscopedPackageName>/src/index.d.ts" | ||
"mainEntryPointFilePath": "<projectFolder>/dist/types/packages/react-components/<unscopedPackageName>/src/index.d.ts" | ||
} |
62 changes: 31 additions & 31 deletions
62
packages/react-components/react-utilities/etc/react-utilities.api.md
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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
66 changes: 62 additions & 4 deletions
66
packages/react-components/react-utilities/src/utils/getNativeElementProps.test.ts
This file contains 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 |
---|---|---|
@@ -1,17 +1,75 @@ | ||
import { getNativeElementProps } from './getNativeElementProps'; | ||
import { getNativeElementProps, getPartitionedNativeProps } from './getNativeElementProps'; | ||
|
||
describe('getNativeElementProps', () => { | ||
it('can filter native element properties', () => { | ||
expect(getNativeElementProps('div', { id: '123', checked: true })).toEqual({ id: '123' }); | ||
const divWithInvalidProps = getNativeElementProps('div', { id: '123', checked: true }); | ||
expect(divWithInvalidProps).toEqual({ | ||
id: '123', | ||
}); | ||
// @ts-expect-error -- 'id' prop is not allowed `div` prop | ||
expect(divWithInvalidProps.checked).toBeUndefined(); | ||
|
||
expect(getNativeElementProps('input', { id: '123', checked: true })).toEqual({ id: '123', checked: true }); | ||
expect(getNativeElementProps('input', { id: '123', checked: true }, ['id'])).toEqual({ checked: true }); | ||
|
||
const inputWithExclude = getNativeElementProps('input', { id: '123', checked: true }, ['id']); | ||
expect(inputWithExclude).toEqual({ checked: true }); | ||
// @ts-expect-error -- 'id' prop was excluded | ||
expect(inputWithExclude.id).toBeUndefined(); | ||
}); | ||
|
||
it('includes `as` as a native prop', () => { | ||
expect(getNativeElementProps('div', { as: 'span' })).toEqual({ as: 'span' }); | ||
}); | ||
|
||
it('excludes props regardless of the allowed', () => { | ||
expect(getNativeElementProps('div', { as: 'span' }, ['as'])).toEqual({}); | ||
const actual = getNativeElementProps('div', { as: 'span' }, ['as']); | ||
|
||
// @ts-expect-error -- `as` prop was excluded | ||
expect(actual.as).toBeUndefined(); | ||
expect(actual).toEqual({}); | ||
}); | ||
}); | ||
|
||
describe('getPartitionedNativeProps', () => { | ||
it('creates modified root and primary and always removes className and styles prop from primary', () => { | ||
const actual = getPartitionedNativeProps({ | ||
primarySlotTagName: 'div', | ||
props: { | ||
className: 'hello', | ||
style: { width: '100px' }, | ||
id: '123', | ||
dir: 'ltr', | ||
defaultChecked: true, | ||
}, | ||
}); | ||
|
||
expect(actual.root).toEqual({ className: 'hello', style: { width: '100px' } }); | ||
expect(actual.primary).toEqual({ id: '123', dir: 'ltr' }); | ||
|
||
// This should throw Type error - `defaultChecked` is not valid DIV prop | ||
// Unfortunately react typings provide `React.HTMLAttributes` interface | ||
// which assign lot of HTML invalid props to every intrinsic element, thus making this invalid by default | ||
expect(actual.primary.defaultChecked).toBeUndefined(); | ||
|
||
// @ts-expect-error -- `className` prop was excluded | ||
expect(actual.primary.className).toBeUndefined(); | ||
// @ts-expect-error -- `style` prop was excluded | ||
expect(actual.primary.style).toBeUndefined(); | ||
}); | ||
|
||
it('works with excluded prop names for "primary"', () => { | ||
const actual = getPartitionedNativeProps({ | ||
primarySlotTagName: 'div', | ||
props: { id: '123', dir: 'ltr', defaultChecked: false }, | ||
excludedPropNames: ['id', 'defaultChecked'], | ||
}); | ||
|
||
expect(actual.primary).toEqual({ dir: 'ltr' }); | ||
|
||
expect(actual.primary.dir).toBe('ltr'); | ||
// @ts-expect-error -- `defaultChecked` prop was excluded | ||
expect(actual.primary.defaultChecked).toBeUndefined(); | ||
// @ts-expect-error -- `id `prop was excluded | ||
expect(actual.primary.id).toBeUndefined(); | ||
}); | ||
}); |
This file contains 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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because we now have proper getNativeProps types, elements that can have variadic root cannot match with original
Props
definition whereSlot
mapped type is used to define the api.Why?
Slot
api design is cannot match with implementation/runtime -Slot
uses binary arguments, where 1st generic can only be a 1st string. from component API perspective that's impossible to achieve as Slot is always 1 string or union of multiple strings -> we cannot extract first type from union nor transform unions to arrays.