Parameter.tsx 2.3 KB
Newer Older
R
Rongfeng Fu 已提交
1
import { intl } from '@/utils/intl';
R
Rongfeng Fu 已提交
2 3
import { useEffect, useState } from 'react';
import { Space, Input, Select } from 'antd';
R
Rongfeng Fu 已提交
4 5 6 7 8 9
import { getLocale } from 'umi';
import EnStyles from './indexEn.less';
import ZhStyles from './indexZh.less';

const locale = getLocale();
const styles = locale === 'zh-CN' ? ZhStyles : EnStyles;
R
Rongfeng Fu 已提交
10 11 12 13 14 15 16 17

interface Props {
  value?: API.ParameterValue;
  onChange?: (value?: API.ParameterValue) => void;
  onBlur?: () => void;
}

const optionConfig = [
R
Rongfeng Fu 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31
  {
    label: intl.formatMessage({
      id: 'OBD.pages.components.Parameter.AutomaticAllocation',
      defaultMessage: '自动分配',
    }),
    value: true,
  },
  {
    label: intl.formatMessage({
      id: 'OBD.pages.components.Parameter.Custom',
      defaultMessage: '自定义',
    }),
    value: false,
  },
R
Rongfeng Fu 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
];

export default function Parameter({
  value: itemValue,
  onChange,
  onBlur,
}: Props) {
  const [parameterValue, setParameterValue] =
    useState<API.ParameterValue>(itemValue);

  useEffect(() => {
    if (onChange) {
      if (
        itemValue?.adaptive !== parameterValue?.adaptive ||
        itemValue?.value !== parameterValue?.value
      ) {
        onChange(parameterValue);
      }
    }
  }, [parameterValue]);
  return (
    <Space size={4}>
      <Select
        defaultValue={parameterValue?.adaptive}
        className={styles.paramterSelect}
        onChange={(value) =>
          setParameterValue({ ...parameterValue, adaptive: value })
        }
        disabled={!parameterValue?.auto}
R
Rongfeng Fu 已提交
61 62
        dropdownMatchSelectWidth={false}
        style={locale === 'zh-CN' ? { width: 100 } : { width: 180 }}
R
Rongfeng Fu 已提交
63 64 65 66 67 68 69 70
      >
        {optionConfig.map((option) => (
          <Select.Option value={option.value} key={`${option.value}`}>
            {option.label}
          </Select.Option>
        ))}
      </Select>
      <Input
R
Rongfeng Fu 已提交
71 72 73 74
        placeholder={intl.formatMessage({
          id: 'OBD.pages.components.Parameter.PleaseEnter',
          defaultMessage: '请输入',
        })}
R
Rongfeng Fu 已提交
75 76 77 78 79 80 81 82 83 84 85 86
        defaultValue={parameterValue?.value}
        className={styles.paramterInput}
        style={{ width: 86 }}
        disabled={parameterValue?.adaptive}
        onBlur={onBlur}
        onChange={(e) =>
          setParameterValue({ ...parameterValue, value: e.target.value })
        }
      />
    </Space>
  );
}