Filter.tsx 1.71 KB
import React, { useState, useEffect } from "react";
import { Row, Select } from "antd";
import { LabeledValue } from "antd/lib/select";

const Option = Select.Option;

const userData = [
  { value: 7, label: "工作车辆" },
  { value: 8, label: "员工车辆" },
  { value: 9, label: "大客户" },
];

export interface FilterPara {
  discountType?: number;
  discountTypeName?: string;
  [key: string]: any;
}

interface Props {
  onChange: Function;
  filterParam: FilterPara;
}

export default function Filter({ onChange, filterParam}: Props) {
  const [type, setType] = useState<LabeledValue>();

  useEffect(() => {
    if (filterParam) {
      filterParam.discountType && filterParam.discountTypeName
        ? setType({
            key: `${filterParam.discountType}`,
            value: filterParam.discountType,
            label: filterParam.discountTypeName,
          })
        : null;
    }
  }, [filterParam]);

  function onTypeChange(value: LabeledValue) {
    const _value = value || ({} as LabeledValue);
    const { key, label } = _value;
    onChange &&
      onChange(
        { discountType: key || undefined, current: 1, discountTypeName: label },
        true
      );
  }

  return (
    <Row style={{ width: "100%", alignItems: "center" }}>
      <span>客户类型:</span>
      <Select
        allowClear={false}
        placeholder="请选择"
        labelInValue
        value={type}
        onChange={onTypeChange}
        style={{ width: 150, marginLeft: 10 }}
      >
        {userData.map((r) => (
          <Option key={r.value} value={r.value}>
            {r.label || ""}
          </Option>
        ))}
      </Select>
    </Row>
  );
}