-
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: Implement MenuItemLink components (#28369)
* feat: Implement MenuItemLink components Implements the MenuItemLink component which is similar to the MenuItem component with a different root slot and a required `href` prop. * add best practice * changefile * update snapshot * revert * Update packages/react-components/react-menu/src/components/MenuItemLink/useMenuItemLinkStyles.styles.ts Co-authored-by: Oleksandr Fediashov <[email protected]> * Update packages/react-components/react-menu/src/components/MenuItemLink/MenuItemLink.tsx Co-authored-by: Oleksandr Fediashov <[email protected]> * fix formatting --------- Co-authored-by: Oleksandr Fediashov <[email protected]>
- Loading branch information
1 parent
a542154
commit 0cbd837
Showing
19 changed files
with
345 additions
and
0 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
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-components-cb5ae280-68b7-4f48-aeda-fb3638d505eb.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": "minor", | ||
"comment": "feat: Implement MenuItemLink components", | ||
"packageName": "@fluentui/react-components", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch" | ||
} |
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-menu-b64fa5d2-460f-403c-b414-6b5732c52e91.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": "minor", | ||
"comment": "feat: Implement MenuItemLink components", | ||
"packageName": "@fluentui/react-menu", | ||
"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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './components/MenuItemLink/index'; |
31 changes: 31 additions & 0 deletions
31
packages/react-components/react-menu/src/components/MenuItemLink/MenuItemLink.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,31 @@ | ||
import * as React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import { MenuItemLink } from './MenuItemLink'; | ||
import { isConformant } from '../../testing/isConformant'; | ||
|
||
describe('MenuItemLink', () => { | ||
isConformant({ | ||
Component: MenuItemLink, | ||
displayName: 'MenuItemLink', | ||
testOptions: { | ||
'has-static-classnames': [ | ||
{ | ||
props: { | ||
icon: 'Test Icon', | ||
checkmark: 'Test Checkmark', | ||
submenuIndicator: 'Test Submenu Indicator', | ||
content: 'Test Content', | ||
secondaryContent: 'Test Secondary Content', | ||
}, | ||
}, | ||
], | ||
}, | ||
}); | ||
|
||
// TODO add more tests here, and create visual regression tests in /apps/vr-tests | ||
|
||
it('renders a default state', () => { | ||
const result = render(<MenuItemLink href="#">Default MenuItemLink</MenuItemLink>); | ||
expect(result.container).toMatchSnapshot(); | ||
}); | ||
}); |
18 changes: 18 additions & 0 deletions
18
packages/react-components/react-menu/src/components/MenuItemLink/MenuItemLink.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,18 @@ | ||
import * as React from 'react'; | ||
import { useMenuItemLink_unstable } from './useMenuItemLink'; | ||
import { renderMenuItemLink_unstable } from './renderMenuItemLink'; | ||
import { useMenuItemLinkStyles_unstable } from './useMenuItemLinkStyles.styles'; | ||
import type { MenuItemLinkProps } from './MenuItemLink.types'; | ||
import type { ForwardRefComponent } from '@fluentui/react-utilities'; | ||
|
||
/** | ||
* MenuItemLink component | ||
*/ | ||
export const MenuItemLink: ForwardRefComponent<MenuItemLinkProps> = React.forwardRef((props, ref) => { | ||
const state = useMenuItemLink_unstable(props, ref); | ||
|
||
useMenuItemLinkStyles_unstable(state); | ||
return renderMenuItemLink_unstable(state); | ||
}); | ||
|
||
MenuItemLink.displayName = 'MenuItemLink'; |
16 changes: 16 additions & 0 deletions
16
packages/react-components/react-menu/src/components/MenuItemLink/MenuItemLink.types.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,16 @@ | ||
import type { ComponentProps, ComponentState, Slot } from '@fluentui/react-utilities'; | ||
import { MenuItemProps, MenuItemSlots } from '../MenuItem/MenuItem.types'; | ||
|
||
export type MenuItemLinkSlots = { | ||
root: Slot<'a'>; | ||
} & Pick<MenuItemSlots, 'icon' | 'content' | 'secondaryContent' | 'checkmark'>; | ||
|
||
/** | ||
* MenuItemLink Props | ||
*/ | ||
export type MenuItemLinkProps = ComponentProps<MenuItemLinkSlots> & Pick<MenuItemProps, 'disabled'> & { href: string }; | ||
|
||
/** | ||
* State used in rendering MenuItemLink | ||
*/ | ||
export type MenuItemLinkState = ComponentState<MenuItemLinkSlots>; |
17 changes: 17 additions & 0 deletions
17
...omponents/react-menu/src/components/MenuItemLink/__snapshots__/MenuItemLink.test.tsx.snap
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,17 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`MenuItemLink renders a default state 1`] = ` | ||
<div> | ||
<a | ||
class="fui-MenuItemLink fui-MenuItem" | ||
href="#" | ||
role="menuitem" | ||
> | ||
<span | ||
class="fui-MenuItemLink__content fui-MenuItem__content" | ||
> | ||
Default MenuItemLink | ||
</span> | ||
</a> | ||
</div> | ||
`; |
5 changes: 5 additions & 0 deletions
5
packages/react-components/react-menu/src/components/MenuItemLink/index.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,5 @@ | ||
export * from './MenuItemLink'; | ||
export * from './MenuItemLink.types'; | ||
export * from './renderMenuItemLink'; | ||
export * from './useMenuItemLink'; | ||
export * from './useMenuItemLinkStyles.styles'; |
23 changes: 23 additions & 0 deletions
23
packages/react-components/react-menu/src/components/MenuItemLink/renderMenuItemLink.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,23 @@ | ||
/** @jsxRuntime classic */ | ||
/** @jsx createElement */ | ||
|
||
import { createElement } from '@fluentui/react-jsx-runtime'; | ||
import { getSlotsNext } from '@fluentui/react-utilities'; | ||
import type { MenuItemLinkState, MenuItemLinkSlots } from './MenuItemLink.types'; | ||
|
||
/** | ||
* Render the final JSX of MenuItemLink | ||
*/ | ||
export const renderMenuItemLink_unstable = (state: MenuItemLinkState) => { | ||
const { slots, slotProps } = getSlotsNext<MenuItemLinkSlots>(state); | ||
|
||
// TODO Add additional slots in the appropriate place | ||
return ( | ||
<slots.root {...slotProps.root}> | ||
{slots.checkmark && <slots.checkmark {...slotProps.checkmark} />} | ||
{slots.icon && <slots.icon {...slotProps.icon} />} | ||
{slots.content && <slots.content {...slotProps.content} />} | ||
{slots.secondaryContent && <slots.secondaryContent {...slotProps.secondaryContent} />} | ||
</slots.root> | ||
); | ||
}; |
34 changes: 34 additions & 0 deletions
34
packages/react-components/react-menu/src/components/MenuItemLink/useMenuItemLink.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,34 @@ | ||
import * as React from 'react'; | ||
import { getNativeElementProps } from '@fluentui/react-utilities'; | ||
import type { MenuItemLinkProps, MenuItemLinkState } from './MenuItemLink.types'; | ||
import { useMenuItem_unstable } from '../MenuItem/useMenuItem'; | ||
import { MenuItemProps } from '../MenuItem/MenuItem.types'; | ||
|
||
/** | ||
* Create the state required to render MenuItemLink. | ||
* | ||
* The returned state can be modified with hooks such as useMenuItemLinkStyles_unstable, | ||
* before being passed to renderMenuItemLink_unstable. | ||
* | ||
* @param props - props from this instance of MenuItemLink | ||
* @param ref - reference to root HTMLElement of MenuItemLink | ||
*/ | ||
export const useMenuItemLink_unstable = ( | ||
props: MenuItemLinkProps, | ||
ref: React.Ref<HTMLAnchorElement>, | ||
): MenuItemLinkState => { | ||
// casting because the root slot changes from div to a | ||
const baseState = useMenuItem_unstable(props as MenuItemProps, null); | ||
return { | ||
...baseState, | ||
components: { | ||
...baseState.components, | ||
root: 'a', | ||
}, | ||
root: getNativeElementProps('a', { | ||
ref, | ||
role: 'menuitem', | ||
...props, | ||
}), | ||
}; | ||
}; |
Oops, something went wrong.