@snack-uikit/tag 0.12.9 → 0.12.11-preview-bb8e00d0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,18 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## 0.12.10 (2025-03-17)
7
+
8
+ ### Only dependencies have been changed
9
+ * [@snack-uikit/dropdown@0.4.7](https://github.com/cloud-ru-tech/snack-uikit/blob/master/packages/dropdown/CHANGELOG.md)
10
+ * [@snack-uikit/icons@0.26.1](https://github.com/cloud-ru-tech/snack-uikit/blob/master/packages/icons/CHANGELOG.md)
11
+ * [@snack-uikit/scroll@0.9.5](https://github.com/cloud-ru-tech/snack-uikit/blob/master/packages/scroll/CHANGELOG.md)
12
+ * [@snack-uikit/utils@3.8.1](https://github.com/cloud-ru-tech/snack-uikit/blob/master/packages/utils/CHANGELOG.md)
13
+
14
+
15
+
16
+
17
+
6
18
  ## 0.12.9 (2025-03-14)
7
19
 
8
20
  ### Only dependencies have been changed
package/README.md CHANGED
@@ -18,6 +18,7 @@
18
18
  | appearance | enum Appearance: `"neutral"`, `"primary"`, `"red"`, `"orange"`, `"yellow"`, `"green"`, `"blue"`, `"violet"`, `"pink"` | - | Внешний вид |
19
19
  | className | `string` | - | CSS-класс |
20
20
  | tabIndex | `number` | - | tabIndex кнопки удаления |
21
+ | tooltip | `TooltipProps` | - | Тултип над тегом |
21
22
  | onClick | `(e: MouseEvent<HTMLAnchorElement, MouseEvent>) => void` | - | |
22
23
  | target | `string` | - | |
23
24
  ## Tag
@@ -31,6 +32,7 @@
31
32
  | appearance | enum Appearance: `"neutral"`, `"primary"`, `"red"`, `"orange"`, `"yellow"`, `"green"`, `"blue"`, `"violet"`, `"pink"` | - | Внешний вид |
32
33
  | className | `string` | - | CSS-класс |
33
34
  | tabIndex | `number` | - | tabIndex кнопки удаления |
35
+ | tooltip | `TooltipProps` | - | Тултип над тегом |
34
36
  | onClick | `(e: MouseEvent<HTMLAnchorElement, MouseEvent>) => void` | - | |
35
37
  | target | `string` | - | |
36
38
  ## TagRow
@@ -1,5 +1,4 @@
1
- import { TagBaseProps } from '../TagBase';
2
- import { TagLinkProps } from '../TagLink';
3
- export type TagProps = TagBaseProps | TagLinkProps;
1
+ import { TagBaseProps, TagLinkProps, TagTooltipProps } from '../../types';
2
+ export type TagProps = (TagBaseProps | TagLinkProps) & TagTooltipProps;
4
3
  export declare function isTagLinkProps(props: TagProps): props is TagLinkProps;
5
- export declare function Tag(props: TagProps): import("react/jsx-runtime").JSX.Element;
4
+ export declare function Tag({ tooltip, ...props }: TagProps): import("react/jsx-runtime").JSX.Element;
@@ -1,19 +1,45 @@
1
1
  "use strict";
2
2
 
3
+ var __rest = void 0 && (void 0).__rest || function (s, e) {
4
+ var t = {};
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
6
+ if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
8
+ }
9
+ return t;
10
+ };
3
11
  Object.defineProperty(exports, "__esModule", {
4
12
  value: true
5
13
  });
6
14
  exports.isTagLinkProps = isTagLinkProps;
7
15
  exports.Tag = Tag;
8
16
  const jsx_runtime_1 = require("react/jsx-runtime");
17
+ const react_1 = require("react");
18
+ const tooltip_1 = require("@snack-uikit/tooltip");
9
19
  const TagBase_1 = require("../TagBase");
10
20
  const TagLink_1 = require("../TagLink");
11
21
  function isTagLinkProps(props) {
12
22
  return 'href' in props && props.href !== undefined;
13
23
  }
14
- function Tag(props) {
15
- if (isTagLinkProps(props)) {
16
- return (0, jsx_runtime_1.jsx)(TagLink_1.TagLink, Object.assign({}, props));
17
- }
18
- return (0, jsx_runtime_1.jsx)(TagBase_1.TagBase, Object.assign({}, props));
24
+ function isRemovable(props) {
25
+ return 'onDelete' in props && props.onDelete !== undefined;
26
+ }
27
+ function Tag(_a) {
28
+ var {
29
+ tooltip
30
+ } = _a,
31
+ props = __rest(_a, ["tooltip"]);
32
+ const [restrictOpenTooltip, setRestrictOpenTooltip] = (0, react_1.useState)(false);
33
+ const removable = isRemovable(props);
34
+ const TagComponent = isTagLinkProps(props) ? TagLink_1.TagLink : TagBase_1.TagBase;
35
+ const TagWithTooltip = (0, tooltip_1.useElementWithTooltip)({
36
+ Element: TagComponent,
37
+ tooltip: tooltip && Object.assign(Object.assign({}, tooltip), {
38
+ open: removable && restrictOpenTooltip ? false : tooltip.open
39
+ })
40
+ });
41
+ const tagProps = Object.assign(Object.assign({}, props), {
42
+ changeRestrictTooltipState: setRestrictOpenTooltip
43
+ });
44
+ return (0, jsx_runtime_1.jsx)(TagWithTooltip, Object.assign({}, tagProps));
19
45
  }
@@ -1,8 +1,3 @@
1
- import { MouseEventHandler } from 'react';
2
- import { WithSupportProps } from '@snack-uikit/utils';
3
- import { CommonTagProps } from '../../types';
4
- export type TagBaseProps = WithSupportProps<{
5
- /** Коллбэк на удаление */
6
- onDelete?: MouseEventHandler<HTMLButtonElement>;
7
- }> & CommonTagProps;
8
- export declare function TagBase({ label, size, appearance, onDelete, className, tabIndex, ...rest }: TagBaseProps): import("react/jsx-runtime").JSX.Element;
1
+ import { ManageRestrictPopoverProps, TagBaseProps } from '../../types';
2
+ export type TagBaseComponentProps = TagBaseProps & ManageRestrictPopoverProps;
3
+ export declare function TagBase({ label, size, appearance, onDelete, className, tabIndex, changeRestrictTooltipState, ...rest }: TagBaseComponentProps): import("react/jsx-runtime").JSX.Element;
@@ -19,6 +19,7 @@ Object.defineProperty(exports, "__esModule", {
19
19
  exports.TagBase = TagBase;
20
20
  const jsx_runtime_1 = require("react/jsx-runtime");
21
21
  const classnames_1 = __importDefault(require("classnames"));
22
+ const react_1 = require("react");
22
23
  const icons_1 = require("@snack-uikit/icons");
23
24
  const utils_1 = require("@snack-uikit/utils");
24
25
  const constants_1 = require("../../constants");
@@ -31,24 +32,46 @@ function TagBase(_a) {
31
32
  appearance = constants_1.APPEARANCE.Neutral,
32
33
  onDelete,
33
34
  className,
34
- tabIndex
35
+ tabIndex,
36
+ changeRestrictTooltipState
35
37
  } = _a,
36
- rest = __rest(_a, ["label", "size", "appearance", "onDelete", "className", "tabIndex"]);
38
+ rest = __rest(_a, ["label", "size", "appearance", "onDelete", "className", "tabIndex", "changeRestrictTooltipState"]);
39
+ const deleteButtonRef = (0, react_1.useRef)(null);
40
+ const isRemovable = Boolean(onDelete);
41
+ const handleMouseEnter = event => {
42
+ var _a;
43
+ if (!isRemovable) {
44
+ return;
45
+ }
46
+ if ((_a = deleteButtonRef.current) === null || _a === void 0 ? void 0 : _a.contains(event.target)) {
47
+ changeRestrictTooltipState(true);
48
+ }
49
+ };
50
+ const handleMouseLeave = () => {
51
+ if (!isRemovable) {
52
+ return;
53
+ }
54
+ changeRestrictTooltipState(false);
55
+ };
37
56
  return (0, jsx_runtime_1.jsxs)("span", Object.assign({}, (0, utils_1.extractSupportProps)(rest), {
38
57
  className: (0, classnames_1.default)(styles_module_scss_1.default.tag, className),
39
58
  "data-size": size,
40
59
  "data-appearance": appearance,
41
- "data-removable": Boolean(onDelete),
60
+ "data-removable": isRemovable,
61
+ onMouseEnter: handleMouseEnter,
62
+ onMouseLeave: handleMouseLeave,
42
63
  children: [(0, jsx_runtime_1.jsx)("span", {
43
- title: label,
44
64
  className: styles_module_scss_1.default.label,
45
65
  children: label
46
- }), onDelete && (0, jsx_runtime_1.jsx)("button", {
66
+ }), isRemovable && (0, jsx_runtime_1.jsx)("button", {
47
67
  type: 'button',
48
68
  className: styles_module_scss_1.default.tagButton,
49
69
  onClick: onDelete,
50
70
  "data-test-id": 'tag-remove-button',
51
71
  tabIndex: tabIndex,
72
+ onMouseEnter: handleMouseEnter,
73
+ onMouseLeave: handleMouseLeave,
74
+ ref: deleteButtonRef,
52
75
  children: size === constants_1.SIZE.Xs ? (0, jsx_runtime_1.jsx)(icons_1.CrossSVG, {
53
76
  size: constants_2.ICON_SIZE[size],
54
77
  className: styles_module_scss_1.default.icon
@@ -1,9 +1,3 @@
1
- import { MouseEvent } from 'react';
2
- import { WithSupportProps } from '@snack-uikit/utils';
3
- import { CommonTagProps } from '../../types';
4
- export type TagLinkProps = WithSupportProps<CommonTagProps> & {
5
- href: string;
6
- onClick?(e: MouseEvent<HTMLAnchorElement>): void;
7
- target?: HTMLAnchorElement['target'];
8
- };
9
- export declare function TagLink({ label, size, appearance, className, tabIndex, href, onClick, target, ...rest }: TagLinkProps): import("react/jsx-runtime").JSX.Element;
1
+ import { ManageRestrictPopoverProps, TagLinkProps } from '../../types';
2
+ export type TagLinkComponentProps = TagLinkProps & ManageRestrictPopoverProps;
3
+ export declare function TagLink({ label, size, appearance, className, tabIndex, href, onClick, target, ...rest }: TagLinkComponentProps): import("react/jsx-runtime").JSX.Element;
@@ -43,7 +43,6 @@ function TagLink(_a) {
43
43
  target: target,
44
44
  onClick: onClick,
45
45
  children: (0, jsx_runtime_1.jsx)("span", {
46
- title: label,
47
46
  className: styles_module_scss_1.default.label,
48
47
  children: label
49
48
  })
@@ -1,5 +1,6 @@
1
- import { MouseEvent } from 'react';
2
- import { ValueOf } from '@snack-uikit/utils';
1
+ import { MouseEvent, MouseEventHandler } from 'react';
2
+ import { TooltipProps } from '@snack-uikit/tooltip';
3
+ import { ValueOf, WithSupportProps } from '@snack-uikit/utils';
3
4
  import { APPEARANCE, SIZE } from './constants';
4
5
  export type Appearance = ValueOf<typeof APPEARANCE>;
5
6
  export type Size = ValueOf<typeof SIZE>;
@@ -28,3 +29,19 @@ export type CommonTagProps = {
28
29
  /** tabIndex кнопки удаления */
29
30
  tabIndex?: number;
30
31
  };
32
+ export type TagTooltipProps = {
33
+ /** Тултип над тегом */
34
+ tooltip?: TooltipProps;
35
+ };
36
+ export type TagBaseProps = WithSupportProps<{
37
+ /** Коллбэк на удаление */
38
+ onDelete?: MouseEventHandler<HTMLButtonElement>;
39
+ }> & CommonTagProps;
40
+ export type TagLinkProps = WithSupportProps<CommonTagProps> & {
41
+ href: string;
42
+ onClick?(e: MouseEvent<HTMLAnchorElement>): void;
43
+ target?: HTMLAnchorElement['target'];
44
+ };
45
+ export type ManageRestrictPopoverProps = {
46
+ changeRestrictTooltipState: (state: boolean) => void;
47
+ };
@@ -1,5 +1,4 @@
1
- import { TagBaseProps } from '../TagBase';
2
- import { TagLinkProps } from '../TagLink';
3
- export type TagProps = TagBaseProps | TagLinkProps;
1
+ import { TagBaseProps, TagLinkProps, TagTooltipProps } from '../../types';
2
+ export type TagProps = (TagBaseProps | TagLinkProps) & TagTooltipProps;
4
3
  export declare function isTagLinkProps(props: TagProps): props is TagLinkProps;
5
- export declare function Tag(props: TagProps): import("react/jsx-runtime").JSX.Element;
4
+ export declare function Tag({ tooltip, ...props }: TagProps): import("react/jsx-runtime").JSX.Element;
@@ -1,12 +1,34 @@
1
+ var __rest = (this && this.__rest) || function (s, e) {
2
+ var t = {};
3
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
+ t[p] = s[p];
5
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
+ t[p[i]] = s[p[i]];
9
+ }
10
+ return t;
11
+ };
1
12
  import { jsx as _jsx } from "react/jsx-runtime";
13
+ import { useState } from 'react';
14
+ import { useElementWithTooltip } from '@snack-uikit/tooltip';
2
15
  import { TagBase } from '../TagBase';
3
16
  import { TagLink } from '../TagLink';
4
17
  export function isTagLinkProps(props) {
5
18
  return 'href' in props && props.href !== undefined;
6
19
  }
7
- export function Tag(props) {
8
- if (isTagLinkProps(props)) {
9
- return _jsx(TagLink, Object.assign({}, props));
10
- }
11
- return _jsx(TagBase, Object.assign({}, props));
20
+ function isRemovable(props) {
21
+ return 'onDelete' in props && props.onDelete !== undefined;
22
+ }
23
+ export function Tag(_a) {
24
+ var { tooltip } = _a, props = __rest(_a, ["tooltip"]);
25
+ const [restrictOpenTooltip, setRestrictOpenTooltip] = useState(false);
26
+ const removable = isRemovable(props);
27
+ const TagComponent = isTagLinkProps(props) ? TagLink : TagBase;
28
+ const TagWithTooltip = useElementWithTooltip({
29
+ Element: TagComponent,
30
+ tooltip: tooltip && Object.assign(Object.assign({}, tooltip), { open: removable && restrictOpenTooltip ? false : tooltip.open }),
31
+ });
32
+ const tagProps = Object.assign(Object.assign({}, props), { changeRestrictTooltipState: setRestrictOpenTooltip });
33
+ return _jsx(TagWithTooltip, Object.assign({}, tagProps));
12
34
  }
@@ -1,8 +1,3 @@
1
- import { MouseEventHandler } from 'react';
2
- import { WithSupportProps } from '@snack-uikit/utils';
3
- import { CommonTagProps } from '../../types';
4
- export type TagBaseProps = WithSupportProps<{
5
- /** Коллбэк на удаление */
6
- onDelete?: MouseEventHandler<HTMLButtonElement>;
7
- }> & CommonTagProps;
8
- export declare function TagBase({ label, size, appearance, onDelete, className, tabIndex, ...rest }: TagBaseProps): import("react/jsx-runtime").JSX.Element;
1
+ import { ManageRestrictPopoverProps, TagBaseProps } from '../../types';
2
+ export type TagBaseComponentProps = TagBaseProps & ManageRestrictPopoverProps;
3
+ export declare function TagBase({ label, size, appearance, onDelete, className, tabIndex, changeRestrictTooltipState, ...rest }: TagBaseComponentProps): import("react/jsx-runtime").JSX.Element;
@@ -11,12 +11,30 @@ var __rest = (this && this.__rest) || function (s, e) {
11
11
  };
12
12
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
13
13
  import cn from 'classnames';
14
+ import { useRef } from 'react';
14
15
  import { CrossSVG } from '@snack-uikit/icons';
15
16
  import { extractSupportProps } from '@snack-uikit/utils';
16
17
  import { APPEARANCE, SIZE } from '../../constants';
17
18
  import { ICON_SIZE } from './constants';
18
19
  import styles from './styles.module.css';
19
20
  export function TagBase(_a) {
20
- var { label, size = SIZE.Xs, appearance = APPEARANCE.Neutral, onDelete, className, tabIndex } = _a, rest = __rest(_a, ["label", "size", "appearance", "onDelete", "className", "tabIndex"]);
21
- return (_jsxs("span", Object.assign({}, extractSupportProps(rest), { className: cn(styles.tag, className), "data-size": size, "data-appearance": appearance, "data-removable": Boolean(onDelete), children: [_jsx("span", { title: label, className: styles.label, children: label }), onDelete && (_jsx("button", { type: 'button', className: styles.tagButton, onClick: onDelete, "data-test-id": 'tag-remove-button', tabIndex: tabIndex, children: size === SIZE.Xs ? (_jsx(CrossSVG, { size: ICON_SIZE[size], className: styles.icon })) : (_jsx(CrossSVG, { size: ICON_SIZE[size], className: styles.icon })) }))] })));
21
+ var { label, size = SIZE.Xs, appearance = APPEARANCE.Neutral, onDelete, className, tabIndex, changeRestrictTooltipState } = _a, rest = __rest(_a, ["label", "size", "appearance", "onDelete", "className", "tabIndex", "changeRestrictTooltipState"]);
22
+ const deleteButtonRef = useRef(null);
23
+ const isRemovable = Boolean(onDelete);
24
+ const handleMouseEnter = (event) => {
25
+ var _a;
26
+ if (!isRemovable) {
27
+ return;
28
+ }
29
+ if ((_a = deleteButtonRef.current) === null || _a === void 0 ? void 0 : _a.contains(event.target)) {
30
+ changeRestrictTooltipState(true);
31
+ }
32
+ };
33
+ const handleMouseLeave = () => {
34
+ if (!isRemovable) {
35
+ return;
36
+ }
37
+ changeRestrictTooltipState(false);
38
+ };
39
+ return (_jsxs("span", Object.assign({}, extractSupportProps(rest), { className: cn(styles.tag, className), "data-size": size, "data-appearance": appearance, "data-removable": isRemovable, onMouseEnter: handleMouseEnter, onMouseLeave: handleMouseLeave, children: [_jsx("span", { className: styles.label, children: label }), isRemovable && (_jsx("button", { type: 'button', className: styles.tagButton, onClick: onDelete, "data-test-id": 'tag-remove-button', tabIndex: tabIndex, onMouseEnter: handleMouseEnter, onMouseLeave: handleMouseLeave, ref: deleteButtonRef, children: size === SIZE.Xs ? (_jsx(CrossSVG, { size: ICON_SIZE[size], className: styles.icon })) : (_jsx(CrossSVG, { size: ICON_SIZE[size], className: styles.icon })) }))] })));
22
40
  }
@@ -1,9 +1,3 @@
1
- import { MouseEvent } from 'react';
2
- import { WithSupportProps } from '@snack-uikit/utils';
3
- import { CommonTagProps } from '../../types';
4
- export type TagLinkProps = WithSupportProps<CommonTagProps> & {
5
- href: string;
6
- onClick?(e: MouseEvent<HTMLAnchorElement>): void;
7
- target?: HTMLAnchorElement['target'];
8
- };
9
- export declare function TagLink({ label, size, appearance, className, tabIndex, href, onClick, target, ...rest }: TagLinkProps): import("react/jsx-runtime").JSX.Element;
1
+ import { ManageRestrictPopoverProps, TagLinkProps } from '../../types';
2
+ export type TagLinkComponentProps = TagLinkProps & ManageRestrictPopoverProps;
3
+ export declare function TagLink({ label, size, appearance, className, tabIndex, href, onClick, target, ...rest }: TagLinkComponentProps): import("react/jsx-runtime").JSX.Element;
@@ -16,5 +16,5 @@ import { APPEARANCE, SIZE } from '../../constants';
16
16
  import styles from './styles.module.css';
17
17
  export function TagLink(_a) {
18
18
  var { label, size = SIZE.Xs, appearance = APPEARANCE.Neutral, className, tabIndex, href, onClick, target } = _a, rest = __rest(_a, ["label", "size", "appearance", "className", "tabIndex", "href", "onClick", "target"]);
19
- return (_jsx("a", Object.assign({}, extractSupportProps(rest), { className: cn(styles.tag, className), "data-size": size, "data-appearance": appearance, tabIndex: tabIndex, href: href, target: target, onClick: onClick, children: _jsx("span", { title: label, className: styles.label, children: label }) })));
19
+ return (_jsx("a", Object.assign({}, extractSupportProps(rest), { className: cn(styles.tag, className), "data-size": size, "data-appearance": appearance, tabIndex: tabIndex, href: href, target: target, onClick: onClick, children: _jsx("span", { className: styles.label, children: label }) })));
20
20
  }
@@ -1,5 +1,6 @@
1
- import { MouseEvent } from 'react';
2
- import { ValueOf } from '@snack-uikit/utils';
1
+ import { MouseEvent, MouseEventHandler } from 'react';
2
+ import { TooltipProps } from '@snack-uikit/tooltip';
3
+ import { ValueOf, WithSupportProps } from '@snack-uikit/utils';
3
4
  import { APPEARANCE, SIZE } from './constants';
4
5
  export type Appearance = ValueOf<typeof APPEARANCE>;
5
6
  export type Size = ValueOf<typeof SIZE>;
@@ -28,3 +29,19 @@ export type CommonTagProps = {
28
29
  /** tabIndex кнопки удаления */
29
30
  tabIndex?: number;
30
31
  };
32
+ export type TagTooltipProps = {
33
+ /** Тултип над тегом */
34
+ tooltip?: TooltipProps;
35
+ };
36
+ export type TagBaseProps = WithSupportProps<{
37
+ /** Коллбэк на удаление */
38
+ onDelete?: MouseEventHandler<HTMLButtonElement>;
39
+ }> & CommonTagProps;
40
+ export type TagLinkProps = WithSupportProps<CommonTagProps> & {
41
+ href: string;
42
+ onClick?(e: MouseEvent<HTMLAnchorElement>): void;
43
+ target?: HTMLAnchorElement['target'];
44
+ };
45
+ export type ManageRestrictPopoverProps = {
46
+ changeRestrictTooltipState: (state: boolean) => void;
47
+ };
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "access": "public"
5
5
  },
6
6
  "title": "Tag",
7
- "version": "0.12.9",
7
+ "version": "0.12.11-preview-bb8e00d0.0",
8
8
  "sideEffects": [
9
9
  "*.css",
10
10
  "*.woff",
@@ -36,11 +36,12 @@
36
36
  "license": "Apache-2.0",
37
37
  "scripts": {},
38
38
  "dependencies": {
39
- "@snack-uikit/dropdown": "0.4.6",
40
- "@snack-uikit/icons": "0.26.0",
41
- "@snack-uikit/scroll": "0.9.4",
42
- "@snack-uikit/utils": "3.8.0",
39
+ "@snack-uikit/dropdown": "0.4.7",
40
+ "@snack-uikit/icons": "0.26.1",
41
+ "@snack-uikit/scroll": "0.9.5",
42
+ "@snack-uikit/tooltip": "0.17.0",
43
+ "@snack-uikit/utils": "3.8.1",
43
44
  "classnames": "2.5.1"
44
45
  },
45
- "gitHead": "b27699a7db8812f2d06c49346d1b4ab6d0d8c24e"
46
+ "gitHead": "6b680ebf8141ff9a3f2f060c983839ce9d8c4d3a"
46
47
  }
@@ -1,16 +1,38 @@
1
- import { TagBase, TagBaseProps } from '../TagBase';
2
- import { TagLink, TagLinkProps } from '../TagLink';
1
+ import { JSXElementConstructor, useState } from 'react';
3
2
 
4
- export type TagProps = TagBaseProps | TagLinkProps;
3
+ import { useElementWithTooltip } from '@snack-uikit/tooltip';
4
+
5
+ import { TagBaseProps, TagLinkProps, TagTooltipProps } from '../../types';
6
+ import { TagBase } from '../TagBase';
7
+ import { TagLink } from '../TagLink';
8
+
9
+ export type TagProps = (TagBaseProps | TagLinkProps) & TagTooltipProps;
10
+
11
+ type ExtractTagProps<T> = T extends JSXElementConstructor<infer P> | ((props: infer P) => JSX.Element) ? P : never;
5
12
 
6
13
  export function isTagLinkProps(props: TagProps): props is TagLinkProps {
7
14
  return 'href' in props && props.href !== undefined;
8
15
  }
9
16
 
10
- export function Tag(props: TagProps) {
11
- if (isTagLinkProps(props)) {
12
- return <TagLink {...props} />;
13
- }
17
+ function isRemovable(props: TagProps): props is TagLinkProps {
18
+ return 'onDelete' in props && props.onDelete !== undefined;
19
+ }
20
+
21
+ export function Tag({ tooltip, ...props }: TagProps) {
22
+ const [restrictOpenTooltip, setRestrictOpenTooltip] = useState(false);
23
+ const removable = isRemovable(props);
24
+
25
+ const TagComponent = isTagLinkProps(props) ? TagLink : TagBase;
26
+ const TagWithTooltip = useElementWithTooltip({
27
+ Element: TagComponent,
28
+ tooltip: tooltip && {
29
+ ...tooltip,
30
+ open: removable && restrictOpenTooltip ? false : tooltip.open,
31
+ },
32
+ });
14
33
 
15
- return <TagBase {...props} />;
34
+ const tagProps = { ...props, changeRestrictTooltipState: setRestrictOpenTooltip } as ExtractTagProps<
35
+ typeof TagWithTooltip
36
+ >;
37
+ return <TagWithTooltip {...tagProps} />;
16
38
  }
@@ -1,19 +1,15 @@
1
1
  import cn from 'classnames';
2
- import { MouseEventHandler } from 'react';
2
+ import { MouseEvent, useRef } from 'react';
3
3
 
4
4
  import { CrossSVG } from '@snack-uikit/icons';
5
- import { extractSupportProps, WithSupportProps } from '@snack-uikit/utils';
5
+ import { extractSupportProps } from '@snack-uikit/utils';
6
6
 
7
7
  import { APPEARANCE, SIZE } from '../../constants';
8
- import { CommonTagProps } from '../../types';
8
+ import { ManageRestrictPopoverProps, TagBaseProps } from '../../types';
9
9
  import { ICON_SIZE } from './constants';
10
10
  import styles from './styles.module.scss';
11
11
 
12
- export type TagBaseProps = WithSupportProps<{
13
- /** Коллбэк на удаление */
14
- onDelete?: MouseEventHandler<HTMLButtonElement>;
15
- }> &
16
- CommonTagProps;
12
+ export type TagBaseComponentProps = TagBaseProps & ManageRestrictPopoverProps;
17
13
 
18
14
  export function TagBase({
19
15
  label,
@@ -22,26 +18,52 @@ export function TagBase({
22
18
  onDelete,
23
19
  className,
24
20
  tabIndex,
21
+ changeRestrictTooltipState,
25
22
  ...rest
26
- }: TagBaseProps) {
23
+ }: TagBaseComponentProps) {
24
+ const deleteButtonRef = useRef<HTMLButtonElement>(null);
25
+
26
+ const isRemovable = Boolean(onDelete);
27
+
28
+ const handleMouseEnter = (event: MouseEvent<HTMLButtonElement>) => {
29
+ if (!isRemovable) {
30
+ return;
31
+ }
32
+
33
+ if (deleteButtonRef.current?.contains(event.target as Node)) {
34
+ changeRestrictTooltipState(true);
35
+ }
36
+ };
37
+
38
+ const handleMouseLeave = () => {
39
+ if (!isRemovable) {
40
+ return;
41
+ }
42
+
43
+ changeRestrictTooltipState(false);
44
+ };
45
+
27
46
  return (
28
47
  <span
29
48
  {...extractSupportProps(rest)}
30
49
  className={cn(styles.tag, className)}
31
50
  data-size={size}
32
51
  data-appearance={appearance}
33
- data-removable={Boolean(onDelete)}
52
+ data-removable={isRemovable}
53
+ onMouseEnter={handleMouseEnter}
54
+ onMouseLeave={handleMouseLeave}
34
55
  >
35
- <span title={label} className={styles.label}>
36
- {label}
37
- </span>
38
- {onDelete && (
56
+ <span className={styles.label}>{label}</span>
57
+ {isRemovable && (
39
58
  <button
40
59
  type='button'
41
60
  className={styles.tagButton}
42
61
  onClick={onDelete}
43
62
  data-test-id='tag-remove-button'
44
63
  tabIndex={tabIndex}
64
+ onMouseEnter={handleMouseEnter}
65
+ onMouseLeave={handleMouseLeave}
66
+ ref={deleteButtonRef}
45
67
  >
46
68
  {size === SIZE.Xs ? (
47
69
  <CrossSVG size={ICON_SIZE[size]} className={styles.icon} />
@@ -1,17 +1,12 @@
1
1
  import cn from 'classnames';
2
- import { MouseEvent } from 'react';
3
2
 
4
- import { extractSupportProps, WithSupportProps } from '@snack-uikit/utils';
3
+ import { extractSupportProps } from '@snack-uikit/utils';
5
4
 
6
5
  import { APPEARANCE, SIZE } from '../../constants';
7
- import { CommonTagProps } from '../../types';
6
+ import { ManageRestrictPopoverProps, TagLinkProps } from '../../types';
8
7
  import styles from './styles.module.scss';
9
8
 
10
- export type TagLinkProps = WithSupportProps<CommonTagProps> & {
11
- href: string;
12
- onClick?(e: MouseEvent<HTMLAnchorElement>): void;
13
- target?: HTMLAnchorElement['target'];
14
- };
9
+ export type TagLinkComponentProps = TagLinkProps & ManageRestrictPopoverProps;
15
10
 
16
11
  export function TagLink({
17
12
  label,
@@ -23,7 +18,7 @@ export function TagLink({
23
18
  onClick,
24
19
  target,
25
20
  ...rest
26
- }: TagLinkProps) {
21
+ }: TagLinkComponentProps) {
27
22
  return (
28
23
  <a
29
24
  {...extractSupportProps(rest)}
@@ -35,9 +30,7 @@ export function TagLink({
35
30
  target={target}
36
31
  onClick={onClick}
37
32
  >
38
- <span title={label} className={styles.label}>
39
- {label}
40
- </span>
33
+ <span className={styles.label}>{label}</span>
41
34
  </a>
42
35
  );
43
36
  }
package/src/types.ts CHANGED
@@ -1,6 +1,7 @@
1
- import { MouseEvent } from 'react';
1
+ import { MouseEvent, MouseEventHandler } from 'react';
2
2
 
3
- import { ValueOf } from '@snack-uikit/utils';
3
+ import { TooltipProps } from '@snack-uikit/tooltip';
4
+ import { ValueOf, WithSupportProps } from '@snack-uikit/utils';
4
5
 
5
6
  import { APPEARANCE, SIZE } from './constants';
6
7
 
@@ -36,3 +37,24 @@ export type CommonTagProps = {
36
37
  /** tabIndex кнопки удаления */
37
38
  tabIndex?: number;
38
39
  };
40
+
41
+ export type TagTooltipProps = {
42
+ /** Тултип над тегом */
43
+ tooltip?: TooltipProps;
44
+ };
45
+
46
+ export type TagBaseProps = WithSupportProps<{
47
+ /** Коллбэк на удаление */
48
+ onDelete?: MouseEventHandler<HTMLButtonElement>;
49
+ }> &
50
+ CommonTagProps;
51
+
52
+ export type TagLinkProps = WithSupportProps<CommonTagProps> & {
53
+ href: string;
54
+ onClick?(e: MouseEvent<HTMLAnchorElement>): void;
55
+ target?: HTMLAnchorElement['target'];
56
+ };
57
+
58
+ export type ManageRestrictPopoverProps = {
59
+ changeRestrictTooltipState: (state: boolean) => void;
60
+ };