Skip to content

Commit

Permalink
[Tag] Clarify that onClick and onRemove are mutually exclusive (#2987)
Browse files Browse the repository at this point in the history
* [Tag] Clarify that onClick and onRemove are mutually exclusive

* Shorter, clearer removable tag example

Co-authored-by: Ben Scott <ben.scott@shopify.com>
  • Loading branch information
chloerice and BPScott authored Jan 29, 2021
1 parent 95e4fa7 commit eb0bac1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
2 changes: 2 additions & 0 deletions UNRELEASED.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Use [the changelog guidelines](https://git.io/polaris-changelog-guidelines) to f

### Documentation

- Added an example for the `onRemove` prop to `Tag` and clarified that no remove button is rendered when `onClick` is set ([#2987](https://github.com/Shopify/polaris-react/pull/2987))

### Development workflow

### Dependency upgrades
Expand Down
38 changes: 28 additions & 10 deletions src/components/Tag/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,50 @@ Tags should:

### Default tag

Use to allow merchants to add attributes to, and remove attributes from, an object.
Use to signify the attributes of an object.

```jsx
<Tag>Wholesale</Tag>
```

### Remove tag
### Removable tag

Use to allow merchants to remove attributes from an object.

```jsx
<Tag onRemove={() => {}}>Wholesale</Tag>
function RemovableTagExample() {
const [selectedTags, setSelectedTags] = useState([
'Rustic',
'Antique',
'Vinyl',
'Refurbished',
]);

const removeTag = useCallback(
(tag) => () => {
setSelectedTags((previousTags) =>
previousTags.filter((previousTag) => previousTag !== tag),
);
},
[],
);

const tagMarkup = selectedTags.map((option) => (
<Tag key={option} onRemove={removeTag(option)}>
{option}
</Tag>
));

return <Stack spacing="tight">{tagMarkup}</Stack>;
}
```

### Clickable tag

Use to allow merchants to add attributes to an object.

```jsx
<Tag
onClick={() => {
console.log('Clicked');
}}
>
Wholesale
</Tag>
<Tag onClick={() => console.log('Clicked')}>Wholesale</Tag>
```

<!-- content-for: android -->
Expand Down
4 changes: 2 additions & 2 deletions src/components/Tag/Tag.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ export interface NonMutuallyExclusiveProps {
children?: string;
/** Disables the tag */
disabled?: boolean;
/** Callback when tag is clicked or keypressed */
/** Callback when tag is clicked or keypressed. Renders without remove button when set. */
onClick?(): void;
/** Callback when remove button is clicked or keypressed */
/** Callback when remove button is clicked or keypressed. */
onRemove?(): void;
}

Expand Down

0 comments on commit eb0bac1

Please sign in to comment.