Map组件新增所属区域选择

master
LiuJiaNan 2025-11-08 09:44:00 +08:00
parent db0c60308c
commit 48fbc201fb
4 changed files with 56 additions and 15 deletions

View File

@ -1,5 +1,14 @@
import type { FC } from "react"; import type { FC } from "react";
export interface OnConfirmParams {
/** 经度值 */
longitude: number | string;
/** 纬度值 */
latitude: number | string;
/** 额外参数 */
extra?: { area: string };
}
export interface MapSelectorProps { export interface MapSelectorProps {
/** 是否显示弹窗 */ /** 是否显示弹窗 */
visible: boolean; visible: boolean;
@ -9,10 +18,17 @@ export interface MapSelectorProps {
longitude?: number | string; longitude?: number | string;
/** 纬度值 */ /** 纬度值 */
latitude?: number | string; latitude?: number | string;
/** 所属区域 */
area?: string;
/** 是否显示所属区域 */
showArea?: boolean;
/** 确认选择回调 */ /** 确认选择回调 */
onConfirm?: (longitude: number | string, latitude: number | string) => void; onConfirm?: (longitude: number | string, latitude: number | string, extra: { area: string }) => void;
} }
/**
*
*/
declare const MapSelector: FC<MapSelectorProps>; declare const MapSelector: FC<MapSelectorProps>;
export default MapSelector; export default MapSelector;

View File

@ -1,4 +1,4 @@
import { Button, Col, Form, Input, Modal, Row, Spin } from "antd"; import { Button, Col, Form, Input, Modal, Row, Select, Spin } from "antd";
import { useEffect, useRef, useState } from "react"; import { useEffect, useRef, useState } from "react";
/** /**
@ -11,6 +11,8 @@ const MapSelector = (props) => {
longitude, longitude,
latitude, latitude,
onConfirm, onConfirm,
area = "",
showArea = false,
} = props; } = props;
const mapContainerRef = useRef(null); const mapContainerRef = useRef(null);
@ -19,12 +21,14 @@ const MapSelector = (props) => {
const [currentLongitude, setCurrentLongitude] = useState(longitude || ""); const [currentLongitude, setCurrentLongitude] = useState(longitude || "");
const [currentLatitude, setCurrentLatitude] = useState(latitude || ""); const [currentLatitude, setCurrentLatitude] = useState(latitude || "");
const [localSearch, setLocalSearch] = useState(""); const [localSearch, setLocalSearch] = useState("");
const [currentArea, setCurrentArea] = useState("");
// 当外部经纬度变化时,更新内部状态 // 当外部经纬度变化时,更新内部状态
useEffect(() => { useEffect(() => {
setCurrentLongitude(longitude || ""); setCurrentLongitude(longitude || "");
setCurrentLatitude(latitude || ""); setCurrentLatitude(latitude || "");
}, [longitude, latitude]); setCurrentArea(area || "");
}, [longitude, latitude, area]);
// 初始化地图 // 初始化地图
const initMap = async () => { const initMap = async () => {
@ -96,7 +100,7 @@ const MapSelector = (props) => {
// 确认选择 // 确认选择
const handleConfirm = () => { const handleConfirm = () => {
if (onConfirm) { if (onConfirm) {
onConfirm(currentLongitude, currentLatitude); onConfirm(currentLongitude, currentLatitude, { area: currentArea });
} }
handleClose(); handleClose();
}; };
@ -146,19 +150,29 @@ const MapSelector = (props) => {
title="坐标" title="坐标"
onCancel={handleClose} onCancel={handleClose}
onOk={handleConfirm} onOk={handleConfirm}
width={800} width={1000}
destroyOnHidden={false} destroyOnHidden={false}
afterClose={handleAfterClose} afterClose={handleAfterClose}
> >
<Form labelAlign="right" labelCol={{ span: 6 }} wrapperCol={{ span: 18 }}> <Form labelAlign="right" labelCol={{ span: 6 }} wrapperCol={{ span: 18 }}>
{
showArea && (
<Row gutter={24}>
<Col span={12}>
<Form.Item label="所属区域">
<Select value={currentArea} onChange={e => setCurrentArea(e)} allowClear>
<Select.Option value="1">东港区</Select.Option>
<Select.Option value="2">西港区</Select.Option>
</Select>
</Form.Item>
</Col>
</Row>
)
}
<Row gutter={24}> <Row gutter={24}>
<Col span={12}> <Col span={12}>
<Form.Item label="关键字搜索"> <Form.Item label="关键字搜索">
<Input <Input value={localSearch} onChange={e => setLocalSearch(e.target.value)} allowClear />
value={localSearch}
onChange={e => setLocalSearch(e.target.value)}
allowClear
/>
</Form.Item> </Form.Item>
</Col> </Col>
<Col span={12}> <Col span={12}>

View File

@ -5,12 +5,19 @@ export interface MapProps {
longitudeProps?: string; longitudeProps?: string;
/** 纬度属性名,默认 latitude */ /** 纬度属性名,默认 latitude */
latitudeProps?: string; latitudeProps?: string;
/** 经纬度变化回调 */
onConfirm?: (longitude: number | string, latitude: number | string) => void;
/** 经纬度是否必填,默认 true */ /** 经纬度是否必填,默认 true */
required?: boolean; required?: boolean;
/** 所属区域 */
area?: string;
/** 是否显示所属区域 */
showArea?: boolean;
/** 确认选择回调 */
onConfirm?: (longitude: number | string, latitude: number | string, extra: { area: string }) => void;
} }
/**
*
*/
declare const Map: FC<MapProps>; declare const Map: FC<MapProps>;
export default Map; export default Map;

View File

@ -11,6 +11,8 @@ const Map = (props) => {
latitudeProps = "latitude", latitudeProps = "latitude",
onConfirm, onConfirm,
required = true, required = true,
area = "",
showArea = false,
} = props; } = props;
const form = Form.useFormInstance(); const form = Form.useFormInstance();
@ -18,14 +20,14 @@ const Map = (props) => {
const [currentLongitude, setCurrentLongitude] = useState(form.getFieldValue(longitudeProps) || ""); const [currentLongitude, setCurrentLongitude] = useState(form.getFieldValue(longitudeProps) || "");
const [currentLatitude, setCurrentLatitude] = useState(form.getFieldValue(latitudeProps) || ""); const [currentLatitude, setCurrentLatitude] = useState(form.getFieldValue(latitudeProps) || "");
const handleMapConfirm = (longitudeValue, latitudeValue) => { const handleMapConfirm = (longitudeValue, latitudeValue, extra) => {
setCurrentLongitude(longitudeValue); setCurrentLongitude(longitudeValue);
setCurrentLatitude(latitudeValue); setCurrentLatitude(latitudeValue);
form.setFieldsValue({ form.setFieldsValue({
[longitudeProps]: longitudeValue, [longitudeProps]: longitudeValue,
[latitudeProps]: latitudeValue, [latitudeProps]: latitudeValue,
}); });
onConfirm?.(longitudeValue, latitudeValue); onConfirm?.(longitudeValue, latitudeValue, extra);
setMapVisible(false); setMapVisible(false);
}; };
@ -44,7 +46,7 @@ const Map = (props) => {
<Input disabled /> <Input disabled />
</Form.Item> </Form.Item>
<Button type="primary" onClick={() => setMapVisible(true)}> <Button type="primary" onClick={() => setMapVisible(true)}>
点击定位 地图定位
</Button> </Button>
</div> </div>
</Form.Item> </Form.Item>
@ -55,6 +57,8 @@ const Map = (props) => {
onClose={() => setMapVisible(false)} onClose={() => setMapVisible(false)}
longitude={currentLongitude} longitude={currentLongitude}
latitude={currentLatitude} latitude={currentLatitude}
area={area}
showArea={showArea}
onConfirm={handleMapConfirm} onConfirm={handleMapConfirm}
/> />
</> </>