-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat: add toolbar controlled state example for toggle buttons #24380
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
chpalac
merged 17 commits into
microsoft:master
from
chpalac:feat/toolbar-controlled-state
Sep 2, 2022
Merged
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
60f6e1c
feat: add toolbar controlled state example for toggle buttons
chpalac 10bbcf0
Merge branch 'master' into feat/toolbar-controlled-state
chpalac e776d5d
chore: add changes
chpalac a74c8ff
Merge branch 'feat/toolbar-controlled-state' of github.com:chpalac/fl…
chpalac 6c253ac
chore: remove Menu usages in toolbar example
chpalac 1bcdc0a
chore: use context passed handler
chpalac 6d0158b
chore: add changes
chpalac a3e2b4f
chore: update api
chpalac 41721b4
chore: fix types
chpalac fbed3c8
chore: update api
chpalac 55ea07e
chore: stop spreading props into state
chpalac 93ac85e
chore: fix types
chpalac eb62fa3
chore: move logic to useToolbarToggleButton
chpalac d4f3d1d
chore: remove unnecessary change
chpalac 849ba64
chore: update api
chpalac 39a09be
chore: move size to the use hook
chpalac 39dd491
chore: move size to the use hook
chpalac 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-react-toolbar-3e1d7781-11bc-4929-b8e8-25aa0d674e4a.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": "none", | ||
| "comment": "feat: add toolbar controlled state example for toggle buttons", | ||
| "packageName": "@fluentui/react-toolbar", | ||
| "email": "[email protected]", | ||
| "dependentChangeType": "none" | ||
| } |
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
3 changes: 2 additions & 1 deletion
3
packages/react-components/react-toolbar/src/components/Toolbar/useToolbarContextValues.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
10 changes: 3 additions & 7 deletions
10
...react-components/react-toolbar/src/components/ToolbarToggleButton/ToolbarToggleButton.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
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
35 changes: 35 additions & 0 deletions
35
...act-components/react-toolbar/src/components/ToolbarToggleButton/useToolbarToggleButton.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,35 @@ | ||
| import * as React from 'react'; | ||
| import { useToggleButton_unstable } from '@fluentui/react-button'; | ||
| import { useToolbarContext } from '../Toolbar/ToolbarContext'; | ||
| import { ToolbarToggleButtonProps, ToolbarToggleButtonState } from './ToolbarToggleButton.types'; | ||
|
|
||
| /** | ||
| * Given user props, defines default props for the ToggleButton, calls useButtonState and useChecked, and returns | ||
| * processed state. | ||
| * @param props - User provided props to the ToggleButton component. | ||
| * @param ref - User provided ref to be passed to the ToggleButton component. | ||
| */ | ||
| export const useToolbarToggleButton_unstable = ( | ||
| props: ToolbarToggleButtonProps, | ||
| ref: React.Ref<HTMLButtonElement | HTMLAnchorElement>, | ||
| ): ToolbarToggleButtonState => { | ||
| const { handleToggleButton } = useToolbarContext(); | ||
| const { onClick: onClickOriginal } = props; | ||
| const state = useToggleButton_unstable(props, ref) as ToolbarToggleButtonState; | ||
|
|
||
| const handleOnClick = ( | ||
| e: React.MouseEvent<HTMLButtonElement, MouseEvent> & React.MouseEvent<HTMLAnchorElement, MouseEvent>, | ||
| ) => { | ||
| if (state.disabled) { | ||
| e.preventDefault(); | ||
| e.stopPropagation(); | ||
| return; | ||
| } | ||
|
|
||
| handleToggleButton?.(e, state.name, state.value, state.checked); | ||
| onClickOriginal?.(e); | ||
| }; | ||
|
|
||
| state.root.onClick = handleOnClick; | ||
| return state; | ||
| }; |
18 changes: 18 additions & 0 deletions
18
...ct-components/react-toolbar/src/stories/Toolbar/ToolbarControlledToggleButton.stories.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,18 @@ | ||
| import * as React from 'react'; | ||
| import { Toolbar, ToolbarToggleButton, ToolbarProps } from '@fluentui/react-toolbar'; | ||
|
|
||
| export const ControlledToggleButton = () => { | ||
| const [checkedValues, setCheckedValues] = React.useState<Record<string, string[]>>({ edit: ['cut', 'paste'] }); | ||
| const onChange: ToolbarProps['onCheckedValueChange'] = (e, { name, checkedItems }) => { | ||
| setCheckedValues(s => { | ||
| return s ? { ...s, [name]: checkedItems } : { [name]: checkedItems }; | ||
| }); | ||
| }; | ||
|
|
||
| return ( | ||
| <Toolbar checkedValues={checkedValues} onCheckedValueChange={onChange}> | ||
| <ToolbarToggleButton name="group">Enable Group</ToolbarToggleButton> | ||
| <ToolbarToggleButton name="group">Enable Group</ToolbarToggleButton> | ||
| </Toolbar> | ||
| ); | ||
| }; |
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
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.