@cloud-ru/ds-fields 2.5.9-preview-b543ec31.0 → 3.0.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 +6 -0
- package/README.md +2 -1
- package/dist/cjs/components/FieldSelect/FieldSelect.js +136 -65
- package/dist/cjs/components/FieldSelect/constants.d.ts +15 -0
- package/dist/cjs/components/FieldSelect/constants.js +32 -1
- package/dist/cjs/components/FieldSelect/hooks.d.ts +32 -0
- package/dist/cjs/components/FieldSelect/hooks.js +94 -0
- package/dist/cjs/components/FieldSelect/types.d.ts +24 -8
- package/dist/cjs/components/FieldSelect/utils/customOption.d.ts +20 -0
- package/dist/cjs/components/FieldSelect/utils/customOption.js +48 -0
- package/dist/cjs/components/FieldSelect/utils/extract.d.ts +11 -0
- package/dist/cjs/components/FieldSelect/utils/extract.js +39 -0
- package/dist/cjs/components/FieldSelect/utils/filter.d.ts +3 -0
- package/dist/cjs/components/FieldSelect/utils/filter.js +40 -0
- package/dist/cjs/components/FieldSelect/utils/index.d.ts +6 -0
- package/dist/cjs/components/FieldSelect/utils/index.js +22 -0
- package/dist/cjs/components/FieldSelect/utils/items.d.ts +9 -0
- package/dist/cjs/components/FieldSelect/utils/items.js +47 -0
- package/dist/cjs/components/FieldSelect/utils/selection.d.ts +11 -0
- package/dist/cjs/components/FieldSelect/utils/selection.js +18 -0
- package/dist/cjs/components/FieldSelect/utils/tagSize.d.ts +4 -0
- package/dist/cjs/components/FieldSelect/utils/tagSize.js +5 -0
- package/dist/esm/components/FieldSelect/FieldSelect.js +138 -67
- package/dist/esm/components/FieldSelect/constants.d.ts +15 -0
- package/dist/esm/components/FieldSelect/constants.js +31 -0
- package/dist/esm/components/FieldSelect/hooks.d.ts +32 -0
- package/dist/esm/components/FieldSelect/hooks.js +91 -0
- package/dist/esm/components/FieldSelect/types.d.ts +24 -8
- package/dist/esm/components/FieldSelect/utils/customOption.d.ts +20 -0
- package/dist/esm/components/FieldSelect/utils/customOption.js +41 -0
- package/dist/esm/components/FieldSelect/utils/extract.d.ts +11 -0
- package/dist/esm/components/FieldSelect/utils/extract.js +34 -0
- package/dist/esm/components/FieldSelect/utils/filter.d.ts +3 -0
- package/dist/esm/components/FieldSelect/utils/filter.js +36 -0
- package/dist/esm/components/FieldSelect/utils/index.d.ts +6 -0
- package/dist/esm/components/FieldSelect/utils/index.js +6 -0
- package/dist/esm/components/FieldSelect/utils/items.d.ts +9 -0
- package/dist/esm/components/FieldSelect/utils/items.js +41 -0
- package/dist/esm/components/FieldSelect/utils/selection.d.ts +11 -0
- package/dist/esm/components/FieldSelect/utils/selection.js +14 -0
- package/dist/esm/components/FieldSelect/utils/tagSize.d.ts +4 -0
- package/dist/esm/components/FieldSelect/utils/tagSize.js +2 -0
- package/dist/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/tsconfig.esm.tsbuildinfo +1 -1
- package/package.json +20 -20
- package/src/components/FieldSelect/FieldSelect.tsx +190 -82
- package/src/components/FieldSelect/constants.ts +36 -0
- package/src/components/FieldSelect/hooks.ts +170 -0
- package/src/components/FieldSelect/types.ts +80 -53
- package/src/components/FieldSelect/utils/customOption.ts +69 -0
- package/src/components/FieldSelect/utils/extract.ts +49 -0
- package/src/components/FieldSelect/utils/filter.ts +51 -0
- package/src/components/FieldSelect/utils/index.ts +6 -0
- package/src/components/FieldSelect/utils/items.ts +58 -0
- package/src/components/FieldSelect/utils/selection.ts +25 -0
- package/src/components/FieldSelect/utils/tagSize.ts +5 -0
- package/dist/cjs/components/FieldSelect/utils.d.ts +0 -19
- package/dist/cjs/components/FieldSelect/utils.js +0 -103
- package/dist/esm/components/FieldSelect/utils.d.ts +0 -19
- package/dist/esm/components/FieldSelect/utils.js +0 -92
- package/src/components/FieldSelect/utils.ts +0 -132
|
@@ -4,9 +4,30 @@ import { Appearance as TagAppearance } from '@cloud-ru/ds-tag';
|
|
|
4
4
|
import { ValueOf } from '@cloud-ru/ds-utils';
|
|
5
5
|
import { FocusEvent, KeyboardEvent, ReactNode } from 'react';
|
|
6
6
|
import { FieldLayoutPresets } from '../../hooks';
|
|
7
|
-
import { SELECTION_MODE } from './constants';
|
|
7
|
+
import { ADD_CUSTOM_OPTION_TRIGGER, ADD_CUSTOM_OPTION_TRIGGERS_MULTIPLE_ALLOWED, ADD_CUSTOM_OPTION_TRIGGERS_SINGLE_ALLOWED, SELECTION_MODE } from './constants';
|
|
8
8
|
/** Режим выбора FieldSelect: одно значение или несколько. */
|
|
9
9
|
export type Selection = ValueOf<typeof SELECTION_MODE>;
|
|
10
|
+
/** Событие, по которому произвольный ввод фиксируется как выбранная опция. */
|
|
11
|
+
export type FieldSelectAddCustomOptionTrigger = ValueOf<typeof ADD_CUSTOM_OPTION_TRIGGER>;
|
|
12
|
+
/** Триггеры кастомной опции в режиме `single`. */
|
|
13
|
+
export type FieldSelectSingleAddCustomOptionTrigger = (typeof ADD_CUSTOM_OPTION_TRIGGERS_SINGLE_ALLOWED)[number];
|
|
14
|
+
/** Триггеры кастомной опции в режиме `multiple`. */
|
|
15
|
+
export type FieldSelectMultipleAddCustomOptionTrigger = (typeof ADD_CUSTOM_OPTION_TRIGGERS_MULTIPLE_ALLOWED)[number];
|
|
16
|
+
type CustomOptionProps<CustomOptionTrigger extends FieldSelectAddCustomOptionTrigger> = {
|
|
17
|
+
/**
|
|
18
|
+
* Разрешить фиксировать произвольный ввод из строки поиска как выбранную опцию
|
|
19
|
+
* (значение, которого нет в `items`).
|
|
20
|
+
* @default false
|
|
21
|
+
*/
|
|
22
|
+
allowCustomOption?: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* События, по которым произвольный ввод фиксируется как выбранная опция.
|
|
25
|
+
* Учитывается только при `allowCustomOption={true}`.
|
|
26
|
+
* Если не задан — используются все триггеры режима (`single`: enter, blur; `multiple`: enter, blur, space, comma).
|
|
27
|
+
* Триггеры, недоступные в текущем `selection`, игнорируются.
|
|
28
|
+
*/
|
|
29
|
+
addCustomOptionTriggers?: CustomOptionTrigger[];
|
|
30
|
+
};
|
|
10
31
|
type WithTagAppearance<T> = T extends {
|
|
11
32
|
items: ItemProps[];
|
|
12
33
|
} ? Omit<T, 'items'> & {
|
|
@@ -96,11 +117,6 @@ type CommonSelectProps = FieldSelectDecoratorProps & DroplistPassthrough & {
|
|
|
96
117
|
* @default false
|
|
97
118
|
*/
|
|
98
119
|
autocomplete?: boolean;
|
|
99
|
-
/**
|
|
100
|
-
* Зафиксировать введённый текст как новый выбор по `Enter` (создание опции «на лету»).
|
|
101
|
-
* @default false
|
|
102
|
-
*/
|
|
103
|
-
addOptionByEnter?: boolean;
|
|
104
120
|
/**
|
|
105
121
|
* Сбрасывать строку поиска к выбранному значению после выбора. `false` нужен при асинхронной
|
|
106
122
|
* подгрузке (оставить введённый запрос как значение, пока данные не пришли).
|
|
@@ -154,7 +170,7 @@ type CommonSelectProps = FieldSelectDecoratorProps & DroplistPassthrough & {
|
|
|
154
170
|
/** Тестовый id корня */
|
|
155
171
|
'data-test-id'?: string;
|
|
156
172
|
};
|
|
157
|
-
export type FieldSelectSingleProps = CommonSelectProps & {
|
|
173
|
+
export type FieldSelectSingleProps = CommonSelectProps & CustomOptionProps<FieldSelectSingleAddCustomOptionTrigger> & {
|
|
158
174
|
/** Режим выбора. По умолчанию `'single'`. */
|
|
159
175
|
selection?: typeof SELECTION_MODE.Single;
|
|
160
176
|
/** Управляемое значение. Пустая строка трактуется как «значение не выбрано». */
|
|
@@ -169,7 +185,7 @@ export type FieldSelectSingleProps = CommonSelectProps & {
|
|
|
169
185
|
*/
|
|
170
186
|
closeDroplistOnItemClick?: boolean;
|
|
171
187
|
};
|
|
172
|
-
export type FieldSelectMultipleProps = CommonSelectProps & {
|
|
188
|
+
export type FieldSelectMultipleProps = CommonSelectProps & CustomOptionProps<FieldSelectMultipleAddCustomOptionTrigger> & {
|
|
173
189
|
/** Режим выбора */
|
|
174
190
|
selection: typeof SELECTION_MODE.Multiple;
|
|
175
191
|
/** Управляемые значения */
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { FieldSelectAddCustomOptionTrigger, FieldSelectMultipleAddCustomOptionTrigger } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Сопоставляет клавишу с триггером кастомной опции (`enter` / `space` / `comma`).
|
|
4
|
+
* Сначала `KeyboardEvent.code`, затем `key` — если `code` пустой или не из словаря (`','` без `Comma`).
|
|
5
|
+
*/
|
|
6
|
+
export declare function getCustomOptionTriggerByKey(key: string): FieldSelectMultipleAddCustomOptionTrigger | undefined;
|
|
7
|
+
export declare function shouldHandleCustomOptionTrigger(trigger: FieldSelectAddCustomOptionTrigger | undefined, availableTriggers: readonly FieldSelectAddCustomOptionTrigger[]): trigger is FieldSelectAddCustomOptionTrigger;
|
|
8
|
+
export declare function resolveAddCustomOptionTriggers(params: {
|
|
9
|
+
allowCustomOption: boolean;
|
|
10
|
+
addCustomOptionTriggers: readonly FieldSelectAddCustomOptionTrigger[] | undefined;
|
|
11
|
+
multiple?: boolean;
|
|
12
|
+
}): readonly FieldSelectAddCustomOptionTrigger[];
|
|
13
|
+
/** Фокус ушёл на дроплист, футер или postfix — кастомную опцию на blur фиксировать не нужно. */
|
|
14
|
+
export declare function isCustomOptionBlurInsideField(relatedTarget: EventTarget | null, nodes: (Node | null | undefined)[]): boolean;
|
|
15
|
+
/** Клик снаружи закрывает список, не снимая фокус с input — blur не придёт. */
|
|
16
|
+
export declare function shouldCommitCustomOptionOnClose(params: {
|
|
17
|
+
skip: boolean;
|
|
18
|
+
inputElement: Element | null;
|
|
19
|
+
activeElement: Element | null;
|
|
20
|
+
}): boolean;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getCustomOptionTriggerByKey = getCustomOptionTriggerByKey;
|
|
4
|
+
exports.shouldHandleCustomOptionTrigger = shouldHandleCustomOptionTrigger;
|
|
5
|
+
exports.resolveAddCustomOptionTriggers = resolveAddCustomOptionTriggers;
|
|
6
|
+
exports.isCustomOptionBlurInsideField = isCustomOptionBlurInsideField;
|
|
7
|
+
exports.shouldCommitCustomOptionOnClose = shouldCommitCustomOptionOnClose;
|
|
8
|
+
const constants_1 = require("../constants");
|
|
9
|
+
/**
|
|
10
|
+
* Сопоставляет клавишу с триггером кастомной опции (`enter` / `space` / `comma`).
|
|
11
|
+
* Сначала `KeyboardEvent.code`, затем `key` — если `code` пустой или не из словаря (`','` без `Comma`).
|
|
12
|
+
*/
|
|
13
|
+
function getCustomOptionTriggerByKey(key) {
|
|
14
|
+
switch (key) {
|
|
15
|
+
case 'Enter':
|
|
16
|
+
return constants_1.ADD_CUSTOM_OPTION_TRIGGER.Enter;
|
|
17
|
+
case ' ':
|
|
18
|
+
return constants_1.ADD_CUSTOM_OPTION_TRIGGER.Space;
|
|
19
|
+
case ',':
|
|
20
|
+
return constants_1.ADD_CUSTOM_OPTION_TRIGGER.Comma;
|
|
21
|
+
default:
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
function shouldHandleCustomOptionTrigger(trigger, availableTriggers) {
|
|
26
|
+
return trigger !== undefined && availableTriggers.includes(trigger);
|
|
27
|
+
}
|
|
28
|
+
function resolveAddCustomOptionTriggers(params) {
|
|
29
|
+
if (params.allowCustomOption) {
|
|
30
|
+
const defaultTriggers = params.multiple
|
|
31
|
+
? constants_1.ADD_CUSTOM_OPTION_TRIGGERS_MULTIPLE_DEFAULT
|
|
32
|
+
: constants_1.ADD_CUSTOM_OPTION_TRIGGERS_SINGLE_DEFAULT;
|
|
33
|
+
const allowedTriggers = params.multiple
|
|
34
|
+
? constants_1.ADD_CUSTOM_OPTION_TRIGGERS_MULTIPLE_ALLOWED
|
|
35
|
+
: constants_1.ADD_CUSTOM_OPTION_TRIGGERS_SINGLE_ALLOWED;
|
|
36
|
+
const availableTriggers = params.addCustomOptionTriggers ?? defaultTriggers;
|
|
37
|
+
return availableTriggers.filter(trigger => allowedTriggers.includes(trigger));
|
|
38
|
+
}
|
|
39
|
+
return [];
|
|
40
|
+
}
|
|
41
|
+
/** Фокус ушёл на дроплист, футер или postfix — кастомную опцию на blur фиксировать не нужно. */
|
|
42
|
+
function isCustomOptionBlurInsideField(relatedTarget, nodes) {
|
|
43
|
+
return relatedTarget instanceof Node && nodes.some(node => Boolean(node?.contains(relatedTarget)));
|
|
44
|
+
}
|
|
45
|
+
/** Клик снаружи закрывает список, не снимая фокус с input — blur не придёт. */
|
|
46
|
+
function shouldCommitCustomOptionOnClose(params) {
|
|
47
|
+
return !params.skip && params.activeElement !== null && params.activeElement === params.inputElement;
|
|
48
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ItemId } from '@cloud-ru/ds-list';
|
|
2
|
+
import { Appearance as TagAppearance } from '@cloud-ru/ds-tag';
|
|
3
|
+
export type WithIdContent = {
|
|
4
|
+
id?: ItemId;
|
|
5
|
+
content?: unknown;
|
|
6
|
+
disabled?: boolean;
|
|
7
|
+
appearance?: TagAppearance;
|
|
8
|
+
};
|
|
9
|
+
export declare function extractLabel(item: WithIdContent): string;
|
|
10
|
+
export declare function extractSearchText(item: WithIdContent): string;
|
|
11
|
+
export declare function extractAppearance(item?: WithIdContent): TagAppearance | undefined;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.extractLabel = extractLabel;
|
|
4
|
+
exports.extractSearchText = extractSearchText;
|
|
5
|
+
exports.extractAppearance = extractAppearance;
|
|
6
|
+
function extractLabel(item) {
|
|
7
|
+
const { content, id } = item;
|
|
8
|
+
if (!content) {
|
|
9
|
+
return String(id ?? '');
|
|
10
|
+
}
|
|
11
|
+
if (typeof content === 'string' || typeof content === 'number') {
|
|
12
|
+
return String(content);
|
|
13
|
+
}
|
|
14
|
+
if (typeof content === 'object' && content !== null && 'label' in content) {
|
|
15
|
+
return String(content.label);
|
|
16
|
+
}
|
|
17
|
+
return String(id ?? '');
|
|
18
|
+
}
|
|
19
|
+
// Текст для поиска: лейбл (option) + caption + description — паритет с легаси `useSearch`,
|
|
20
|
+
// который матчил запрос по всем трём полям контента, а не только по лейблу.
|
|
21
|
+
function extractSearchText(item) {
|
|
22
|
+
const parts = [extractLabel(item)];
|
|
23
|
+
const { content } = item;
|
|
24
|
+
if (content && typeof content === 'object') {
|
|
25
|
+
const c = content;
|
|
26
|
+
if (typeof c.caption === 'string') {
|
|
27
|
+
parts.push(c.caption);
|
|
28
|
+
}
|
|
29
|
+
if (typeof c.description === 'string') {
|
|
30
|
+
parts.push(c.description);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return parts.join(' ');
|
|
34
|
+
}
|
|
35
|
+
// Цвет тега выбранного значения (multiple) — паритет с легаси `option.appearance`.
|
|
36
|
+
// Проверка типа нужна и при типизированном поле: items часто приходят из ответа бэкенда.
|
|
37
|
+
function extractAppearance(item) {
|
|
38
|
+
return typeof item?.appearance === 'string' ? item.appearance : undefined;
|
|
39
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isFuzzyMatch = isFuzzyMatch;
|
|
4
|
+
exports.filterItems = filterItems;
|
|
5
|
+
const extract_1 = require("./extract");
|
|
6
|
+
// Subsequence-fuzzy: символы запроса должны встречаться в строке в исходном порядке.
|
|
7
|
+
// Пример: query=`lge` matches `Large` (L → r → arg → e).
|
|
8
|
+
function isFuzzyMatch(haystack, needle) {
|
|
9
|
+
if (!needle) {
|
|
10
|
+
return true;
|
|
11
|
+
}
|
|
12
|
+
let i = 0;
|
|
13
|
+
for (const ch of haystack) {
|
|
14
|
+
if (ch === needle[i]) {
|
|
15
|
+
i += 1;
|
|
16
|
+
if (i === needle.length) {
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
function filterItems(items, query, fuzzy) {
|
|
24
|
+
if (!query) {
|
|
25
|
+
return items;
|
|
26
|
+
}
|
|
27
|
+
const q = query.toLowerCase();
|
|
28
|
+
const matches = (item) => {
|
|
29
|
+
const text = (0, extract_1.extractSearchText)(item).toLowerCase();
|
|
30
|
+
return fuzzy ? isFuzzyMatch(text, q) : text.includes(q);
|
|
31
|
+
};
|
|
32
|
+
const walk = (list) => list.flatMap(item => {
|
|
33
|
+
if ('items' in item && Array.isArray(item.items)) {
|
|
34
|
+
const filteredChildren = walk(item.items);
|
|
35
|
+
return filteredChildren.length > 0 ? [{ ...item, items: filteredChildren }] : [];
|
|
36
|
+
}
|
|
37
|
+
return matches(item) ? [item] : [];
|
|
38
|
+
});
|
|
39
|
+
return walk(items);
|
|
40
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./customOption"), exports);
|
|
18
|
+
__exportStar(require("./extract"), exports);
|
|
19
|
+
__exportStar(require("./filter"), exports);
|
|
20
|
+
__exportStar(require("./items"), exports);
|
|
21
|
+
__exportStar(require("./selection"), exports);
|
|
22
|
+
__exportStar(require("./tagSize"), exports);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ItemId } from '@cloud-ru/ds-list';
|
|
2
|
+
import { FieldSelectItem } from '../types';
|
|
3
|
+
import { WithIdContent } from './extract';
|
|
4
|
+
export declare function flatten(items: FieldSelectItem[]): WithIdContent[];
|
|
5
|
+
export declare function findItem(items: FieldSelectItem[], id: ItemId): WithIdContent | undefined;
|
|
6
|
+
/** Айтем для выбранного значения, которого нет в `items` (кастомная опция, ленивая подгрузка). */
|
|
7
|
+
export declare function createPlaceholderItem(id: ItemId): FieldSelectItem;
|
|
8
|
+
/** Выбранные id, которых нет в дереве айтемов — в дроплист их нужно добавить отдельно. */
|
|
9
|
+
export declare function getMissingSelectedItems(selectedIds: ItemId[], allItems: FieldSelectItem[]): FieldSelectItem[];
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.flatten = flatten;
|
|
4
|
+
exports.findItem = findItem;
|
|
5
|
+
exports.createPlaceholderItem = createPlaceholderItem;
|
|
6
|
+
exports.getMissingSelectedItems = getMissingSelectedItems;
|
|
7
|
+
function flatten(items) {
|
|
8
|
+
const out = [];
|
|
9
|
+
for (const item of items) {
|
|
10
|
+
out.push(item);
|
|
11
|
+
if ('items' in item && Array.isArray(item.items)) {
|
|
12
|
+
out.push(...flatten(item.items));
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
return out;
|
|
16
|
+
}
|
|
17
|
+
function findItem(items, id) {
|
|
18
|
+
for (const item of flatten(items)) {
|
|
19
|
+
if (item.id === id) {
|
|
20
|
+
return item;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
/** Айтем для выбранного значения, которого нет в `items` (кастомная опция, ленивая подгрузка). */
|
|
26
|
+
function createPlaceholderItem(id) {
|
|
27
|
+
return { id, content: { label: String(id) } };
|
|
28
|
+
}
|
|
29
|
+
/** Выбранные id, которых нет в дереве айтемов — в дроплист их нужно добавить отдельно. */
|
|
30
|
+
function getMissingSelectedItems(selectedIds, allItems) {
|
|
31
|
+
if (selectedIds.length === 0) {
|
|
32
|
+
return [];
|
|
33
|
+
}
|
|
34
|
+
const existingIds = new Set();
|
|
35
|
+
for (const item of flatten(allItems)) {
|
|
36
|
+
if (item.id !== undefined) {
|
|
37
|
+
existingIds.add(item.id);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const missing = [];
|
|
41
|
+
for (const id of selectedIds) {
|
|
42
|
+
if (id !== '' && !existingIds.has(id)) {
|
|
43
|
+
missing.push(createPlaceholderItem(id));
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return missing;
|
|
47
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ItemId } from '@cloud-ru/ds-list';
|
|
2
|
+
import { FieldSelectMultipleProps, Selection } from '../types';
|
|
3
|
+
export declare function isMultiple(props: {
|
|
4
|
+
selection?: Selection;
|
|
5
|
+
}): props is FieldSelectMultipleProps;
|
|
6
|
+
/** Выбранные id в нормализованном виде: массив и для `single`, и для `multiple`. */
|
|
7
|
+
export declare function getSelectedIds(params: {
|
|
8
|
+
multiple: boolean;
|
|
9
|
+
multipleValue: ItemId[];
|
|
10
|
+
singleValue: ItemId | undefined;
|
|
11
|
+
}): ItemId[];
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isMultiple = isMultiple;
|
|
4
|
+
exports.getSelectedIds = getSelectedIds;
|
|
5
|
+
const constants_1 = require("../constants");
|
|
6
|
+
function isMultiple(props) {
|
|
7
|
+
return props.selection === constants_1.SELECTION_MODE.Multiple;
|
|
8
|
+
}
|
|
9
|
+
/** Выбранные id в нормализованном виде: массив и для `single`, и для `multiple`. */
|
|
10
|
+
function getSelectedIds(params) {
|
|
11
|
+
if (params.multiple) {
|
|
12
|
+
return params.multipleValue;
|
|
13
|
+
}
|
|
14
|
+
if (params.singleValue === undefined) {
|
|
15
|
+
return [];
|
|
16
|
+
}
|
|
17
|
+
return [params.singleValue];
|
|
18
|
+
}
|