TagGroup
Tags allow users to categorize content. They can represent keywords or people, and are grouped to describe an item or a search request.
install | yarn add @react-spectrum/tag |
---|---|
version | 3.0.0-beta.4 |
usage | import {Item, TagGroup} from '@react-spectrum/tag' |
Example#
<TagGroup aria-label="Static TagGroup items example">
<Item>News</Item>
<Item>Travel</Item>
<Item>Gaming</Item>
<Item>Shopping</Item>
</TagGroup>
<TagGroup aria-label="Static TagGroup items example">
<Item>News</Item>
<Item>Travel</Item>
<Item>Gaming</Item>
<Item>Shopping</Item>
</TagGroup>
<TagGroup aria-label="Static TagGroup items example">
<Item>News</Item>
<Item>Travel</Item>
<Item>Gaming</Item>
<Item>Shopping</Item>
</TagGroup>
Content#
TagGroup is a component that allows users to categorize content. Basic usage of TagGroup, seen in the example above, shows the use of a static collection where the contents of the TagGroup are hard coded. Dynamic collections, as shown below, can be used when the options come from an external data source such as an API, or update over time.
Each item has a unique key defined by the data. In the example below, the key
of each row element is implicitly defined by the id property of the row object.
See collections to learn more about keys in dynamic collections.
const items = [
{id: 1, name: 'News'},
{id: 2, name: 'Travel'},
{id: 3, name: 'Gaming'},
{id: 4, name: 'Shopping'}
];
<TagGroup items={items} aria-label="Dynamic TagGroup items example">
{item => <Item>{item.name}</Item>}
</TagGroup>
const items = [
{ id: 1, name: 'News' },
{ id: 2, name: 'Travel' },
{ id: 3, name: 'Gaming' },
{ id: 4, name: 'Shopping' }
];
<TagGroup
items={items}
aria-label="Dynamic TagGroup items example"
>
{(item) => <Item>{item.name}</Item>}
</TagGroup>
const items = [
{
id: 1,
name: 'News'
},
{
id: 2,
name: 'Travel'
},
{
id: 3,
name: 'Gaming'
},
{
id: 4,
name: 'Shopping'
}
];
<TagGroup
items={items}
aria-label="Dynamic TagGroup items example"
>
{(item) => (
<Item>
{item.name}
</Item>
)}
</TagGroup>
Internationalization#
To internationalize a TagGroup, all text content within the TagGroup should be localized. This includes the aria-label
provided to the TagGroup if any.
For languages that are read right-to-left (e.g. Hebrew and Arabic), the layout of TagGroup is automatically flipped.
Labeling#
A visual label can be provided with the label
prop. If a visual label isn't included, an aria-label
must be provided to the TagGroup for accessibility. If the TagGroup is labeled by a separate element, an aria-labelledby
prop must be provided using the id of the labeling element instead.
<TagGroup label="Categories">
<Item>News</Item>
<Item>Travel</Item>
<Item>Gaming</Item>
<Item>Shopping</Item>
</TagGroup>
<TagGroup label="Categories">
<Item>News</Item>
<Item>Travel</Item>
<Item>Gaming</Item>
<Item>Shopping</Item>
</TagGroup>
<TagGroup label="Categories">
<Item>News</Item>
<Item>Travel</Item>
<Item>Gaming</Item>
<Item>Shopping</Item>
</TagGroup>
Events#
onRemove#
Removing tags can be enabled by providing the allowsRemoving
prop to the TagGroup. TagGroup will pass the key of the removed item to the onRemove
handler as shown below.
function Example() {
let defaultItems = [
{id: 1, name: 'News'},
{id: 2, name: 'Travel'},
{id: 3, name: 'Gaming'},
{id: 4, name: 'Shopping'}
];
let [items, setItems] = React.useState(defaultItems);
let removeItem = (key) => {
setItems(prevItems => prevItems.filter((item) => key !== item.id));
};
return (
<TagGroup
items={items}
allowsRemoving
onRemove={removeItem} aria-label="Removable TagGroup example">
{item => <Item>{item.name}</Item>}
</TagGroup>
);
}
function Example() {
let defaultItems = [
{ id: 1, name: 'News' },
{ id: 2, name: 'Travel' },
{ id: 3, name: 'Gaming' },
{ id: 4, name: 'Shopping' }
];
let [items, setItems] = React.useState(defaultItems);
let removeItem = (key) => {
setItems((prevItems) =>
prevItems.filter((item) => key !== item.id)
);
};
return (
<TagGroup
items={items}
allowsRemoving
onRemove={removeItem} aria-label="Removable TagGroup example"
>
{(item) => <Item>{item.name}</Item>}
</TagGroup>
);
}
function Example() {
let defaultItems = [
{
id: 1,
name: 'News'
},
{
id: 2,
name: 'Travel'
},
{
id: 3,
name: 'Gaming'
},
{
id: 4,
name: 'Shopping'
}
];
let [items, setItems] =
React.useState(
defaultItems
);
let removeItem = (
key
) => {
setItems(
(prevItems) =>
prevItems.filter(
(item) =>
key !==
item.id
)
);
};
return (
<TagGroup
items={items}
allowsRemoving
onRemove={removeItem} aria-label="Removable TagGroup example"
>
{(item) => (
<Item>
{item.name}
</Item>
)}
</TagGroup>
);
}
onAction#
TagGroup supports an onAction
handler that, when used with the actionLabel
prop, will add an action button at the end of the tags that can be used to perform a custom action.
<TagGroup
actionLabel="Clear"
onAction={() => alert('Clear action pressed.')} aria-label="TagGroup with action">
<Item>News</Item>
<Item>Travel</Item>
<Item>Gaming</Item>
<Item>Shopping</Item>
</TagGroup>
<TagGroup
actionLabel="Clear"
onAction={() => alert('Clear action pressed.')} aria-label="TagGroup with action">
<Item>News</Item>
<Item>Travel</Item>
<Item>Gaming</Item>
<Item>Shopping</Item>
</TagGroup>
<TagGroup
actionLabel="Clear"
onAction={() =>
alert(
'Clear action pressed.'
)} aria-label="TagGroup with action"
>
<Item>News</Item>
<Item>Travel</Item>
<Item>Gaming</Item>
<Item>Shopping</Item>
</TagGroup>
Props#
Name | Type | Default | Description |
children | CollectionChildren<object> | — | The contents of the collection. |
actionLabel | string | — | The label to display on the action button. |
keyboardDelegate | TagKeyboardDelegate<object> | — | An optional keyboard delegate to handle arrow key navigation, to override the default. |
allowsRemoving | boolean | — | Whether the TagGroup allows removal of tags. |
maxRows | number | — | Limit the number of rows initially shown. This will render a button that allows the user to expand to show all tags. |
items | Iterable<object> | — | Item objects in the collection. |
validationState | ValidationState | — | Whether the input should display its "valid" or "invalid" visual styling. |
isRequired | boolean | — | Whether user input is required on the input before form submission.
Often paired with the |
labelPosition | LabelPosition | 'top' | The label's overall position relative to the element it is labeling. |
labelAlign | Alignment | 'start' | The label's horizontal alignment relative to the element it is labeling. |
necessityIndicator | NecessityIndicator | 'icon' | Whether the required state should be shown as an icon or text. |
contextualHelp | ReactNode | — | A ContextualHelp element to place next to the label. |
label | ReactNode | — | The content to display as the label. |
isDisabled | boolean | — | Whether the description is displayed with lighter text. |
description | ReactNode | — | A description for the field. Provides a hint such as specific requirements for what to choose. |
errorMessage | ReactNode | — | An error message for the field. |
Events
Name | Type | Default | Description |
onAction | () => void | — | Handler that is called when the action button is pressed. |
onRemove | (
(key: Key
)) => void | — | Called when the user removes a tag. |
Layout
Name | Type | Default | Description |
flex | Responsive<string
| number
| boolean> | — | When used in a flex layout, specifies how the element will grow or shrink to fit the space available. See MDN. |
flexGrow | Responsive<number> | — | When used in a flex layout, specifies how the element will grow to fit the space available. See MDN. |
flexShrink | Responsive<number> | — | When used in a flex layout, specifies how the element will shrink to fit the space available. See MDN. |
flexBasis | Responsive<number | string> | — | When used in a flex layout, specifies the initial main size of the element. See MDN. |
alignSelf | Responsive<'auto'
| 'normal'
| 'start'
| 'end'
| 'center'
| 'flex-start'
| 'flex-end'
| 'self-start'
| 'self-end'
| 'stretch'> | — | Overrides the alignItems property of a flex or grid container. See MDN. |
justifySelf | Responsive<'auto'
| 'normal'
| 'start'
| 'end'
| 'flex-start'
| 'flex-end'
| 'self-start'
| 'self-end'
| 'center'
| 'left'
| 'right'
| 'stretch'> | — | Specifies how the element is justified inside a flex or grid container. See MDN. |
order | Responsive<number> | — | The layout order for the element within a flex or grid container. See MDN. |
gridArea | Responsive<string> | — | When used in a grid layout, specifies the named grid area that the element should be placed in within the grid. See MDN. |
gridColumn | Responsive<string> | — | When used in a grid layout, specifies the column the element should be placed in within the grid. See MDN. |
gridRow | Responsive<string> | — | When used in a grid layout, specifies the row the element should be placed in within the grid. See MDN. |
gridColumnStart | Responsive<string> | — | When used in a grid layout, specifies the starting column to span within the grid. See MDN. |
gridColumnEnd | Responsive<string> | — | When used in a grid layout, specifies the ending column to span within the grid. See MDN. |
gridRowStart | Responsive<string> | — | When used in a grid layout, specifies the starting row to span within the grid. See MDN. |
gridRowEnd | Responsive<string> | — | When used in a grid layout, specifies the ending row to span within the grid. See MDN. |
Spacing
Name | Type | Default | Description |
margin | Responsive<DimensionValue> | — | The margin for all four sides of the element. See MDN. |
marginTop | Responsive<DimensionValue> | — | The margin for the top side of the element. See MDN. |
marginBottom | Responsive<DimensionValue> | — | The margin for the bottom side of the element. See MDN. |
marginStart | Responsive<DimensionValue> | — | The margin for the logical start side of the element, depending on layout direction. See MDN. |
marginEnd | Responsive<DimensionValue> | — | The margin for the logical end side of an element, depending on layout direction. See MDN. |
marginX | Responsive<DimensionValue> | — | The margin for both the left and right sides of the element. See MDN. |
marginY | Responsive<DimensionValue> | — | The margin for both the top and bottom sides of the element. See MDN. |
Sizing
Name | Type | Default | Description |
width | Responsive<DimensionValue> | — | The width of the element. See MDN. |
minWidth | Responsive<DimensionValue> | — | The minimum width of the element. See MDN. |
maxWidth | Responsive<DimensionValue> | — | The maximum width of the element. See MDN. |
height | Responsive<DimensionValue> | — | The height of the element. See MDN. |
minHeight | Responsive<DimensionValue> | — | The minimum height of the element. See MDN. |
maxHeight | Responsive<DimensionValue> | — | The maximum height of the element. See MDN. |
Positioning
Name | Type | Default | Description |
position | Responsive<'static'
| 'relative'
| 'absolute'
| 'fixed'
| 'sticky'> | — | Specifies how the element is positioned. See MDN. |
top | Responsive<DimensionValue> | — | The top position for the element. See MDN. |
bottom | Responsive<DimensionValue> | — | The bottom position for the element. See MDN. |
left | Responsive<DimensionValue> | — | The left position for the element. See MDN. Consider using start instead for RTL support. |
right | Responsive<DimensionValue> | — | The right position for the element. See MDN. Consider using start instead for RTL support. |
start | Responsive<DimensionValue> | — | The logical start position for the element, depending on layout direction. See MDN. |
end | Responsive<DimensionValue> | — | The logical end position for the element, depending on layout direction. See MDN. |
zIndex | Responsive<number> | — | The stacking order for the element. See MDN. |
isHidden | Responsive<boolean> | — | Hides the element. |
Accessibility
Name | Type | Default | Description |
id | string | — | The element's unique identifier. See MDN. |
aria-label | string | — | Defines a string value that labels the current element. |
aria-labelledby | string | — | Identifies the element (or elements) that labels the current element. |
aria-describedby | string | — | Identifies the element (or elements) that describes the object. |
aria-details | string | — | Identifies the element (or elements) that provide a detailed, extended description for the object. |
Advanced
Name | Type | Default | Description |
UNSAFE_className | string | — | Sets the CSS className for the element. Only use as a last resort. Use style props instead. |
UNSAFE_style | CSSProperties | — | Sets inline style for the element. Only use as a last resort. Use style props instead. |
Visual options#
With icons#
<TagGroup aria-label="TagGroup with icons example">
<Item>
<News />
<Text>News</Text>
</Item>
<Item>
<Airplane />
<Text>Travel</Text>
</Item>
<Item>
<Game />
<Text>Gaming</Text>
</Item>
<Item>
<ShoppingCart />
<Text>Shopping</Text>
</Item>
</TagGroup>
<TagGroup aria-label="TagGroup with icons example">
<Item>
<News />
<Text>News</Text>
</Item>
<Item>
<Airplane />
<Text>Travel</Text>
</Item>
<Item>
<Game />
<Text>Gaming</Text>
</Item>
<Item>
<ShoppingCart />
<Text>Shopping</Text>
</Item>
</TagGroup>
<TagGroup aria-label="TagGroup with icons example">
<Item>
<News />
<Text>News</Text>
</Item>
<Item>
<Airplane />
<Text>Travel</Text>
</Item>
<Item>
<Game />
<Text>Gaming</Text>
</Item>
<Item>
<ShoppingCart />
<Text>
Shopping
</Text>
</Item>
</TagGroup>
With avatars#
<TagGroup aria-label="TagGroup with avatars example">
<Item>
<Avatar src="https://i.imgur.com/kJOwAdv.png" />
<Text>Person 1</Text>
</Item>
<Item>
<Avatar src="https://i.imgur.com/kJOwAdv.png" />
<Text>Person 2</Text>
</Item>
<Item>
<Avatar src="https://i.imgur.com/kJOwAdv.png" />
<Text>Person 3</Text>
</Item>
<Item>
<Avatar src="https://i.imgur.com/kJOwAdv.png" />
<Text>Person 4</Text>
</Item>
</TagGroup>
<TagGroup aria-label="TagGroup with avatars example">
<Item>
<Avatar src="https://i.imgur.com/kJOwAdv.png" />
<Text>Person 1</Text>
</Item>
<Item>
<Avatar src="https://i.imgur.com/kJOwAdv.png" />
<Text>Person 2</Text>
</Item>
<Item>
<Avatar src="https://i.imgur.com/kJOwAdv.png" />
<Text>Person 3</Text>
</Item>
<Item>
<Avatar src="https://i.imgur.com/kJOwAdv.png" />
<Text>Person 4</Text>
</Item>
</TagGroup>
<TagGroup aria-label="TagGroup with avatars example">
<Item>
<Avatar src="https://i.imgur.com/kJOwAdv.png" />
<Text>
Person 1
</Text>
</Item>
<Item>
<Avatar src="https://i.imgur.com/kJOwAdv.png" />
<Text>
Person 2
</Text>
</Item>
<Item>
<Avatar src="https://i.imgur.com/kJOwAdv.png" />
<Text>
Person 3
</Text>
</Item>
<Item>
<Avatar src="https://i.imgur.com/kJOwAdv.png" />
<Text>
Person 4
</Text>
</Item>
</TagGroup>
Label position and alignment#
By default, the label is positioned above the TagGroup.
The labelPosition
prop can be used to position the label to the side. The labelAlign
prop can
be used to align the label as "start" or "end".
For left-to-right (LTR) languages, "start" refers to the left most edge of the TagGroup
and "end" refers to the right most edge. For right-to-left (RTL) languages, this is flipped.
<TagGroup label="Categories" labelPosition="side" labelAlign="end">
<Item>News</Item>
<Item>Travel</Item>
<Item>Gaming</Item>
<Item>Shopping</Item>
</TagGroup>
<TagGroup
label="Categories"
labelPosition="side"
labelAlign="end"
>
<Item>News</Item>
<Item>Travel</Item>
<Item>Gaming</Item>
<Item>Shopping</Item>
</TagGroup>
<TagGroup
label="Categories"
labelPosition="side"
labelAlign="end"
>
<Item>News</Item>
<Item>Travel</Item>
<Item>Gaming</Item>
<Item>Shopping</Item>
</TagGroup>
Help text#
Both a description and an error message can be supplied to a TagGroup. The description is always visible unless the validationState
is “invalid” and an error message is provided. The error message can be used to help the user fix their input quickly and should be specific to the detected error. All strings should be localized.
function Example() {
let defaultItems = [
{id: 1, name: 'News'},
{id: 2, name: 'Travel'},
{id: 3, name: 'Gaming'},
{id: 4, name: 'Shopping'}
];
let [items, setItems] = React.useState(defaultItems);
let removeItem = (key) => {
setItems(prevItems => prevItems.filter((item) => key !== item.id));
};
let isValid = items.length <= 3;
return (
<TagGroup
label="Categories"
items={items}
allowsRemoving
onRemove={removeItem}
aria-label="TagGroup help text example"
validationState={isValid ? 'valid' : 'invalid'}
description="Please include tags for related categories."
errorMessage="Must contain no more than 3 tags. Please remove some."
>
{item => <Item>{item.name}</Item>}
</TagGroup>
);
}
function Example() {
let defaultItems = [
{ id: 1, name: 'News' },
{ id: 2, name: 'Travel' },
{ id: 3, name: 'Gaming' },
{ id: 4, name: 'Shopping' }
];
let [items, setItems] = React.useState(defaultItems);
let removeItem = (key) => {
setItems((prevItems) =>
prevItems.filter((item) => key !== item.id)
);
};
let isValid = items.length <= 3;
return (
<TagGroup
label="Categories"
items={items}
allowsRemoving
onRemove={removeItem}
aria-label="TagGroup help text example"
validationState={isValid ? 'valid' : 'invalid'}
description="Please include tags for related categories."
errorMessage="Must contain no more than 3 tags. Please remove some."
>
{(item) => <Item>{item.name}</Item>}
</TagGroup>
);
}
function Example() {
let defaultItems = [
{
id: 1,
name: 'News'
},
{
id: 2,
name: 'Travel'
},
{
id: 3,
name: 'Gaming'
},
{
id: 4,
name: 'Shopping'
}
];
let [items, setItems] =
React.useState(
defaultItems
);
let removeItem = (
key
) => {
setItems(
(prevItems) =>
prevItems.filter(
(item) =>
key !==
item.id
)
);
};
let isValid =
items.length <= 3;
return (
<TagGroup
label="Categories"
items={items}
allowsRemoving
onRemove={removeItem}
aria-label="TagGroup help text example"
validationState={isValid
? 'valid'
: 'invalid'}
description="Please include tags for related categories."
errorMessage="Must contain no more than 3 tags. Please remove some."
>
{(item) => (
<Item>
{item.name}
</Item>
)}
</TagGroup>
);
}
Contextual help#
A ContextualHelp element may be placed next to the label to provide additional information or help about a TagGroup.
import {Content, ContextualHelp, Heading} from '@adobe/react-spectrum';
<TagGroup
label="Categories"
contextualHelp={
<ContextualHelp>
<Heading>What are tags?</Heading>
<Content>Tags allow users to categorize content.</Content>
</ContextualHelp>
}>
<Item>News</Item>
<Item>Travel</Item>
<Item>Gaming</Item>
<Item>Shopping</Item>
</TagGroup>
import {
Content,
ContextualHelp,
Heading
} from '@adobe/react-spectrum';
<TagGroup
label="Categories"
contextualHelp={
<ContextualHelp>
<Heading>What are tags?</Heading>
<Content>
Tags allow users to categorize content.
</Content>
</ContextualHelp>
}
>
<Item>News</Item>
<Item>Travel</Item>
<Item>Gaming</Item>
<Item>Shopping</Item>
</TagGroup>
import {
Content,
ContextualHelp,
Heading
} from '@adobe/react-spectrum';
<TagGroup
label="Categories"
contextualHelp={
<ContextualHelp>
<Heading>
What are tags?
</Heading>
<Content>
Tags allow
users to
categorize
content.
</Content>
</ContextualHelp>
}
>
<Item>News</Item>
<Item>Travel</Item>
<Item>Gaming</Item>
<Item>Shopping</Item>
</TagGroup>
Limit rows#
TagGroup supports a maxRows
prop that will limit the number of rows initially shown. This will add an action button that can be pressed to show the remaining tags.
<View
maxWidth="size-3400"
minHeight="size-2000"
padding="size-150"
borderWidth="thin"
borderColor="dark"
borderRadius="medium"
>
<TagGroup
maxRows={2} aria-label="Static TagGroup items example with maxRows set"
>
<Item>News</Item>
<Item>Travel</Item>
<Item>Gaming</Item>
<Item>Shopping</Item>
<Item>Business</Item>
<Item>Entertainment</Item>
<Item>Food</Item>
<Item>Technology</Item>
<Item>Politics</Item>
<Item>Health</Item>
<Item>Science</Item>
</TagGroup>
</View>
<View
maxWidth="size-3400"
minHeight="size-2000"
padding="size-150"
borderWidth="thin"
borderColor="dark"
borderRadius="medium"
>
<TagGroup
maxRows={2} aria-label="Static TagGroup items example with maxRows set"
>
<Item>News</Item>
<Item>Travel</Item>
<Item>Gaming</Item>
<Item>Shopping</Item>
<Item>Business</Item>
<Item>Entertainment</Item>
<Item>Food</Item>
<Item>Technology</Item>
<Item>Politics</Item>
<Item>Health</Item>
<Item>Science</Item>
</TagGroup>
</View>
<View
maxWidth="size-3400"
minHeight="size-2000"
padding="size-150"
borderWidth="thin"
borderColor="dark"
borderRadius="medium"
>
<TagGroup
maxRows={2} aria-label="Static TagGroup items example with maxRows set"
>
<Item>News</Item>
<Item>Travel</Item>
<Item>Gaming</Item>
<Item>
Shopping
</Item>
<Item>
Business
</Item>
<Item>
Entertainment
</Item>
<Item>Food</Item>
<Item>
Technology
</Item>
<Item>
Politics
</Item>
<Item>Health</Item>
<Item>
Science
</Item>
</TagGroup>
</View>