All files / src/form/fields HttpMethodSelect.tsx

100% Statements 14/14
83.33% Branches 5/6
100% Functions 4/4
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54                                        66x 13x 13x   13x 13x 13x 13x 13x 13x     13x   13x   13x                     110x            
import React, { CSSProperties, ReactElement } from "react";
import { Select } from "antd";
import { getReadOnlyClass } from "@utils/ComponentUtils";
import { HttpMethods } from '@config/base';
import { SuffixIcon } from '../../components';
import { EnumMetaProps } from "@props/RecordProps";
 
interface HttpMethodSelectProps {
  value?: string | undefined | { enumType: string; name: string };
  style: CSSProperties;
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  onSelect: any;
  updatable?: boolean;
  className?: string;
  options?: Array<EnumMetaProps> | undefined;
}
 
/**
 * Http Method 的选择控件,受控控件
 */
const HttpMethodSelect = (props: HttpMethodSelectProps): ReactElement => {
  const { style, value, updatable, options } = props;
  const name: string = (value == null) ? "" : (typeof value === "string") ? value : value?.name;
 
  const removeUpdatableAttribute = (): HttpMethodSelectProps => {
    const propsToSelect: HttpMethodSelectProps = {} as HttpMethodSelectProps;
    Object.assign(propsToSelect, props);
    delete propsToSelect.updatable;
    propsToSelect.value = name;
    return propsToSelect;
  };
 
  const optionsToShow = options ? (options.map(opt => opt.value)) : HttpMethods;
 
  const propsToSelect = removeUpdatableAttribute();
 
  return (
    <Select
      {...propsToSelect}
      style={style}
      showSearch
      defaultValue={name}
      className={`httpMethod ${getReadOnlyClass(updatable)}`}
      suffixIcon={<SuffixIcon updatable={updatable} />}
      disabled={!updatable}
      dropdownStyle={{ zIndex: 2000 }}
    >
      {optionsToShow.map(opt => <Select.Option key={opt} value={opt}>{opt}</Select.Option>)}
    </Select>
  );
};
 
export default HttpMethodSelect;