import {computed, designComponent, PropType, reactive, useModel, watch} from "@peryl/react-compose"; import {EditProps, useEdit} from "../../uses/useEdit"; import {StyleProps, useStyle} from "../../uses/useStyle"; import {$configuration} from "../$configuration"; import {iAddressService} from "../$address/createAddressService"; import {iAddressLazyMeta} from "../$address/address.utils"; import {defer, DFD} from "@peryl/utils/defer"; import {Select} from "../Select"; import {SelectOption} from "../SelectOption"; export const Address = designComponent({ name: '-address', inheritPropsType: Select, props: { ...EditProps, ...StyleProps, modelValue: { type: String }, // 绑定值,如果bindId为true,那么就是绑定id; parentValue: { type: String }, // 父绑定值,如果bindId为true,那么就是绑定id; province: { type: Boolean, default: false }, // 是否为选择省份 city: { type: Boolean, default: false }, // 是否为选择城市 district: { type: Boolean, default: false }, // 是否为选择区县 watchParentChange: { type: Boolean, default: true },// 是否自动监听父值变化清空当前值 service: { type: Object as PropType }, // 自定义地址服务 }, emits: { onUpdateModelValue: (val?: string | number) => true, }, setup({ props, event, attrs }) { const model = useModel(() => props.modelValue, event.emit.onUpdateModelValue); const service = props.service || $configuration.use('address')!; useEdit({ adjust: val => {if (!props.province && !props.parentValue) {val.disabled = true;}} }); useStyle(); const state = reactive({ showAddressMeta: null as null | iAddressLazyMeta, loadingCount: 0, selectMetas: null as null | { parentCode: string, metas: iAddressLazyMeta[], dfd: DFD, } }); const parentCode = computed(() => props.province ? "0" : props.parentValue); watch(() => model.value, async val => { if (!val) { state.showAddressMeta = null; } else { state.loadingCount++; try { const [meta] = await service.getAddressByCodes([val]); state.showAddressMeta = meta; } finally { state.loadingCount--; } } }, { immediate: true }); const utils = { loadSelectMetas: async () => { if (!parentCode.value) { state.selectMetas = null; } if (!!state.selectMetas && state.selectMetas.parentCode === parentCode.value) { return; } state.selectMetas = { parentCode: parentCode.value!, metas: [], dfd: defer(), }; try { state.loadingCount++; state.selectMetas!.metas = await service.getAddressByParentCodes([state.selectMetas.parentCode]); } finally { state.loadingCount--; } }, }; const handler = { onPopperShow: () => { utils.loadSelectMetas(); }, }; const popperAttrs = computed(() => { const { onShow } = attrs; return { ...attrs, onShow: () => { handler.onPopperShow(); onShow?.(); }, }; }); watch(() => ({ parentCode: parentCode.value, value: model.value, }), (newVal, oldVal) => { if (!props.watchParentChange) {return;} if (!oldVal) {return;} if (newVal.parentCode !== oldVal.parentCode && newVal.value === oldVal.value) { model.value = null as any; return; } if (newVal.value !== oldVal.value) { state.showAddressMeta = null; } if (newVal.parentCode !== oldVal.parentCode) { state.selectMetas = null; } }); return { render: () => ( ) }; }, });