-
Notifications
You must be signed in to change notification settings - Fork 938
fix(useElementIds): prevent Webpack static analysis issue with useId #1676
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 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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 |
|---|---|---|
| @@ -1,53 +1,60 @@ | ||
| import * as React from 'react' | ||
|
|
||
| import {generateId} from '../../../utils-ts' | ||
| import {UseTagGroupProps} from '../index.types' | ||
|
|
||
| export type UseElementIdsProps = Pick< | ||
| UseTagGroupProps<unknown>, | ||
| 'id' | 'getTagId' | 'tagGroupId' | ||
| > | ||
| type UseElementIdsProps = { | ||
| id?: string | ||
| tagGroupId?: string | ||
| getTagId?: (index: number) => string | ||
| } | ||
|
|
||
| type UseElementIdsReturnValue = { | ||
| tagGroupId: string | ||
| getTagId: (index: number) => string | ||
| } | ||
|
|
||
| export type UseElementIdsReturnValue = Required< | ||
| Pick<UseTagGroupProps<unknown>, 'getTagId' | 'tagGroupId'> | ||
| > | ||
| // eslint-disable-next-line @typescript-eslint/dot-notation | ||
| const reactUseId = React['useId'] | ||
|
|
||
| // istanbul ignore next | ||
| export const useElementIds: ( | ||
| props: UseElementIdsProps, | ||
| ) => UseElementIdsReturnValue = | ||
| 'useId' in React // Avoid conditional useId call | ||
| ? useElementIdsR18 | ||
| : useElementIdsLegacy | ||
| typeof reactUseId === 'function' ? useElementIdsR18 : useElementIdsLegacy | ||
|
|
||
| function useElementIdsR18({ | ||
| id, | ||
| tagGroupId, | ||
| getTagId, | ||
| }: UseElementIdsProps): UseElementIdsReturnValue { | ||
| // Avoid conditional useId call | ||
| const reactId = `downshift-${React.useId()}` | ||
| const reactId = `downshift-${reactUseId()}` | ||
| if (!id) { | ||
| id = reactId | ||
| } | ||
|
|
||
| const elementIdsRef = React.useRef({ | ||
| tagGroupId: tagGroupId ?? `${id}-tag-group`, | ||
| getTagId: getTagId ?? (index => `${id}-tag-${index}`), | ||
| }) | ||
| const elementIds = React.useMemo( | ||
| () => ({ | ||
| tagGroupId: tagGroupId ?? `${id}-tag-group`, | ||
| getTagId: getTagId ?? (index => `${id}-tag-${index}`), | ||
| }), | ||
| [getTagId, id, tagGroupId], | ||
| ) | ||
|
|
||
| return elementIdsRef.current | ||
| return elementIds | ||
| } | ||
|
|
||
| function useElementIdsLegacy({ | ||
| id = `downshift-${generateId()}`, | ||
| getTagId, | ||
| tagGroupId, | ||
| }: UseElementIdsProps): UseElementIdsReturnValue { | ||
| const elementIdsRef = React.useRef({ | ||
| tagGroupId: tagGroupId ?? `${id}-tag-group`, | ||
| getTagId: getTagId ?? (index => `${id}-tag-${index}`), | ||
| }) | ||
|
|
||
| return elementIdsRef.current | ||
| const elementIds = React.useMemo( | ||
| () => ({ | ||
| tagGroupId: tagGroupId ?? `${id}-tag-group`, | ||
| getTagId: getTagId ?? (index => `${id}-tag-${index}`), | ||
| }), | ||
| [getTagId, id, tagGroupId], | ||
| ) | ||
silviuaavram marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return elementIds | ||
| } | ||
5 changes: 3 additions & 2 deletions
5
...s/__tests__/utils.use-element-ids.test.js → ...wn/__tests__/useElementIds.legacy.test.ts
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
3 changes: 2 additions & 1 deletion
3
...tests__/utils.use-element-ids.r18.test.js → ...pdown/__tests__/useElementIds.r18.test.ts
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,54 @@ | ||
| import {renderHook} from '@testing-library/react' | ||
|
|
||
| import {useElementIds} from '../useElementIds' | ||
|
|
||
| describe('useElementIds', () => { | ||
| test('returns the same reference on re-renders when the props do not change', () => { | ||
| const getTestItemId = () => 'test-item-id' | ||
| const {result, rerender} = renderHook(useElementIds, { | ||
| initialProps: { | ||
| id: 'test-id', | ||
| labelId: 'test-label-id', | ||
| menuId: 'test-menu-id', | ||
| getItemId: getTestItemId, | ||
| toggleButtonId: 'test-toggle-button-id', | ||
| inputId: 'test-input-id', | ||
| }, | ||
| }) | ||
| const renderOneResult = result.current | ||
| rerender({ | ||
| id: 'test-id', | ||
| labelId: 'test-label-id', | ||
| menuId: 'test-menu-id', | ||
| getItemId: getTestItemId, | ||
| toggleButtonId: 'test-toggle-button-id', | ||
| inputId: 'test-input-id', | ||
| }) | ||
| const renderTwoResult = result.current | ||
| expect(renderOneResult).toBe(renderTwoResult) | ||
| }) | ||
|
|
||
| test('returns a new reference on re-renders when the props change', () => { | ||
| const {result, rerender} = renderHook(useElementIds, { | ||
| initialProps: { | ||
| id: 'test-id', | ||
| labelId: 'test-label-id', | ||
| menuId: 'test-menu-id', | ||
| getItemId: () => 'test-item-id', | ||
| toggleButtonId: 'test-toggle-button-id', | ||
| inputId: 'test-input-id', | ||
| }, | ||
| }) | ||
| const renderOneResult = result.current | ||
| rerender({ | ||
| id: 'test-id', | ||
| labelId: 'test-label-id', | ||
| menuId: 'test-menu-id', | ||
| getItemId: () => 'test-item-id', | ||
| toggleButtonId: 'test-toggle-button-id', | ||
| inputId: 'test-input-id', | ||
| }) | ||
| const renderTwoResult = result.current | ||
| expect(renderOneResult).not.toBe(renderTwoResult) | ||
| }) | ||
| }) |
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,75 @@ | ||
| import * as React from 'react' | ||
|
|
||
| import {generateId} from '../../utils-ts' | ||
|
|
||
| type UseElementIdsProps = { | ||
| id?: string | ||
| labelId?: string | ||
| menuId?: string | ||
| getItemId?: (index: number) => string | ||
| toggleButtonId?: string | ||
| inputId?: string | ||
| } | ||
|
|
||
| type UseElementIdsReturnValue = { | ||
| labelId: string | ||
| menuId: string | ||
| getItemId: (index: number) => string | ||
| toggleButtonId: string | ||
| inputId: string | ||
| } | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/dot-notation | ||
| const reactUseId = React['useId'] | ||
|
|
||
| export const useElementIds = | ||
| typeof reactUseId === 'function' ? useElementIdsR18 : useElementIdsLegacy | ||
|
|
||
| function useElementIdsR18({ | ||
| id, | ||
| labelId, | ||
| menuId, | ||
| getItemId, | ||
| toggleButtonId, | ||
| inputId, | ||
| }: UseElementIdsProps): UseElementIdsReturnValue { | ||
| const reactId = `downshift-${reactUseId()}` | ||
| if (!id) { | ||
| id = reactId | ||
| } | ||
|
|
||
| const elementIds = React.useMemo( | ||
| () => ({ | ||
| labelId: labelId ?? `${id}-label`, | ||
| menuId: menuId ?? `${id}-menu`, | ||
| getItemId: getItemId ?? (index => `${id}-item-${index}`), | ||
| toggleButtonId: toggleButtonId ?? `${id}-toggle-button`, | ||
| inputId: inputId ?? `${id}-input`, | ||
| }), | ||
| [getItemId, id, inputId, labelId, menuId, toggleButtonId], | ||
| ) | ||
|
|
||
| return elementIds | ||
| } | ||
|
|
||
| function useElementIdsLegacy({ | ||
| id = `downshift-${generateId()}`, | ||
| labelId, | ||
| menuId, | ||
| getItemId, | ||
| toggleButtonId, | ||
| inputId, | ||
| }: UseElementIdsProps): UseElementIdsReturnValue { | ||
| const elementIds = React.useMemo( | ||
| () => ({ | ||
| labelId: labelId ?? `${id}-label`, | ||
| menuId: menuId ?? `${id}-menu`, | ||
| getItemId: getItemId ?? (index => `${id}-item-${index}`), | ||
| toggleButtonId: toggleButtonId ?? `${id}-toggle-button`, | ||
| inputId: inputId ?? `${id}-input`, | ||
| }), | ||
| [getItemId, id, inputId, labelId, menuId, toggleButtonId], | ||
silviuaavram marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) | ||
|
|
||
| return elementIds | ||
| } | ||
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.