image-20221011173932728.png

  1. import React, { useState, useEffect } from "react";
  2. import {
  3. func,
  4. array,
  5. object,
  6. bool,
  7. string,
  8. number,
  9. oneOfType,
  10. oneOf
  11. } from "prop-types";
  12. import { Space, InputNumber, Input } from "antd";
  13. import { isFunction, isObject, isEqual } from "lodash-es";
  14. const defaultOptions = [
  15. { label: "室", key: "bedroom" },
  16. { label: "厅", key: "livingroom" },
  17. { label: "厨", key: "kitchen" },
  18. { label: "卫", key: "bathroom" }
  19. ];
  20. const allowComponent = ["Input", "InputNumber"];
  21. const defaultComponent = allowComponent.at(-1);
  22. InputDimension.propTypes = {
  23. value: oneOfType([bool, number, string]),
  24. onChange: func,
  25. options: array,
  26. component: oneOf(allowComponent),
  27. componentProps: object,
  28. labelInValue: bool,
  29. };
  30. InputDimension.defaultProps = {
  31. options: defaultOptions,
  32. component: defaultComponent,
  33. labelInValue: false,
  34. };
  35. function InputDimension(props) {
  36. const {
  37. value,
  38. onChange,
  39. options,
  40. component,
  41. componentProps,
  42. labelInValue
  43. } = props;
  44. const [state, setState] = useState({});
  45. useEffect(() => {
  46. if (isObject(value) && !isEqual(value, state)) {
  47. setState(value);
  48. }
  49. }, [value]);
  50. function handleChange({label, key}) {
  51. return (e) => {
  52. const _value = isObject(e) ? e.target.value : e;
  53. setState(prevState => {
  54. const newState = { ...prevState, [key]: _value };
  55. if (labelInValue) {
  56. newState.label = label
  57. }
  58. if (isFunction(onChange)) {
  59. onChange(newState);
  60. }
  61. return newState;
  62. });
  63. };
  64. }
  65. return (
  66. <Space>
  67. {
  68. options.map(it => {
  69. const { label, key } = it;
  70. return (
  71. <RenderChild
  72. key={key}
  73. label={label}
  74. value={state[key]}
  75. component={component}
  76. componentProps={componentProps}
  77. onChange={handleChange(it)}
  78. />
  79. );
  80. })
  81. }
  82. </Space>
  83. );
  84. }
  85. export default InputDimension;
  86. function RenderChild(props) {
  87. const {
  88. label,
  89. component,
  90. componentProps,
  91. ...restProps
  92. } = props;
  93. if (isEqual(component, defaultComponent)) {
  94. return (
  95. <InputNumber
  96. addonAfter={label}
  97. {...componentProps}
  98. {...restProps}
  99. />
  100. );
  101. }
  102. return (
  103. <Input
  104. suffix={label}
  105. {...componentProps}
  106. {...restProps}
  107. />
  108. );
  109. }

维度数据

  1. const options = [
  2. { label: "长", value: "length" },
  3. { label: "宽", value: "width" },
  4. { label: "高", value: "height" },
  5. ];
  6. // 两个后面都是 itude
  7. const optionsLat = [
  8. { label: "经度", value: "longitude" },
  9. { label: "维度", value: "latitude" },
  10. ]