diff --git a/packages/react-components/react-tags-preview/stories/InteractionTag/InteractionTagDismiss.stories.tsx b/packages/react-components/react-tags-preview/stories/InteractionTag/InteractionTagDismiss.stories.tsx index 146783dbca0a5..45a1e15cc3bc7 100644 --- a/packages/react-components/react-tags-preview/stories/InteractionTag/InteractionTagDismiss.stories.tsx +++ b/packages/react-components/react-tags-preview/stories/InteractionTag/InteractionTagDismiss.stories.tsx @@ -1,5 +1,5 @@ import * as React from 'react'; - +import { Button, makeStyles } from '@fluentui/react-components'; import { InteractionTag, InteractionTagPrimary, @@ -8,27 +8,79 @@ import { TagGroupProps, } from '@fluentui/react-tags-preview'; +const useStyles = makeStyles({ + container: { + display: 'flex', + flexDirection: 'column', + rowGap: '10px', + }, + resetButton: { + width: 'fit-content', + }, +}); + const initialTags = [ { value: '1', children: 'Tag 1' }, { value: '2', children: 'Tag 2' }, { value: '3', children: 'Tag 3' }, ]; +/** + * focus management for the reset button + */ +const useResetExample = (visibleTagsLength: number) => { + const resetButtonRef = React.useRef(null); + const firstTagRef = React.useRef(null); + + const prevVisibleTagsLengthRef = React.useRef(visibleTagsLength); + React.useEffect(() => { + if (visibleTagsLength === 0) { + resetButtonRef.current?.focus(); + } else if (prevVisibleTagsLengthRef.current === 0) { + firstTagRef.current?.focus(); + } + + prevVisibleTagsLengthRef.current = visibleTagsLength; + }, [visibleTagsLength]); + + return { firstTagRef, resetButtonRef }; +}; + export const Dismiss = () => { const [visibleTags, setVisibleTags] = React.useState(initialTags); const removeItem: TagGroupProps['onDismiss'] = (_e, { value }) => { setVisibleTags([...visibleTags].filter(tag => tag.value !== value)); }; + const resetItems = () => setVisibleTags(initialTags); + const { firstTagRef, resetButtonRef } = useResetExample(visibleTags.length); + + const styles = useStyles(); return ( - - {visibleTags.map(tag => ( - - {tag.children} - - - ))} - +
+ {visibleTags.length !== 0 && ( + + {visibleTags.map((tag, index) => ( + + + {tag.children} + + + + ))} + + )} + + +
); }; diff --git a/packages/react-components/react-tags-preview/stories/Tag/TagDismiss.stories.tsx b/packages/react-components/react-tags-preview/stories/Tag/TagDismiss.stories.tsx index 840a4715ad5e4..a485c037a5e1a 100644 --- a/packages/react-components/react-tags-preview/stories/Tag/TagDismiss.stories.tsx +++ b/packages/react-components/react-tags-preview/stories/Tag/TagDismiss.stories.tsx @@ -1,5 +1,17 @@ import * as React from 'react'; import { Tag, TagGroup, TagGroupProps } from '@fluentui/react-tags-preview'; +import { Button, makeStyles } from '@fluentui/react-components'; + +const useStyles = makeStyles({ + container: { + display: 'flex', + flexDirection: 'column', + rowGap: '10px', + }, + resetButton: { + width: 'fit-content', + }, +}); const initialTags = [ { value: '1', children: 'Tag 1' }, @@ -7,20 +19,65 @@ const initialTags = [ { value: '3', children: 'Tag 3' }, ]; +/** + * focus management for the reset button + */ +const useResetExample = (visibleTagsLength: number) => { + const resetButtonRef = React.useRef(null); + const firstTagRef = React.useRef(null); + + const prevVisibleTagsLengthRef = React.useRef(visibleTagsLength); + React.useEffect(() => { + if (visibleTagsLength === 0) { + resetButtonRef.current?.focus(); + } else if (prevVisibleTagsLengthRef.current === 0) { + firstTagRef.current?.focus(); + } + + prevVisibleTagsLengthRef.current = visibleTagsLength; + }, [visibleTagsLength]); + + return { firstTagRef, resetButtonRef }; +}; + export const Dismiss = () => { const [visibleTags, setVisibleTags] = React.useState(initialTags); const removeItem: TagGroupProps['onDismiss'] = (_e, { value }) => { setVisibleTags([...visibleTags].filter(tag => tag.value !== value)); }; + const resetItems = () => setVisibleTags(initialTags); + const { firstTagRef, resetButtonRef } = useResetExample(visibleTags.length); + + const styles = useStyles(); return ( - - {visibleTags.map(tag => ( - - {tag.children} - - ))} - +
+ {visibleTags.length !== 0 && ( + + {visibleTags.map((tag, index) => ( + + {tag.children} + + ))} + + )} + + +
); }; diff --git a/packages/react-components/react-tags-preview/stories/TagGroup/TagGroupDismiss.stories.tsx b/packages/react-components/react-tags-preview/stories/TagGroup/TagGroupDismiss.stories.tsx index 26982e94ca6a9..779d9bbe9f11b 100644 --- a/packages/react-components/react-tags-preview/stories/TagGroup/TagGroupDismiss.stories.tsx +++ b/packages/react-components/react-tags-preview/stories/TagGroup/TagGroupDismiss.stories.tsx @@ -7,7 +7,18 @@ import { InteractionTagPrimary, InteractionTagSecondary, } from '@fluentui/react-tags-preview'; -import { makeStyles } from '@fluentui/react-components'; +import { Button, makeStyles } from '@fluentui/react-components'; + +const useStyles = makeStyles({ + container: { + display: 'flex', + flexDirection: 'column', + rowGap: '10px', + }, + resetButton: { + width: 'fit-content', + }, +}); const initialTags = [ { value: '1', children: 'Tag 1' }, @@ -15,20 +26,64 @@ const initialTags = [ { value: '3', children: 'Tag 3' }, ]; +/** + * focus management for the reset button + */ +const useResetExample = (visibleTagsLength: number) => { + const resetButtonRef = React.useRef(null); + const firstTagRef = React.useRef(null); + + const prevVisibleTagsLengthRef = React.useRef(visibleTagsLength); + React.useEffect(() => { + if (visibleTagsLength === 0) { + resetButtonRef.current?.focus(); + } else if (prevVisibleTagsLengthRef.current === 0) { + firstTagRef.current?.focus(); + } + + prevVisibleTagsLengthRef.current = visibleTagsLength; + }, [visibleTagsLength]); + + return { firstTagRef, resetButtonRef }; +}; + const DismissWithTags = () => { const [visibleTags, setVisibleTags] = React.useState(initialTags); const removeItem: TagGroupProps['onDismiss'] = (_e, { value }) => { setVisibleTags([...visibleTags].filter(tag => tag.value !== value)); }; + const resetItems = () => setVisibleTags(initialTags); + const { firstTagRef, resetButtonRef } = useResetExample(visibleTags.length); + + const styles = useStyles(); return ( - - {visibleTags.map(tag => ( - - {tag.children} - - ))} - + <> + {visibleTags.length !== 0 && ( + + {visibleTags.map((tag, index) => ( + + {tag.children} + + ))} + + )} + + ); }; @@ -37,27 +92,38 @@ const DismissWithInteractionTags = () => { const removeItem: TagGroupProps['onDismiss'] = (_e, { value }) => { setVisibleTags([...visibleTags].filter(tag => tag.value !== value)); }; + const resetItems = () => setVisibleTags(initialTags); + const { firstTagRef, resetButtonRef } = useResetExample(visibleTags.length); + + const styles = useStyles(); return ( - - {visibleTags.map(tag => ( - - {tag.children} - - - ))} - + <> + {visibleTags.length !== 0 && ( + + {visibleTags.map((tag, index) => ( + + + {tag.children} + + + + ))} + + )} + + ); }; -const useStyles = makeStyles({ - container: { - display: 'flex', - flexDirection: 'column', - rowGap: '10px', - }, -}); - export const Dismiss = () => { const styles = useStyles(); return ( diff --git a/packages/react-components/react-tags-preview/stories/TagGroup/TagGroupOverflow.stories.tsx b/packages/react-components/react-tags-preview/stories/TagGroup/TagGroupOverflow.stories.tsx index 0d35fcac44b80..c2b0ee262d3eb 100644 --- a/packages/react-components/react-tags-preview/stories/TagGroup/TagGroupOverflow.stories.tsx +++ b/packages/react-components/react-tags-preview/stories/TagGroup/TagGroupOverflow.stories.tsx @@ -150,7 +150,7 @@ export const WithOverflow = () => { return (
- + {defaultItems.map(({ value, ...rest }) => (