-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Menu): remove parent context flags with new hook (#18812)
* feat(Menu): remove parent context flags with new hook Removes the `hasMenuContext` and `hasMenuListContext` flags in `Menu` contexts and replaces them with a new `useHasParentContext` hook in `react-context-selectors` * Change files * fix docs * revert web-components * update README
- Loading branch information
Showing
16 changed files
with
112 additions
and
27 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-context-selector-2cb4962b-d3d6-4f87-b2a9-4e25b1b03d8c.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(useHasParentContext): new hook to determine if a context provider exists as parent", | ||
"packageName": "@fluentui/react-context-selector", | ||
"email": "lingfan.gao@microsoft.com", | ||
"dependentChangeType": "patch" | ||
} |
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-menu-efe68e60-8877-4b34-8414-8686209e89b1.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(Menu): remove parent context flags with new hook", | ||
"packageName": "@fluentui/react-menu", | ||
"email": "lingfan.gao@microsoft.com", | ||
"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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { createContext } from './createContext'; | ||
export { useContextSelector } from './useContextSelector'; | ||
export { useHasParentContext } from './useHasParentContext'; | ||
export * from './types'; |
25 changes: 25 additions & 0 deletions
25
packages/react-context-selector/src/useHasParentContext.test.tsx
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,25 @@ | ||
import * as React from 'react'; | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { createContext } from './createContext'; | ||
import { useHasParentContext } from './useHasParentContext'; | ||
|
||
const TestContext = createContext<number>(1); | ||
|
||
describe('useHasParentContext', () => { | ||
it('should return true if wrapped with context', () => { | ||
// Arrange | ||
const wrapper: React.FC = ({ children }) => <TestContext.Provider value={1}>{children}</TestContext.Provider>; | ||
const { result } = renderHook(() => useHasParentContext(TestContext), { wrapper }); | ||
|
||
// Assert | ||
expect(result.current).toBe(true); | ||
}); | ||
|
||
it('should return true if not wrapped with context', () => { | ||
// Arrange | ||
const { result } = renderHook(() => useHasParentContext(TestContext)); | ||
|
||
// Assert | ||
expect(result.current).toBe(false); | ||
}); | ||
}); |
19 changes: 19 additions & 0 deletions
19
packages/react-context-selector/src/useHasParentContext.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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import * as React from 'react'; | ||
import { Context, ContextValue } from './types'; | ||
|
||
/** | ||
* Utility hook for contexts created by react-context-selector to determine if a parent context exists | ||
* WARNING: This hook will not work for native React contexts | ||
* | ||
* @param context - context created by react-context-selector | ||
* @returns whether the hook is wrapped by a parent context | ||
*/ | ||
export function useHasParentContext<Value>(context: Context<Value>) { | ||
const contextValue = React.useContext((context as unknown) as Context<ContextValue<Value>>); | ||
|
||
if (contextValue.version) { | ||
return contextValue.version.current !== -1; | ||
} | ||
|
||
return false; | ||
} |
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
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
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