diff --git a/packages/plugins/@tachybase/plugin-china-region/src/client/ChinaRegionProvider.tsx b/packages/plugins/@tachybase/plugin-china-region/src/client/ChinaRegionProvider.tsx index 8fddba88f..45e924b80 100644 --- a/packages/plugins/@tachybase/plugin-china-region/src/client/ChinaRegionProvider.tsx +++ b/packages/plugins/@tachybase/plugin-china-region/src/client/ChinaRegionProvider.tsx @@ -11,9 +11,7 @@ export const useChinaRegionDataSource = (options) => { params: { sort: 'code', paginate: false, - filter: { - level: 1, - }, + filter: { level: 1 }, }, }, { @@ -38,7 +36,7 @@ export const useChinaRegionLoadData = () => { const api = useAPIClient(); const field = useField(); const maxLevel = field.componentProps.maxLevel; - return (selectedOptions) => { + return (selectedOptions, onSuccess?) => { const targetOption = selectedOptions[selectedOptions.length - 1]; if (targetOption?.children?.length > 0) { return; @@ -54,14 +52,18 @@ export const useChinaRegionLoadData = () => { }, }) .then(({ data }) => { - targetOption.loading = false; - targetOption.children = - data?.data?.map((item) => { - if (maxLevel > item.level) { - item.isLeaf = false; - } - return item; - }) || []; + 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) => { diff --git a/packages/plugins/@tachybase/plugin-mobile-client/src/client/core/schema/components/antd-mobile/Cascader/AntdCadcader.tsx b/packages/plugins/@tachybase/plugin-mobile-client/src/client/core/schema/components/antd-mobile/Cascader/AntdCadcader.tsx index 66b7d1001..e7e99b1dc 100644 --- a/packages/plugins/@tachybase/plugin-mobile-client/src/client/core/schema/components/antd-mobile/Cascader/AntdCadcader.tsx +++ b/packages/plugins/@tachybase/plugin-mobile-client/src/client/core/schema/components/antd-mobile/Cascader/AntdCadcader.tsx @@ -6,7 +6,7 @@ import { useFieldServiceFilter, useRequest, } from '@tachybase/client'; -import { observer, useFieldSchema, useForm } from '@tachybase/schema'; +import { ArrayField, observer, useField, useFieldSchema, useForm } from '@tachybase/schema'; import { isArray } from '@tachybase/utils/client'; import { Button, CascaderView, Divider, Popup, SearchBar, Space } from 'antd-mobile'; @@ -18,7 +18,18 @@ import { MInput } from '../Input'; import { useStyles } from './style'; export const AntdCascader = observer((props) => { - const { service, fieldNames, disabled, multiple, onChange, value, formCascaderValues, index } = props as any; + const { + service, + fieldNames, + disabled, + useLoadData, + useDataSource, + multiple, + onChange, + value, + formCascaderValues, + index, + } = props as any; const fieldSchema = useFieldSchema(); const cm = useCollectionManager(); const { styles } = useStyles(); @@ -34,7 +45,8 @@ export const AntdCascader = observer((props) => { const [cascaderValue, setCascaderValue] = useState(value); const [valueText, setValueText] = useState(''); const [flatOption, setFlatOption] = useState([]); - const { run } = useRequest( + const field = useField(); + const { loading: collectionLoading, run: collectionRun } = useRequest( { resource: collection?.name, action: 'list', @@ -47,7 +59,8 @@ export const AntdCascader = observer((props) => { manual: true, onSuccess({ data }) { if (data) { - setOptions(cascadeOption(data)); + setOptions(data); + field.dataSource = data || []; const valueOption = flatOptions(data); setFlatOption(valueOption); if (value) { @@ -57,39 +70,40 @@ export const AntdCascader = observer((props) => { }, }, ); - - const cascadeOption = (option) => { - option.forEach((item) => { - item['value'] = item[fieldNames.value]; - item['label'] = item[fieldNames.label]; - if (item.children) { - item.children = cascadeOption(item.children); - } - }); - return option; - }; + const loadData = useLoadData?.(props); + const { loading: areaLoading, run: areaRun } = useDataSource?.({ + onSuccess(data) { + field.dataSource = data?.data || []; + setOptions(data?.data); + }, + }) || { loading: true, run: null }; + if (!valueText && value && isChinaRegion) { + setValueText(getChinaRegionText(value, fieldNames)); + } useEffect(() => { if (!isChinaRegion && collection?.name) { checkedPopup(); } }, [filter]); useEffect(() => { - if (options.length) { - cascadeOption(options); + if (!isChinaRegion && flatOption.length) { + setValueText(getValueText(flatOption, value, '', fieldNames['label'])); } }, [fieldSchema['x-component-props']?.['fieldNames']]); const handleSelect = async (option) => { if (option) { - setCascaderValue(option[option.length - 1]); + setCascaderValue(isChinaRegion ? option : option[option.length - 1]); } else { setCascaderValue({}); } }; const checkedPopup = () => { - if (collection && collection.name) { - run(); + if (!isChinaRegion) { + collectionRun(); + } else { + areaRun(); } }; @@ -135,18 +149,47 @@ export const AntdCascader = observer((props) => { }} > - { - const paramsFilter = { ...filter }; - paramsFilter[fieldNames['label']] = { $includes: value }; - setFilter(paramsFilter); - setSearchValue(value); + {!isChinaRegion ? ( + <> + { + const paramsFilter = { ...filter }; + paramsFilter[fieldNames['label']] = { $includes: value }; + setFilter(paramsFilter); + setSearchValue(value); + }} + /> + + + ) : null} + { + if (isChinaRegion) { + const maxLevel = field.componentProps.maxLevel; + const targetOption = extend['items'][extend['items'].length - 1]; + if (maxLevel > targetOption.level) { + targetOption.children = []; + } + loadData(extend['items'], (data) => { + if (data?.data.length) { + targetOption.children = data.data.map((item) => { + if (maxLevel > item.level) { + item.isLeaf = false; + } + return item; + }); + setOptions([...options]); + } + }); + } + handleSelect(extend['items']); }} /> - - handleSelect(extend['items'])} />