Skip to content

Commit

Permalink
refactor(tag): Refactor tag with the clickable div element (#145)
Browse files Browse the repository at this point in the history
* refactor(tag): Refactor tag with the clickable div element

* refactor(tag): Refactor Tag with keyboardEventConstants

* refactor(tag): Add line breaks
  • Loading branch information
mfarukkoc authored Aug 26, 2021
1 parent 02214b2 commit ff0f210
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
35 changes: 25 additions & 10 deletions src/tag/Tag.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import "./_tag.scss";

import CloseIcon from "../ui/icons/close.svg";

import React from "react";
import classNames from "classnames";

import Button from "../button/Button";

// SCSS import is moved here to override Button styles without nesting
import "./_tag.scss";
import {KEYBOARD_EVENT_KEY} from "../core/utils/keyboard/keyboardEventConstants";

export interface TagShape<Context = any> {
id: string;
Expand All @@ -27,11 +26,13 @@ function Tag({testid, tag, onRemove, customClassName}: TagProps) {
});

return (
<Button
testid={testid}
customClassName={containerClassName}
onClick={handleRemove}
shouldStopPropagation={true}>
<div
role={"button"}
tabIndex={0}
data-testid={testid}
onKeyDown={handleKeyPress}
className={containerClassName}
onClick={handleRemove}>
<div className={"tag__body"}>{tag.content}</div>

{onRemove && (
Expand All @@ -41,14 +42,28 @@ function Tag({testid, tag, onRemove, customClassName}: TagProps) {
aria-hidden={true}
/>
)}
</Button>
</div>
);

function handleRemove() {
if (onRemove) {
onRemove(tag);
}
}

function handleKeyPress(event: React.KeyboardEvent<HTMLDivElement>) {
event.stopPropagation();

switch (event.key) {
case KEYBOARD_EVENT_KEY.ENTER:
case KEYBOARD_EVENT_KEY.BACKSPACE:
handleRemove();
break;

default:
break;
}
}
}

export default Tag;
2 changes: 2 additions & 0 deletions src/tag/_tag.scss
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
}

.tag--is-removable {
cursor: pointer;

&:hover {
path {
stroke: var(--text-color);
Expand Down

0 comments on commit ff0f210

Please sign in to comment.