1
This commit is contained in:
+18
@@ -0,0 +1,18 @@
|
||||
import type { DefaultOptionType, FieldNames, InternalFieldNames, InternalValueType, SingleValueType } from '../Cascader';
|
||||
export declare const VALUE_SPLIT = "__RC_CASCADER_SPLIT__";
|
||||
export declare const SHOW_PARENT = "SHOW_PARENT";
|
||||
export declare const SHOW_CHILD = "SHOW_CHILD";
|
||||
/**
|
||||
* Will convert value to string, and join with `VALUE_SPLIT`
|
||||
*/
|
||||
export declare function toPathKey(value: SingleValueType): string;
|
||||
/**
|
||||
* Batch convert value to string, and join with `VALUE_SPLIT`
|
||||
*/
|
||||
export declare function toPathKeys(value: SingleValueType[]): string[];
|
||||
export declare function toPathValueStr(pathKey: string): string[];
|
||||
export declare function fillFieldNames(fieldNames?: FieldNames): InternalFieldNames;
|
||||
export declare function isLeaf(option: DefaultOptionType, fieldNames: FieldNames): any;
|
||||
export declare function scrollIntoParentView(element: HTMLElement): void;
|
||||
export declare function getFullPathKeys(options: DefaultOptionType[], fieldNames: FieldNames): any[];
|
||||
export declare function toRawValues(value?: InternalValueType): SingleValueType[];
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
import { SEARCH_MARK } from "../hooks/useSearchOptions";
|
||||
export const VALUE_SPLIT = '__RC_CASCADER_SPLIT__';
|
||||
export const SHOW_PARENT = 'SHOW_PARENT';
|
||||
export const SHOW_CHILD = 'SHOW_CHILD';
|
||||
|
||||
/**
|
||||
* Will convert value to string, and join with `VALUE_SPLIT`
|
||||
*/
|
||||
export function toPathKey(value) {
|
||||
return value.join(VALUE_SPLIT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Batch convert value to string, and join with `VALUE_SPLIT`
|
||||
*/
|
||||
export function toPathKeys(value) {
|
||||
return value.map(toPathKey);
|
||||
}
|
||||
export function toPathValueStr(pathKey) {
|
||||
return pathKey.split(VALUE_SPLIT);
|
||||
}
|
||||
export function fillFieldNames(fieldNames) {
|
||||
const {
|
||||
label,
|
||||
value,
|
||||
children
|
||||
} = fieldNames || {};
|
||||
const val = value || 'value';
|
||||
return {
|
||||
label: label || 'label',
|
||||
value: val,
|
||||
key: val,
|
||||
children: children || 'children'
|
||||
};
|
||||
}
|
||||
export function isLeaf(option, fieldNames) {
|
||||
return option.isLeaf ?? !option[fieldNames.children]?.length;
|
||||
}
|
||||
export function scrollIntoParentView(element) {
|
||||
const parent = element.parentElement;
|
||||
if (!parent) {
|
||||
return;
|
||||
}
|
||||
const elementToParent = element.offsetTop - parent.offsetTop; // offsetParent may not be parent.
|
||||
if (elementToParent - parent.scrollTop < 0) {
|
||||
parent.scrollTo({
|
||||
top: elementToParent
|
||||
});
|
||||
} else if (elementToParent + element.offsetHeight - parent.scrollTop > parent.offsetHeight) {
|
||||
parent.scrollTo({
|
||||
top: elementToParent + element.offsetHeight - parent.offsetHeight
|
||||
});
|
||||
}
|
||||
}
|
||||
export function getFullPathKeys(options, fieldNames) {
|
||||
return options.map(item => item[SEARCH_MARK]?.map(opt => opt[fieldNames.value]));
|
||||
}
|
||||
function isMultipleValue(value) {
|
||||
return Array.isArray(value) && Array.isArray(value[0]);
|
||||
}
|
||||
export function toRawValues(value) {
|
||||
if (!value) {
|
||||
return [];
|
||||
}
|
||||
if (isMultipleValue(value)) {
|
||||
return value;
|
||||
}
|
||||
return (value.length === 0 ? [] : [value]).map(val => Array.isArray(val) ? val : [val]);
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
import type { SingleValueType, DefaultOptionType, InternalFieldNames, ShowCheckedStrategy, LegacyKey } from '../Cascader';
|
||||
import type { GetEntities } from '../hooks/useEntities';
|
||||
export declare function formatStrategyValues(pathKeys: LegacyKey[], getKeyPathEntities: GetEntities, showCheckedStrategy?: ShowCheckedStrategy): LegacyKey[];
|
||||
export declare function toPathOptions(valueCells: SingleValueType, options: DefaultOptionType[], fieldNames: InternalFieldNames, stringMode?: boolean): {
|
||||
value: SingleValueType[number];
|
||||
index: number;
|
||||
option: DefaultOptionType;
|
||||
}[];
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
import { SHOW_CHILD } from "./commonUtil";
|
||||
export function formatStrategyValues(pathKeys, getKeyPathEntities, showCheckedStrategy) {
|
||||
const valueSet = new Set(pathKeys);
|
||||
const keyPathEntities = getKeyPathEntities();
|
||||
return pathKeys.filter(key => {
|
||||
const entity = keyPathEntities[key];
|
||||
const parent = entity ? entity.parent : null;
|
||||
const children = entity ? entity.children : null;
|
||||
if (entity && entity.node.disabled) {
|
||||
return true;
|
||||
}
|
||||
return showCheckedStrategy === SHOW_CHILD ? !(children && children.some(child => child.key && valueSet.has(child.key))) : !(parent && !parent.node.disabled && valueSet.has(parent.key));
|
||||
});
|
||||
}
|
||||
export function toPathOptions(valueCells, options, fieldNames,
|
||||
// Used for loadingKeys which saved loaded keys as string
|
||||
stringMode = false) {
|
||||
let currentList = options;
|
||||
const valueOptions = [];
|
||||
for (let i = 0; i < valueCells.length; i += 1) {
|
||||
const valueCell = valueCells[i];
|
||||
const foundIndex = currentList?.findIndex(option => {
|
||||
const val = option[fieldNames.value];
|
||||
return stringMode ? String(val) === String(valueCell) : val === valueCell;
|
||||
});
|
||||
const foundOption = foundIndex !== -1 ? currentList?.[foundIndex] : null;
|
||||
valueOptions.push({
|
||||
value: foundOption?.[fieldNames.value] ?? valueCell,
|
||||
index: foundIndex,
|
||||
option: foundOption
|
||||
});
|
||||
currentList = foundOption?.[fieldNames.children];
|
||||
}
|
||||
return valueOptions;
|
||||
}
|
||||
Generated
Vendored
+2
@@ -0,0 +1,2 @@
|
||||
import type { DefaultOptionType, FieldNames } from '../Cascader';
|
||||
export declare function warningNullOptions(options: DefaultOptionType[], fieldNames: FieldNames): void;
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
import warning from "@rc-component/util/es/warning";
|
||||
// value in Cascader options should not be null
|
||||
export function warningNullOptions(options, fieldNames) {
|
||||
if (options) {
|
||||
const recursiveOptions = optionsList => {
|
||||
for (let i = 0; i < optionsList.length; i++) {
|
||||
const option = optionsList[i];
|
||||
if (option[fieldNames?.value] === null) {
|
||||
warning(false, '`value` in Cascader options should not be `null`.');
|
||||
return true;
|
||||
}
|
||||
if (Array.isArray(option[fieldNames?.children]) && recursiveOptions(option[fieldNames?.children])) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
};
|
||||
recursiveOptions(options);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user