antd-mobile
Version:
<img src="https://gw.alipayobjects.com/mdn/rms_ee68a8/afts/img/A*hjjDS5Yy-ooAAAAAAAAAAAAAARQnAQ" alt="logo" width="100%" />
32 lines (27 loc) • 666 B
JavaScript
import { useMemo } from 'react';
export function useCascadePickerOptions(options) {
return useMemo(() => {
let depth = 1;
const subOptionsRecord = {};
function traverse(option, currentDepth) {
if (!option.children) {
return;
}
subOptionsRecord[option.value] = option.children;
const nextDepth = currentDepth + 1;
if (nextDepth > depth) {
depth = nextDepth;
}
option.children.forEach(option => {
traverse(option, nextDepth);
});
}
options.forEach(option => {
traverse(option, 1);
});
return {
depth,
subOptionsRecord
};
}, [options]);
}