import { FC, useState, useRef } from 'react' import { Input } from 'antd-mobile' import ParseLogistics from '../ParseLogistics' import { BasicIdentifyAddressFieldValueType, IBasicIdentifyAddressConfig, AddressItemType } from '@/types' import AddressField from '../Address' import { getCodeByName } from '@/utils' interface IdentifyAddressFieldProps extends IBasicIdentifyAddressConfig { value?: BasicIdentifyAddressFieldValueType onChange?: (data: any) => void } interface ParseLogisticsValueType { addressDetail: string name: string phone: string province: string city: string district: string } interface AddressPropsType extends React.ComponentProps { getData: () => AddressItemType[] } const IdentifyAddressField: FC = props => { const { value, showIdentify = true, disabled = false, onChange } = props const [identifyValue, setIdentifyValue] = useState(value) const addressRef = useRef(null) const handleAddress = (type: string, val: string | object) => { onChange?.({ ...identifyValue, [type]: val }) setIdentifyValue({ ...identifyValue, [type]: val } as BasicIdentifyAddressFieldValueType) } const handleParseLogistics = (data: ParseLogisticsValueType) => { let addrData = [] as AddressItemType[] if (addressRef.current) { addrData = addressRef.current?.getData() } const params = { detail: data?.addressDetail || '', receiverName: data?.name || '', receiverMobile: data?.phone || '', address: [ getCodeByName(data?.province, addrData), getCodeByName(data?.city, addrData), getCodeByName(data?.district, addrData) ] } setIdentifyValue({ ...identifyValue, ...params }) onChange?.({ ...identifyValue, ...params }) } return ( <>
handleAddress('receiverName', val)} >
handleAddress('receiverMobile', val)} >
handleAddress('address', val)} >
handleAddress('detail', val)} placeholder={`请输入详细地址`} />
{!disabled && showIdentify && (
)} ) } export default IdentifyAddressField