-
Notifications
You must be signed in to change notification settings - Fork 50
Added SplitButton teams component #411
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
Open
fogelek
wants to merge
16
commits into
microsoft:main
Choose a base branch
from
fogelek:opinionated-split-button
base: main
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.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
d44608f
Added SplitButton teams component
e75f3aa
Fixing SpliutButtonProps title props
060a621
Merge remote-tracking branch 'origin/main' into opinionated-split-button
9f6cf9d
Adding basic tests
c77d953
Added MenuTrigger wrapper inside of SplitButton
850e647
Change files
a0e89ec
Fixing types in renderTooltip
efcb5fd
Fixed react-shared-context version
68a9bf3
updating yarn.lock
223a8b1
yarn-deduplicate
d9f68dc
fixing typo in package.json
aa20d07
new yarn.lock
f521737
yarn-deduplicate
32652b9
strengthening types
99e2441
Merge branch 'main' into opinionated-split-button
fogelek cbe8b5f
Fixing comments pt 1
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-contrib-teams-components-29fd49f3-75f7-4481-8b4b-b4cd75278283.json
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,7 @@ | ||
{ | ||
"type": "minor", | ||
"comment": "Added SplitButton teams component", | ||
"packageName": "@fluentui-contrib/teams-components", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch" | ||
} |
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
30 changes: 30 additions & 0 deletions
30
packages/teams-components/src/components/SplitButton/SplitButton.test.tsx
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,30 @@ | ||
import * as React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import { SplitButton } from './SplitButton'; | ||
import { CalendarRegular } from '@fluentui/react-icons'; | ||
|
||
describe('SplitButton', () => { | ||
it('should render', () => { | ||
render(<SplitButton>Test</SplitButton>); | ||
}); | ||
|
||
it('should throw error if menuTitle is provided without main title', () => { | ||
expect(() => | ||
render(<SplitButton menuTitle="Menu">Test</SplitButton>) | ||
).toThrow( | ||
'@fluentui-contrib/teams-components::SplitButton with menuTitle present, title must also be provided' | ||
); | ||
}); | ||
|
||
it('should throw error if no content or icon is provided', () => { | ||
expect(() => render(<SplitButton />)).toThrow( | ||
'@fluentui-contrib/teams-components::SplitButton must have at least one of children or icon' | ||
); | ||
}); | ||
|
||
it('should throw error if icon button has no title provided', () => { | ||
expect(() => render(<SplitButton icon={<CalendarRegular />} />)).toThrow( | ||
'@fluentui-contrib/teams-components::Icon button must have a title or aria label' | ||
); | ||
}); | ||
}); |
82 changes: 82 additions & 0 deletions
82
packages/teams-components/src/components/SplitButton/SplitButton.tsx
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,82 @@ | ||
import * as React from 'react'; | ||
import { | ||
renderSplitButton_unstable, | ||
SplitButtonTitleProps, | ||
} from './renderSplitButton'; | ||
import type { SplitButtonProps as SplitButtonPropsBase } from '@fluentui/react-components'; | ||
import { | ||
useSplitButton_unstable, | ||
useSplitButtonStyles_unstable, | ||
renderSplitButton_unstable as renderSplitButtonBase_unstable, | ||
MenuTrigger, | ||
MenuButtonProps, | ||
} from '@fluentui/react-components'; | ||
import { ForwardRefComponent } from '@fluentui/react-utilities'; | ||
import { useCustomStyleHook_unstable } from '@fluentui/react-shared-contexts'; | ||
import { validateStrictClasses } from '../../strictStyles'; | ||
import { StrictSlot } from '../../strictSlot'; | ||
import { ButtonProps, validateMenuButton } from '../Button'; | ||
import { | ||
validateTitleProps, | ||
validateSplitIconButton, | ||
validateHasContent, | ||
} from './validateProps'; | ||
|
||
export interface SplitButtonProps extends ButtonProps { | ||
menuTitle?: StrictSlot; | ||
} | ||
|
||
const useTeamsSplitButton = ( | ||
props: SplitButtonProps, | ||
triggerProps: MenuButtonProps, | ||
ref: React.ForwardedRef<HTMLButtonElement> | ||
) => { | ||
const { className, icon, title, menuTitle, ...restProps } = props; | ||
const buttonProps = { | ||
...restProps, | ||
...triggerProps, | ||
className: className?.toString(), | ||
icon, | ||
} as SplitButtonPropsBase; | ||
const state = useSplitButton_unstable(buttonProps, ref); | ||
|
||
useSplitButtonStyles_unstable(state); | ||
|
||
useCustomStyleHook_unstable('useSplitButtonStyles_unstable')(state); | ||
|
||
const titleProps: SplitButtonTitleProps = { | ||
title: title ?? undefined, | ||
menuTitle: menuTitle ?? undefined, | ||
}; | ||
|
||
return titleProps.title | ||
? renderSplitButton_unstable(state, titleProps) | ||
: renderSplitButtonBase_unstable(state); | ||
}; | ||
|
||
export const SplitButton = React.forwardRef< | ||
HTMLButtonElement, | ||
SplitButtonProps | ||
>((props, ref) => { | ||
if (process.env.NODE_ENV !== 'production') { | ||
validateProps(props); | ||
} | ||
|
||
return ( | ||
<MenuTrigger> | ||
{(triggerProps: MenuButtonProps) => | ||
useTeamsSplitButton(props, triggerProps, ref) | ||
} | ||
</MenuTrigger> | ||
); | ||
}) as ForwardRefComponent<SplitButtonProps>; | ||
|
||
SplitButton.displayName = 'SplitButton'; | ||
|
||
const validateProps = (props: SplitButtonProps) => { | ||
validateHasContent(props); | ||
validateStrictClasses(props.className); | ||
validateSplitIconButton(props); | ||
validateMenuButton(props); | ||
validateTitleProps(props); | ||
}; |
3 changes: 3 additions & 0 deletions
3
packages/teams-components/src/components/SplitButton/index.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './SplitButton'; | ||
export * from './validateProps'; | ||
export * from './renderSplitButton'; |
59 changes: 59 additions & 0 deletions
59
packages/teams-components/src/components/SplitButton/renderSplitButton.tsx
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,59 @@ | ||
/** @jsxRuntime classic */ | ||
/** @jsx createElement */ | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
import { createElement } from '@fluentui/react-jsx-runtime'; | ||
|
||
import { assertSlots } from '@fluentui/react-utilities'; | ||
import type { | ||
SplitButtonSlots, | ||
SplitButtonState, | ||
} from '@fluentui/react-components'; | ||
import { StrictSlot } from '../../strictSlot'; | ||
import { renderTooltip } from './renderTooltip'; | ||
|
||
export interface SplitButtonTitleProps { | ||
title?: NonNullable<StrictSlot>; | ||
menuTitle?: NonNullable<StrictSlot>; | ||
} | ||
|
||
/** | ||
* Renders a SplitButton component by passing the state defined props to the appropriate slots. | ||
*/ | ||
export const renderSplitButton_unstable = ( | ||
state: SplitButtonState, | ||
titleProps: SplitButtonTitleProps | ||
) => { | ||
assertSlots<SplitButtonSlots>(state); | ||
|
||
// rendering without tootlip if title is not defined | ||
if (titleProps.title === undefined) { | ||
return ( | ||
<state.root> | ||
{state.primaryActionButton && <state.primaryActionButton />} | ||
{state.menuButton && <state.menuButton />} | ||
</state.root> | ||
); | ||
} | ||
|
||
// if both title and menuTitle are defined, render separate tooltips for each button | ||
if (titleProps.menuTitle !== undefined) { | ||
return ( | ||
<state.root> | ||
{state.primaryActionButton && | ||
renderTooltip(<state.primaryActionButton />, titleProps.title)} | ||
{state.menuButton && | ||
renderTooltip(<state.menuButton />, titleProps.menuTitle)} | ||
</state.root> | ||
); | ||
} | ||
|
||
// render single tooltip for the whole split button | ||
return renderTooltip( | ||
<state.root> | ||
{state.primaryActionButton && <state.primaryActionButton />} | ||
{state.menuButton && <state.menuButton />} | ||
</state.root>, | ||
titleProps.title | ||
); | ||
}; |
14 changes: 14 additions & 0 deletions
14
packages/teams-components/src/components/SplitButton/renderTooltip.tsx
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,14 @@ | ||
import * as React from 'react'; | ||
import { Tooltip, TooltipProps } from '@fluentui/react-components'; | ||
import { StrictSlot } from '../../strictSlot'; | ||
|
||
export const renderTooltip = ( | ||
children: TooltipProps['children'], | ||
title: NonNullable<StrictSlot> | ||
) => { | ||
return ( | ||
<Tooltip content={title} relationship="label"> | ||
{children} | ||
</Tooltip> | ||
); | ||
}; |
40 changes: 40 additions & 0 deletions
40
packages/teams-components/src/components/SplitButton/validateProps.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
export const validateTitleProps = (props: { | ||
title?: unknown; | ||
menuTitle?: unknown; | ||
}) => { | ||
if (props.menuTitle && !props.title) { | ||
throw new Error( | ||
'@fluentui-contrib/teams-components::SplitButton with menuTitle present, title must also be provided' | ||
); | ||
} | ||
}; | ||
|
||
export const validateSplitIconButton = (props: { | ||
title?: unknown; | ||
icon?: unknown; | ||
children?: unknown; | ||
'aria-label'?: string; | ||
'aria-labelledby'?: string; | ||
}) => { | ||
if ( | ||
!props.children && | ||
props.icon && | ||
!props.title && | ||
!(props['aria-label'] || props['aria-labelledby']) | ||
) { | ||
throw new Error( | ||
'@fluentui-contrib/teams-components::Icon button must have a title or aria label' | ||
); | ||
} | ||
}; | ||
|
||
export const validateHasContent = (props: { | ||
children?: unknown; | ||
icon?: unknown; | ||
}) => { | ||
if (!props.children && !props.icon) { | ||
throw new Error( | ||
'@fluentui-contrib/teams-components::SplitButton must have at least one of children or icon' | ||
); | ||
} | ||
}; |
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
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.
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.
I don't understand this requirement, why would menu title make the title required?
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.
If title is provided without menu title, tooltip will be applied to the full SplitButton
If both are given, there will be separate tooltip for action button and menu button - so I think it makes no sense to be able to provide only menu title