-
Notifications
You must be signed in to change notification settings - Fork 1.4k
TagGroup: add default empty state and support for custom empty state #4358
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
Changes from all commits
aad63c3
b0a618d
3c9bbd0
a653e03
29ffb19
67c7531
b5040bb
bc0cac2
df7ccdd
cdde406
342c934
2b3c0d3
9f1abf9
c4ee85d
22acd60
588f410
ab55328
81f5a99
e9e1ed1
8eb60a3
3a3eb19
2fe6eb0
3164f7f
e6d7ca8
72566a4
0786ddb
38cd4ef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| { | ||
| "actions": "الإجراءات", | ||
| "hideButtonLabel": "إظهار أقل", | ||
| "showAllButtonLabel": "عرض الكل ({tagCount, number})" | ||
| "showAllButtonLabel": "عرض الكل ({tagCount, number})", | ||
| "noTags": "None" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| { | ||
| "showAllButtonLabel": "Show all ({tagCount, number})", | ||
| "hideButtonLabel": "Show less", | ||
| "actions": "Actions" | ||
| "actions": "Actions", | ||
| "noTags": "None" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ran chromatic and it broke cuz it doesn't have ar-AE translations, wanna put a dummy string for noTags for ar-AE for now? |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,7 @@ import {AriaTagGroupProps, TagKeyboardDelegate, useTagGroup} from '@react-aria/t | |
| import {classNames, useDOMRef} from '@react-spectrum/utils'; | ||
| import {DOMRef, SpectrumHelpTextProps, SpectrumLabelableProps, StyleProps} from '@react-types/shared'; | ||
| import {Field} from '@react-spectrum/label'; | ||
| import {FocusScope} from '@react-aria/focus'; | ||
| import {FocusRing, FocusScope} from '@react-aria/focus'; | ||
| // @ts-ignore | ||
| import intlMessages from '../intl/*.json'; | ||
| import {ListCollection, useListState} from '@react-stately/list'; | ||
|
|
@@ -31,7 +31,9 @@ export interface SpectrumTagGroupProps<T> extends Omit<AriaTagGroupProps<T>, 'ke | |
| /** The label to display on the action button. */ | ||
| actionLabel?: string, | ||
| /** Handler that is called when the action button is pressed. */ | ||
| onAction?: () => void | ||
| onAction?: () => void, | ||
| /** Sets what the TagGroup should render when there are no tags to display. */ | ||
| renderEmptyState?: () => JSX.Element | ||
| } | ||
|
|
||
| function TagGroup<T extends object>(props: SpectrumTagGroupProps<T>, ref: DOMRef<HTMLDivElement>) { | ||
|
|
@@ -44,7 +46,8 @@ function TagGroup<T extends object>(props: SpectrumTagGroupProps<T>, ref: DOMRef | |
| children, | ||
| actionLabel, | ||
| onAction, | ||
| labelPosition | ||
| labelPosition, | ||
| renderEmptyState = () => stringFormatter.format('noTags') | ||
| } = props; | ||
| let domRef = useDOMRef(ref); | ||
| let containerRef = useRef(null); | ||
|
|
@@ -76,6 +79,13 @@ function TagGroup<T extends object>(props: SpectrumTagGroupProps<T>, ref: DOMRef | |
|
|
||
| let tags = [...currTagsRef.children]; | ||
| let buttons = [...currContainerRef.parentElement.querySelectorAll('button')]; | ||
| if (tags.length === 0 || buttons.length === 0) { | ||
| return { | ||
| visibleTagCount: 0, | ||
| showCollapseButton: false, | ||
| maxHeight: undefined | ||
| }; | ||
| } | ||
| let currY = -Infinity; | ||
| let rowCount = 0; | ||
| let index = 0; | ||
|
|
@@ -150,6 +160,7 @@ function TagGroup<T extends object>(props: SpectrumTagGroupProps<T>, ref: DOMRef | |
| }; | ||
|
|
||
| let showActions = tagState.showCollapseButton || (actionLabel && onAction); | ||
| let isEmpty = state.collection.size === 0; | ||
|
|
||
| return ( | ||
| <FocusScope> | ||
|
|
@@ -173,24 +184,39 @@ function TagGroup<T extends object>(props: SpectrumTagGroupProps<T>, ref: DOMRef | |
| <div | ||
| style={maxRows != null && tagState.showCollapseButton && isCollapsed ? {maxHeight: tagState.maxHeight, overflow: 'hidden'} : undefined} | ||
| ref={containerRef} | ||
| className={classNames(styles, 'spectrum-Tags-container')}> | ||
| <div | ||
| ref={tagsRef} | ||
| {...gridProps} | ||
| className={classNames(styles, 'spectrum-Tags')}> | ||
| {visibleTags.map(item => ( | ||
| <Tag | ||
| {...item.props} | ||
| key={item.key} | ||
| item={item} | ||
| state={state} | ||
| allowsRemoving={allowsRemoving} | ||
| onRemove={onRemove}> | ||
| {item.rendered} | ||
| </Tag> | ||
| ))} | ||
| </div> | ||
| {showActions && | ||
| className={ | ||
| classNames( | ||
| styles, | ||
| 'spectrum-Tags-container', | ||
| { | ||
| 'spectrum-Tags-container--empty': isEmpty | ||
| } | ||
| ) | ||
| }> | ||
| <FocusRing focusRingClass={classNames(styles, 'focus-ring')}> | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure the FocusRing makes sense when ShowAll hides all items. The hiding all items in this story is an existing bug. Would this go away if we fix that bug and always show one tag? |
||
| <div | ||
| ref={tagsRef} | ||
| {...gridProps} | ||
| className={classNames(styles, 'spectrum-Tags')}> | ||
| {visibleTags.map(item => ( | ||
| <Tag | ||
| {...item.props} | ||
| key={item.key} | ||
| item={item} | ||
| state={state} | ||
| allowsRemoving={allowsRemoving} | ||
| onRemove={onRemove}> | ||
| {item.rendered} | ||
| </Tag> | ||
| ))} | ||
| {isEmpty && ( | ||
| <div className={classNames(styles, 'spectrum-Tags-empty-state')}> | ||
| {renderEmptyState()} | ||
| </div> | ||
| )} | ||
| </div> | ||
| </FocusRing> | ||
| {showActions && !isEmpty && | ||
| <Provider isDisabled={false}> | ||
| <div | ||
| role="group" | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.