import React from 'react'; import {themeable, ClassNamesFn, ThemeProps} from '../theme'; import Overlay from './Overlay'; import PopOver from './PopOver'; import {Icon} from './icons'; import {autobind} from '../utils/helper'; import Alert2 from './Alert2'; import BaiduMapPicker from './BaiduMapPicker'; import {LocaleProps, localeable} from '../locale'; export interface LocationProps extends ThemeProps, LocaleProps { vendor: 'baidu' | 'gaode' | 'tenxun'; coordinatesType: 'bd09' | 'gcj02'; placeholder: string; clearable: boolean; ak: string; value?: { address: string; lat: number; lng: number; city?: string; }; disabled?: boolean; className?: string; popoverClassName?: string; onChange: (value: any) => void; popOverContainer?: any; } export interface LocationState { isFocused: boolean; isOpened: boolean; } export class LocationPicker extends React.Component< LocationProps, LocationState > { static defaultProps = { placeholder: 'LocationPicker.placeholder', clearable: false }; domRef: React.RefObject = React.createRef(); state = { isFocused: false, isOpened: false }; @autobind handleKeyPress(e: React.KeyboardEvent) { if (e.key === ' ') { this.handleClick(); e.preventDefault(); } } @autobind handleFocus() { this.setState({ isFocused: true }); } @autobind handleBlur() { this.setState({ isFocused: true }); } @autobind handleClick() { this.state.isOpened ? this.close() : this.open(); } @autobind getTarget() { return this.domRef.current; } @autobind getParent() { return this.domRef.current?.parentElement; } @autobind open(fn?: () => void) { this.props.disabled || this.setState( { isOpened: true }, fn ); } @autobind close() { this.setState({ isOpened: false }); } @autobind clearValue(e: React.MouseEvent) { e.preventDefault(); e.stopPropagation(); const onChange = this.props.onChange; onChange(''); } @autobind handlePopOverClick(e: React.MouseEvent) { e.stopPropagation(); e.preventDefault(); } @autobind handleChange(value: any) { if (value) { value = { ...value, vendor: this.props.vendor }; } this.props.onChange(value); } render() { const { classnames: cx, value, className, popoverClassName, disabled, placeholder, clearable, popOverContainer, vendor, coordinatesType, ak } = this.props; const __ = this.props.translate; const {isFocused, isOpened} = this.state; return (
{value ? ( {value.address} ) : ( {__(placeholder)} )} {clearable && !disabled && value ? ( ) : null} {vendor === 'baidu' ? ( ) : ( {__('${vendor} 地图控件不支持', {vendor})} )}
); } } const ThemedCity = themeable(localeable(LocationPicker)); export default ThemedCity;