tachybase_todo/packages/plugins/@tachybase/plugin-china-region/src/client/ChinaRegionProvider.tsx
wjh 299c01da9f fix: 完善mobile级联组件的地区功能和只读样式 (#1226)
Reviewed-on: daoyoucloud/tachybase#1226
Reviewed-by: sealday <zhanglin@daoyoucloud.com>
Co-authored-by: wjh <wwwjh0710@163.com>
Co-committed-by: wjh <wwwjh0710@163.com>
2024-06-21 18:12:15 +08:00

74 lines
1.8 KiB
TypeScript

import { useAPIClient, useRequest } from '@tachybase/client';
import { ArrayField, useField } from '@tachybase/schema';
export const useChinaRegionDataSource = (options) => {
const field = useField<ArrayField>();
const maxLevel = field.componentProps.maxLevel;
return useRequest(
{
resource: 'chinaRegions',
action: 'list',
params: {
sort: 'code',
paginate: false,
filter: { level: 1 },
},
},
{
...options,
onSuccess(data) {
options?.onSuccess({
data:
data?.data?.map((item) => {
if (maxLevel !== 1) {
item.isLeaf = false;
}
return item;
}) || [],
});
},
manual: true,
},
);
};
export const useChinaRegionLoadData = () => {
const api = useAPIClient();
const field = useField<ArrayField>();
const maxLevel = field.componentProps.maxLevel;
return (selectedOptions, onSuccess?) => {
const targetOption = selectedOptions[selectedOptions.length - 1];
if (targetOption?.children?.length > 0) {
return;
}
targetOption.loading = true;
api
.resource('chinaRegions')
.list({
sort: 'code',
paginate: false,
filter: {
parentCode: targetOption.code,
},
})
.then(({ data }) => {
if (onSuccess) {
onSuccess(data);
} else {
targetOption.loading = false;
targetOption.children =
data?.data?.map((item) => {
if (maxLevel > item.level) {
item.isLeaf = false;
}
return item;
}) || [];
}
field.dataSource = [...field.dataSource];
})
.catch((err) => {
console.error(err);
});
};
};