1import { CascaderValueType, CascaderOption as RCCascaderOption } from 'rc-cascader/lib/Cascader';
2import { CascaderOption } from './Cascader';
3
4type onChangeType = ((values: string[], options: CascaderOption[]) => void) | undefined;
5
6export const onChangeCascader = (onChanged: onChangeType) => (
7  values: CascaderValueType,
8  options: RCCascaderOption[]
9) => {
10  if (onChanged) {
11    // map values to strings for backwards compatibility with Cascader components
12    onChanged(
13      values.map((value) => String(value)),
14      fromRCOptions(options)
15    );
16  }
17};
18
19type onLoadDataType = ((options: CascaderOption[]) => void) | undefined;
20
21export const onLoadDataCascader = (onLoadData: onLoadDataType) => (options: RCCascaderOption[]) => {
22  if (onLoadData) {
23    onLoadData(fromRCOptions(options));
24  }
25};
26
27const fromRCOptions = (options: RCCascaderOption[]): CascaderOption[] => {
28  return options.map(fromRCOption);
29};
30
31const fromRCOption = (option: RCCascaderOption): CascaderOption => {
32  return {
33    value: option.value ?? '',
34    label: (option.label as unknown) as string,
35  };
36};
37