-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup! Feat(web-react): Introduce Navigation #DS-1524
- Loading branch information
Showing
5 changed files
with
152 additions
and
9 deletions.
There are no files selected for viewing
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
112 changes: 112 additions & 0 deletions
112
packages/web-react/src/components/Navigation/__tests__/useNavigationLinkProps.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 |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
import { SpiritNavigationLinkProps } from '../../../types'; | ||
import { useNavigationLinkProps } from '../useNavigationLinkProps'; | ||
|
||
describe('useNavigationLinkProps', () => { | ||
it('should return defaults props', () => { | ||
const props: SpiritNavigationLinkProps = { | ||
href: '/', | ||
target: '_blank', | ||
isSelected: false, | ||
isDisabled: false, | ||
ariaLabel: 'label', | ||
}; | ||
const { result } = renderHook(() => useNavigationLinkProps(props)); | ||
|
||
expect(result.current).toStrictEqual({ | ||
navigationLinkProps: { | ||
'aria-current': undefined, | ||
'aria-label': 'label', | ||
href: '/', | ||
rel: undefined, | ||
target: '_blank', | ||
}, | ||
}); | ||
}); | ||
|
||
it('should return defaults if element is different from anchor', () => { | ||
const props: SpiritNavigationLinkProps<'span'> = { | ||
elementType: 'span', | ||
href: '/', | ||
target: '_blank', | ||
isSelected: false, | ||
isDisabled: false, | ||
ariaLabel: 'label', | ||
}; | ||
const { result } = renderHook(() => useNavigationLinkProps(props)); | ||
|
||
expect(result.current).toStrictEqual({ | ||
navigationLinkProps: { | ||
'aria-current': undefined, | ||
'aria-label': 'label', | ||
href: undefined, | ||
rel: undefined, | ||
target: undefined, | ||
}, | ||
}); | ||
}); | ||
|
||
it('should return for isDisabled', () => { | ||
const props: SpiritNavigationLinkProps = { | ||
href: '/', | ||
target: '_blank', | ||
isSelected: false, | ||
isDisabled: true, | ||
ariaLabel: 'label', | ||
}; | ||
const { result } = renderHook(() => useNavigationLinkProps(props)); | ||
|
||
expect(result.current).toStrictEqual({ | ||
navigationLinkProps: { | ||
'aria-current': undefined, | ||
'aria-label': 'label', | ||
href: undefined, | ||
rel: undefined, | ||
target: undefined, | ||
}, | ||
}); | ||
}); | ||
|
||
it('should return for isSelected', () => { | ||
const props: SpiritNavigationLinkProps = { | ||
href: '/', | ||
target: '_blank', | ||
isSelected: true, | ||
isDisabled: false, | ||
ariaLabel: 'label', | ||
}; | ||
const { result } = renderHook(() => useNavigationLinkProps(props)); | ||
|
||
expect(result.current).toStrictEqual({ | ||
navigationLinkProps: { | ||
'aria-current': 'page', | ||
'aria-label': 'label', | ||
href: '/', | ||
rel: undefined, | ||
target: '_blank', | ||
}, | ||
}); | ||
}); | ||
|
||
it('should return for elementType', () => { | ||
const props: SpiritNavigationLinkProps<'div'> = { | ||
elementType: 'div', | ||
href: '/', | ||
target: '_blank', | ||
isSelected: false, | ||
isDisabled: false, | ||
ariaLabel: 'label', | ||
}; | ||
const { result } = renderHook(() => useNavigationLinkProps(props)); | ||
|
||
expect(result.current).toStrictEqual({ | ||
navigationLinkProps: { | ||
'aria-current': undefined, | ||
'aria-label': 'label', | ||
href: undefined, | ||
rel: undefined, | ||
target: undefined, | ||
}, | ||
}); | ||
}); | ||
}); |
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
27 changes: 27 additions & 0 deletions
27
packages/web-react/src/components/Navigation/useNavigationLinkProps.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,27 @@ | ||
import { ElementType } from 'react'; | ||
import { SpiritNavigationLinkProps } from '../../types'; | ||
|
||
export type UseNavigationLinkProps<E extends ElementType = 'a'> = Partial<SpiritNavigationLinkProps<E>>; | ||
export type UseNavigationLinkReturn<E extends ElementType = 'a'> = { | ||
navigationLinkProps: UseNavigationLinkProps<E>; | ||
}; | ||
|
||
export const useNavigationLinkProps = <E extends ElementType = 'a'>( | ||
props: UseNavigationLinkProps<E>, | ||
): UseNavigationLinkReturn<E> => { | ||
const { elementType = 'a', isDisabled, isSelected, href, target, rel, ariaLabel } = props; | ||
|
||
const additionalProps: Partial<SpiritNavigationLinkProps> = { | ||
href: elementType === 'a' && !isDisabled ? href : undefined, | ||
target: elementType === 'a' && !isDisabled ? target : undefined, | ||
rel: elementType === 'a' ? rel : undefined, | ||
}; | ||
|
||
return { | ||
navigationLinkProps: { | ||
...additionalProps, | ||
'aria-label': ariaLabel || undefined, | ||
'aria-current': isSelected ? 'page' : undefined, | ||
}, | ||
}; | ||
}; |