Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x | /*
Copyright (c) 2018-2019 Uber Technologies, Inc.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
// @flow
import * as React from 'react';
import {getOverrides} from '../helpers/overrides.js';
import {
Action as StyledAction,
Root as StyledRoot,
ActionIcon as StyledActionIcon,
Text as StyledText,
} from './styled-components.js';
import {KIND, VARIANT} from './constants.js';
import type {PropsT, SharedPropsArgT} from './types.js';
class Tag extends React.Component<PropsT, {}> {
static defaultProps = {
closeable: true,
disabled: false,
isFocused: false,
isHovered: false,
onActionClick: () => {},
onActionKeyDown: () => {},
onClick: null,
onKeyDown: null,
overrides: {},
kind: KIND.primary,
variant: VARIANT.light,
};
handleKeyDown = (event: KeyboardEvent) => {
if (event.currentTarget !== event.target) {
return;
}
const {onClick, onKeyDown} = this.props;
const key = event.key;
if (onClick && key === 'Enter') {
onClick(event);
}
if (onKeyDown) {
onKeyDown(event);
}
};
handleActionKeyDown = (event: KeyboardEvent) => {
const {onActionClick, onActionKeyDown} = this.props;
const key = event.key;
if (onActionClick && key === 'Enter') {
onActionClick(event);
}
if (onActionKeyDown) {
onActionKeyDown(event);
}
};
render() {
const {
children,
closeable,
color,
disabled,
isFocused,
isHovered,
kind,
onActionClick,
onClick,
overrides = {},
variant,
} = this.props;
const [Root, rootProps] = getOverrides(overrides.Root, StyledRoot);
const [Action, actionProps] = getOverrides(overrides.Action, StyledAction);
const [ActionIcon, actionIconProps] = getOverrides(
overrides.ActionIcon,
StyledActionIcon,
);
const [Text, textProps] = getOverrides(overrides.Text, StyledText);
const clickable = typeof onClick === 'function';
const rootHandlers = disabled
? {}
: {
onClick: onClick,
onKeyDown: this.handleKeyDown,
};
const actionHandlers = disabled
? {}
: {
onClick: onActionClick,
onKeyDown: this.handleActionKeyDown,
};
const sharedProps: SharedPropsArgT = {
$clickable: clickable,
$closeable: closeable,
$color: color,
$disabled: disabled,
$isFocused: isFocused,
$isHovered: isHovered,
$kind: kind,
$variant: variant,
};
return (
<Root
data-baseweb="tag"
aria-label="button"
role="button"
tabIndex={clickable ? 0 : null}
{...rootHandlers}
{...sharedProps}
{...rootProps}
>
<Text {...textProps}>{children}</Text>
{closeable ? (
<Action
aria-label="close button"
role="button"
tabIndex={0}
{...actionHandlers}
{...sharedProps}
{...actionProps}
>
<ActionIcon
width={'10'}
height={'10'}
viewBox={'0 0 8 8'}
fill={'none'}
xmlns={'http://www.w3.org/2000/svg'}
{...actionIconProps}
>
<path
fillRule="evenodd"
clipRule="evenodd"
d="M0.861278 0.862254C1.12163 0.601905 1.54374 0.601905 1.80409 0.862254L3.99935 3.05752L6.19461 0.862254C6.45496 0.601905 6.87707 0.601905 7.13742 0.862254C7.39777 1.1226 7.39777 1.54471 7.13742 1.80506L4.94216 4.00033L7.13742 6.19559C7.39777 6.45594 7.39777 6.87805 7.13742 7.1384C6.87707 7.39875 6.45496 7.39875 6.19461 7.1384L3.99935 4.94313L1.80409 7.1384C1.54374 7.39875 1.12163 7.39875 0.861278 7.1384C0.600928 6.87805 0.600928 6.45594 0.861278 6.19559L3.05654 4.00033L0.861278 1.80506C0.600928 1.54471 0.600928 1.1226 0.861278 0.862254Z"
fill="currentColor"
/>
</ActionIcon>
</Action>
) : null}
</Root>
);
}
}
export default Tag;
|