
import React, { useState, useEffect } from "react";import { func, array, object, bool, string, number, oneOfType, oneOf} from "prop-types";import { Space, InputNumber, Input } from "antd";import { isFunction, isObject, isEqual } from "lodash-es";const defaultOptions = [ { label: "室", key: "bedroom" }, { label: "厅", key: "livingroom" }, { label: "厨", key: "kitchen" }, { label: "卫", key: "bathroom" }];const allowComponent = ["Input", "InputNumber"];const defaultComponent = allowComponent.at(-1);InputDimension.propTypes = { value: oneOfType([bool, number, string]), onChange: func, options: array, component: oneOf(allowComponent), componentProps: object, labelInValue: bool,};InputDimension.defaultProps = { options: defaultOptions, component: defaultComponent, labelInValue: false,};function InputDimension(props) { const { value, onChange, options, component, componentProps, labelInValue } = props; const [state, setState] = useState({}); useEffect(() => { if (isObject(value) && !isEqual(value, state)) { setState(value); } }, [value]); function handleChange({label, key}) { return (e) => { const _value = isObject(e) ? e.target.value : e; setState(prevState => { const newState = { ...prevState, [key]: _value }; if (labelInValue) { newState.label = label } if (isFunction(onChange)) { onChange(newState); } return newState; }); }; } return ( <Space> { options.map(it => { const { label, key } = it; return ( <RenderChild key={key} label={label} value={state[key]} component={component} componentProps={componentProps} onChange={handleChange(it)} /> ); }) } </Space> );}export default InputDimension;function RenderChild(props) { const { label, component, componentProps, ...restProps } = props; if (isEqual(component, defaultComponent)) { return ( <InputNumber addonAfter={label} {...componentProps} {...restProps} /> ); } return ( <Input suffix={label} {...componentProps} {...restProps} /> );}
维度数据
const options = [ { label: "长", value: "length" }, { label: "宽", value: "width" }, { label: "高", value: "height" },];// 两个后面都是 itudeconst optionsLat = [ { label: "经度", value: "longitude" }, { label: "维度", value: "latitude" },]