@rc-component/cascader
Version:
cascade select ui component for react
69 lines (67 loc) • 1.91 kB
JavaScript
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]);
}