@cloud-ru/ds-tag 1.2.10 → 1.2.12-preview-7f0ca3f7.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 +4 -0
- package/dist/cjs/helperComponents/TagList/TagList.d.ts +1 -4
- package/dist/cjs/helperComponents/TagList/TagList.js +4 -4
- package/dist/cjs/helperComponents/TagRowSimple/TagRowSimple.d.ts +1 -3
- package/dist/cjs/helperComponents/TagRowSimple/TagRowSimple.js +2 -2
- package/dist/cjs/helperComponents/TagRowTruncated/TagRowTruncated.js +8 -43
- package/dist/cjs/helperComponents/TagRowTruncated/hooks.d.ts +15 -2
- package/dist/cjs/helperComponents/TagRowTruncated/hooks.js +30 -14
- package/dist/cjs/helperComponents/TagRowTruncated/utils.d.ts +1 -2
- package/dist/cjs/helperComponents/TagRowTruncated/utils.js +3 -5
- package/dist/esm/helperComponents/TagList/TagList.d.ts +1 -4
- package/dist/esm/helperComponents/TagList/TagList.js +4 -4
- package/dist/esm/helperComponents/TagRowSimple/TagRowSimple.d.ts +1 -3
- package/dist/esm/helperComponents/TagRowSimple/TagRowSimple.js +2 -2
- package/dist/esm/helperComponents/TagRowTruncated/TagRowTruncated.js +10 -45
- package/dist/esm/helperComponents/TagRowTruncated/hooks.d.ts +15 -2
- package/dist/esm/helperComponents/TagRowTruncated/hooks.js +30 -13
- package/dist/esm/helperComponents/TagRowTruncated/utils.d.ts +1 -2
- package/dist/esm/helperComponents/TagRowTruncated/utils.js +3 -4
- package/dist/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/tsconfig.esm.tsbuildinfo +1 -1
- package/package.json +8 -8
- package/src/helperComponents/TagList/TagList.tsx +4 -9
- package/src/helperComponents/TagRowSimple/TagRowSimple.tsx +2 -4
- package/src/helperComponents/TagRowTruncated/TagRowTruncated.tsx +11 -63
- package/src/helperComponents/TagRowTruncated/hooks.ts +39 -15
- package/src/helperComponents/TagRowTruncated/utils.ts +3 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloud-ru/ds-tag",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.12-preview-7f0ca3f7.0",
|
|
4
4
|
"description": "Пакет компактных меток — компонент Tag (опционально удаляемый или ссылочный) и контейнер TagRow для групп меток с обрезанием по строкам.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -34,13 +34,13 @@
|
|
|
34
34
|
],
|
|
35
35
|
"dependencies": {
|
|
36
36
|
"classnames": "^2.5.1",
|
|
37
|
-
"@cloud-ru/ds-dropdown": "
|
|
38
|
-
"@cloud-ru/ds-icons": "
|
|
39
|
-
"@cloud-ru/ds-
|
|
40
|
-
"@cloud-ru/ds-tooltip": "
|
|
41
|
-
"@cloud-ru/ds-
|
|
42
|
-
"@cloud-ru/ds-truncate-string": "
|
|
43
|
-
"@cloud-ru/ds-utils": "
|
|
37
|
+
"@cloud-ru/ds-dropdown": "1.2.12-preview-7f0ca3f7.0",
|
|
38
|
+
"@cloud-ru/ds-icons": "1.8.2-preview-7f0ca3f7.0",
|
|
39
|
+
"@cloud-ru/ds-scroll": "1.0.11-preview-7f0ca3f7.0",
|
|
40
|
+
"@cloud-ru/ds-tooltip": "1.0.24-preview-7f0ca3f7.0",
|
|
41
|
+
"@cloud-ru/ds-materials": "2.0.7-preview-7f0ca3f7.0",
|
|
42
|
+
"@cloud-ru/ds-truncate-string": "1.1.1-preview-7f0ca3f7.0",
|
|
43
|
+
"@cloud-ru/ds-utils": "1.5.1-preview-7f0ca3f7.0"
|
|
44
44
|
},
|
|
45
45
|
"publishConfig": {
|
|
46
46
|
"access": "public"
|
|
@@ -1,33 +1,28 @@
|
|
|
1
|
-
import { Ref } from 'react';
|
|
2
|
-
|
|
3
1
|
import { Tag } from '../../components/Tag';
|
|
4
2
|
import { SIZE } from '../../constants';
|
|
5
3
|
import { Size, TagRowItemInner } from '../../types';
|
|
6
4
|
import styles from './styles.module.scss';
|
|
7
5
|
|
|
8
|
-
type SetTagRef = (item: TagRowItemInner) => Ref<HTMLDivElement>;
|
|
9
|
-
|
|
10
6
|
type TagListProps = {
|
|
11
7
|
items: TagRowItemInner[];
|
|
12
8
|
size?: Size;
|
|
13
9
|
onItemRemove?(item: string): void;
|
|
14
|
-
setTagRef?: SetTagRef;
|
|
15
10
|
};
|
|
16
11
|
|
|
17
12
|
type OnDeleteHandler = () => void;
|
|
18
13
|
|
|
19
|
-
function renderTag(size: Size, handleRemoveItem?: (item: TagRowItemInner) => OnDeleteHandler
|
|
14
|
+
function renderTag(size: Size, handleRemoveItem?: (item: TagRowItemInner) => OnDeleteHandler) {
|
|
20
15
|
return function TagRowItem(item: TagRowItemInner) {
|
|
21
16
|
return (
|
|
22
|
-
<div key={item.id ?? item.label}
|
|
17
|
+
<div key={item.id ?? item.label} className={styles.tagWrapper}>
|
|
23
18
|
<Tag size={size} {...(item.href ? {} : { onDelete: handleRemoveItem?.(item) })} {...item} />
|
|
24
19
|
</div>
|
|
25
20
|
);
|
|
26
21
|
};
|
|
27
22
|
}
|
|
28
23
|
|
|
29
|
-
export function TagList({ items, size = SIZE.S, onItemRemove
|
|
24
|
+
export function TagList({ items, size = SIZE.S, onItemRemove }: TagListProps) {
|
|
30
25
|
const handleRemoveItem = onItemRemove ? (item: TagRowItemInner) => () => onItemRemove(item.label) : undefined;
|
|
31
26
|
|
|
32
|
-
return <>{items.map(renderTag(size, handleRemoveItem
|
|
27
|
+
return <>{items.map(renderTag(size, handleRemoveItem))}</>;
|
|
33
28
|
}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { extractSupportProps } from '@cloud-ru/ds-utils';
|
|
2
2
|
import cn from 'classnames';
|
|
3
|
-
import { Ref } from 'react';
|
|
4
3
|
|
|
5
4
|
import { Size, TagRowItemInner } from '../../types';
|
|
6
5
|
import { TagList } from '../TagList';
|
|
@@ -10,14 +9,13 @@ type TagRowSimpleProps = {
|
|
|
10
9
|
items: TagRowItemInner[];
|
|
11
10
|
size: Size;
|
|
12
11
|
onItemRemove?(item: string): void;
|
|
13
|
-
setTagRef?: (item: TagRowItemInner) => Ref<HTMLDivElement>;
|
|
14
12
|
className?: string;
|
|
15
13
|
};
|
|
16
14
|
|
|
17
|
-
export function TagRowSimple({ items, size, onItemRemove,
|
|
15
|
+
export function TagRowSimple({ items, size, onItemRemove, className, ...rest }: TagRowSimpleProps) {
|
|
18
16
|
return (
|
|
19
17
|
<div className={cn(styles.tagRowSimpleWrapper, className)} {...extractSupportProps(rest)} data-size={size}>
|
|
20
|
-
<TagList items={items} size={size} onItemRemove={onItemRemove}
|
|
18
|
+
<TagList items={items} size={size} onItemRemove={onItemRemove} />
|
|
21
19
|
</div>
|
|
22
20
|
);
|
|
23
21
|
}
|
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import { extractSupportProps } from '@cloud-ru/ds-utils';
|
|
2
2
|
import cn from 'classnames';
|
|
3
|
-
import { memo,
|
|
3
|
+
import { memo, useMemo } from 'react';
|
|
4
4
|
|
|
5
5
|
import { TEST_IDS } from '../../constants';
|
|
6
6
|
import { Size, TagRowItemInner } from '../../types';
|
|
7
7
|
import { TagList } from '../TagList';
|
|
8
8
|
import { TagMore } from '../TagMore';
|
|
9
|
-
import {
|
|
9
|
+
import { useTagRowMeasurements } from './hooks';
|
|
10
10
|
import styles from './styles.module.scss';
|
|
11
|
-
import { getGapWidth } from './utils';
|
|
12
11
|
|
|
13
12
|
type TagRowTruncatedProps = {
|
|
14
13
|
items: TagRowItemInner[];
|
|
@@ -19,16 +18,6 @@ type TagRowTruncatedProps = {
|
|
|
19
18
|
onItemRemove?(item: string): void;
|
|
20
19
|
};
|
|
21
20
|
|
|
22
|
-
function setTagElement(tag: TagRowItemInner, tagsMapRef: MutableRefObject<Map<TagRowItemInner, HTMLElement | null>>) {
|
|
23
|
-
return (tagElement: HTMLElement | null) => {
|
|
24
|
-
if (tagElement === null) {
|
|
25
|
-
tagsMapRef.current.delete(tag);
|
|
26
|
-
} else {
|
|
27
|
-
tagsMapRef.current.set(tag, tagElement);
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
|
|
32
21
|
function TagRowTruncatedInner({
|
|
33
22
|
items,
|
|
34
23
|
rowLimit,
|
|
@@ -38,50 +27,12 @@ function TagRowTruncatedInner({
|
|
|
38
27
|
onItemRemove,
|
|
39
28
|
...rest
|
|
40
29
|
}: TagRowTruncatedProps) {
|
|
41
|
-
const
|
|
42
|
-
const [
|
|
43
|
-
|
|
44
|
-
const hiddenRowElementRef = useRef<HTMLDivElement>(null);
|
|
45
|
-
const hiddenMoreButtonRef = useRef<HTMLButtonElement>(null);
|
|
46
|
-
|
|
47
|
-
const tagsMapRef = useRef(new Map<TagRowItemInner, HTMLElement | null>());
|
|
48
|
-
|
|
49
|
-
const { width: maxWidth } = useResizeObserver(hiddenRowElementRef.current);
|
|
50
|
-
|
|
51
|
-
// Ширины тегов доезжают позже монтирования (шрифты, вложенные измерения), поэтому
|
|
52
|
-
// пересчитываем упаковку, пока они меняются, а не по одному первому тегу.
|
|
53
|
-
const [measuredWidths, setMeasuredWidths] = useState('');
|
|
54
|
-
|
|
55
|
-
useEffect(() => {
|
|
56
|
-
const observer = new ResizeObserver(() =>
|
|
57
|
-
setMeasuredWidths([...tagsMapRef.current.values()].map(element => element?.offsetWidth ?? 0).join()),
|
|
58
|
-
);
|
|
59
|
-
|
|
60
|
-
tagsMapRef.current.forEach(element => element && observer.observe(element));
|
|
61
|
-
|
|
62
|
-
if (hiddenMoreButtonRef.current) {
|
|
63
|
-
observer.observe(hiddenMoreButtonRef.current);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
return () => observer.disconnect();
|
|
67
|
-
}, [items]);
|
|
68
|
-
|
|
69
|
-
const gapWidth = getGapWidth(hiddenRowElementRef);
|
|
70
|
-
|
|
71
|
-
useEffect(() => {
|
|
72
|
-
if (maxWidth < 1) {
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
|
|
30
|
+
const { rowRef, buttonRef, measurements } = useTagRowMeasurements(items, size);
|
|
31
|
+
const [visibleTags, hiddenTags] = useMemo(() => {
|
|
76
32
|
const newVisibleTags: TagRowItemInner[] = [];
|
|
77
33
|
const newHiddenTags: TagRowItemInner[] = [];
|
|
78
|
-
|
|
79
|
-
const moreButtonWidth
|
|
80
|
-
|
|
81
|
-
const measuredTags = [...tagsMapRef.current].map(([item, element]) => ({
|
|
82
|
-
item,
|
|
83
|
-
width: element?.offsetWidth || 0,
|
|
84
|
-
}));
|
|
34
|
+
if (!measurements) return [newVisibleTags, newHiddenTags];
|
|
35
|
+
const { maxWidth, moreButtonWidth, gapWidth, measuredTags } = measurements;
|
|
85
36
|
|
|
86
37
|
let rowWidth = 0;
|
|
87
38
|
let rowCount = 1;
|
|
@@ -129,19 +80,16 @@ function TagRowTruncatedInner({
|
|
|
129
80
|
newVisibleTags.push(item);
|
|
130
81
|
});
|
|
131
82
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
}, [items, rowLimit, maxWidth, gapWidth, measuredWidths]);
|
|
135
|
-
|
|
136
|
-
const setTagRef = (item: TagRowItemInner) => setTagElement(item, tagsMapRef);
|
|
83
|
+
return [newVisibleTags, newHiddenTags];
|
|
84
|
+
}, [measurements, rowLimit]);
|
|
137
85
|
|
|
138
86
|
return (
|
|
139
87
|
<div className={cn(styles.wrapper, className)} {...extractSupportProps(rest)} data-size={size}>
|
|
140
|
-
<div className={styles.hiddenRow} ref={
|
|
141
|
-
<TagList items={items} size={size} onItemRemove={onItemRemove}
|
|
88
|
+
<div className={styles.hiddenRow} ref={rowRef} data-size={size}>
|
|
89
|
+
<TagList items={items} size={size} onItemRemove={onItemRemove} />
|
|
142
90
|
</div>
|
|
143
91
|
<div className={styles.hiddenMoreButton}>
|
|
144
|
-
<TagMore items={items} text={moreButtonLabel} size={size} buttonRef={
|
|
92
|
+
<TagMore items={items} text={moreButtonLabel} size={size} buttonRef={buttonRef} />
|
|
145
93
|
</div>
|
|
146
94
|
<div className={styles.visibleRow} data-size={size} data-test-id={TEST_IDS.tagRow.visibleTagsWrapper}>
|
|
147
95
|
<TagList items={visibleTags} size={size} onItemRemove={onItemRemove} />
|
|
@@ -1,23 +1,47 @@
|
|
|
1
|
-
import { useEffect, useState } from 'react';
|
|
1
|
+
import { useEffect, useRef, useState } from 'react';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
import { Size, TagRowItemInner } from '../../types';
|
|
4
|
+
import { getGapWidth } from './utils';
|
|
5
|
+
|
|
6
|
+
type TagRowMeasurements = {
|
|
7
|
+
maxWidth: number;
|
|
8
|
+
moreButtonWidth: number;
|
|
9
|
+
gapWidth: number;
|
|
10
|
+
measuredTags: { item: TagRowItemInner; width: number }[];
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export function useTagRowMeasurements(items: TagRowItemInner[], size: Size) {
|
|
14
|
+
const rowRef = useRef<HTMLDivElement>(null);
|
|
15
|
+
const buttonRef = useRef<HTMLButtonElement>(null);
|
|
16
|
+
const [measurements, setMeasurements] = useState<TagRowMeasurements>();
|
|
5
17
|
|
|
6
18
|
useEffect(() => {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
19
|
+
const row = rowRef.current;
|
|
20
|
+
const button = buttonRef.current;
|
|
21
|
+
if (!row || !button) return;
|
|
10
22
|
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
23
|
+
const tags = Array.from(row.children);
|
|
24
|
+
const elements = [row, button, ...tags];
|
|
25
|
+
const widths = new Map<Element, number>();
|
|
26
|
+
const observer = new ResizeObserver(entries => {
|
|
27
|
+
for (const entry of entries) {
|
|
28
|
+
widths.set(entry.target, entry.borderBoxSize[0].inlineSize);
|
|
14
29
|
}
|
|
15
|
-
|
|
30
|
+
const maxWidth = widths.get(row);
|
|
31
|
+
const moreButtonWidth = widths.get(button);
|
|
32
|
+
if (!maxWidth || moreButtonWidth === undefined || widths.size < elements.length) return;
|
|
16
33
|
|
|
17
|
-
|
|
34
|
+
setMeasurements({
|
|
35
|
+
maxWidth,
|
|
36
|
+
moreButtonWidth,
|
|
37
|
+
gapWidth: getGapWidth(row),
|
|
38
|
+
measuredTags: items.map((item, index) => ({ item, width: widths.get(tags[index]) ?? 0 })),
|
|
39
|
+
});
|
|
40
|
+
});
|
|
18
41
|
|
|
19
|
-
|
|
20
|
-
|
|
42
|
+
elements.forEach(element => observer.observe(element, { box: 'border-box' }));
|
|
43
|
+
return () => observer.disconnect();
|
|
44
|
+
}, [items, size]);
|
|
21
45
|
|
|
22
|
-
return {
|
|
23
|
-
}
|
|
46
|
+
return { rowRef, buttonRef, measurements };
|
|
47
|
+
}
|
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const cssValue = elementRef.current ? getComputedStyle(elementRef.current, null).getPropertyValue('gap') : '0px';
|
|
5
|
-
|
|
6
|
-
return parseInt(cssValue, 10) || 0;
|
|
7
|
-
};
|
|
1
|
+
export function getGapWidth(row: HTMLElement): number {
|
|
2
|
+
return parseFloat(getComputedStyle(row).columnGap) || 0;
|
|
3
|
+
}
|