All files / src/kernel SearchInputComponents.tsx

18.75% Statements 9/48
0% Branches 0/26
0% Functions 0/19
18.75% Lines 9/48

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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194                                                              66x                           66x                           66x                           66x                                 66x                                       66x                                 66x       66x                                                                                               66x                            
import React, { ReactElement  } from "react";
import {
  EnumMetaProps, MatchMode, ObjectSelectOptionProp,
  ObjectSelectOptionProps,
  SearchValue,
  SelectMetaProps, TableMetaProps
} from "@props/RecordProps";
import { DatePicker, Input, InputNumber, Select, Switch } from "antd";
import { isDateTimeType } from "@utils/ColumnsUtils";
import { HttpMethods, DateFormat, DateTimeFormat } from '@config/base';
import { EnumOrDynamicSelectSearchInput, Percentage, Currency, ObjectSelect } from '../form/fields';
import { getRawDomainName } from "@utils/ObjectUtils";
import { isMultiple } from "@kernel/ServerSideSearcherConfig";
 
export interface SearchInputProps {
  column: TableMetaProps;
  onUpdateSearchKeyword: (value: SearchValue, valueLabels?: { [propName: string]: string }) => void;
  commonProps: {
    [propName: string]: unknown;
  };
  performSearchCallback: () => Promise<void>;
  type?: string;
  defaultValue?: boolean | string | number | number[] | undefined;
  enumValue?: Array<EnumMetaProps>,
  dynamicSelectValue?: Array<SelectMetaProps>;
  ownerClass?: string;
  fieldName?: string;
  zIndex?: number;
  matchMode?: MatchMode;
}
 
export const getNumberSearchInput = (props: SearchInputProps): ReactElement => {
  const { commonProps, onUpdateSearchKeyword, performSearchCallback } = props;
  return (
    <InputNumber
      className="search-input"
      onChange={(value) => {
        onUpdateSearchKeyword(value);
      }}
      {...commonProps}
      onPressEnter={performSearchCallback}
    />
  );
};
 
export const getPercentageSearchInput = (props: SearchInputProps): ReactElement => {
  const { commonProps, onUpdateSearchKeyword, performSearchCallback } = props;
  return (
    <Percentage
      {...commonProps}
      className="search-input"
      onChange={(value) => {
        onUpdateSearchKeyword(value);
      }}
      onPressEnter={performSearchCallback}
    />
  );
};
 
export const getCurrencySearchInput = (props: SearchInputProps): ReactElement => {
  const { commonProps, onUpdateSearchKeyword, performSearchCallback } = props;
  return (
    <Currency
      {...commonProps}
      className="search-input"
      onChange={(value) => {
        onUpdateSearchKeyword(value);
      }}
      onPressEnter={performSearchCallback}
    />
  );
};
 
export const getBooleanSearchInput = (props: SearchInputProps): ReactElement => {
  const { commonProps, onUpdateSearchKeyword, defaultValue } = props;
  return (
    <div>
      <Switch
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore
        defaultChecked={defaultValue == null ? true : defaultValue}
        className="search-switch"
        {...commonProps}
        onChange={(value) => {
          onUpdateSearchKeyword(value);
        }} />
    </div>
  );
};
 
export const getDateSearchInput = (props: SearchInputProps): ReactElement => {
  const { commonProps, onUpdateSearchKeyword, type } = props;
  return (
    <div>
      <DatePicker
        style={{ width: "100%" }}
        className="date"
        showTime={isDateTimeType(type ?? "")}
        {...commonProps}
        format={isDateTimeType(type ?? "") ? DateTimeFormat : DateFormat}
        onChange={(dateMoment, dateString) => {
          if (dateString != null) {
            onUpdateSearchKeyword(dateString);
          }
        }}
      />
    </div>
  );
};
 
export const getHttpMethodSelectSearchInput = (props: SearchInputProps): ReactElement => {
  const { commonProps, onUpdateSearchKeyword } = props;
  return (
    <Select
      showSearch
      style={{ width: "100%" }}
      className="search-input httpMethod"
      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
      // @ts-ignore
      onChange={(value) => onUpdateSearchKeyword(value)}
      {...commonProps}
    >
      {HttpMethods.map(opt => <Select.Option key={opt} value={opt}>{opt}</Select.Option>)}
    </Select>
  );
};
 
export const getEnumOrDynamicSelectSearchInput = (props: SearchInputProps): ReactElement => {
  return <EnumOrDynamicSelectSearchInput {...props} />;
};
 
export const getObjectSearchInput = (props: SearchInputProps): ReactElement => {
  const {
    onUpdateSearchKeyword, type, defaultValue, ownerClass,
    fieldName, zIndex, column, matchMode,
  } = props;
  const transferredType = getRawDomainName(type ?? "");
  const multiple = isMultiple(matchMode);
  return (
    <div>
      <ObjectSelect
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore
        defaultValue={defaultValue}
        style={{ width: "100%" }}
        domainName={transferredType}
        mode={multiple ? "multiple" : undefined}
        placeholder="Type to search"
        notFoundContent="No data found"
        operation="query"
        onChange={(
          val: string | Array<string>,
          option?: ObjectSelectOptionProp | ObjectSelectOptionProps
        ) => {
          const valueLabels: { [propName: string]: string } = {};
          if (Array.isArray(option)) {
            option?.forEach(opt => {
              valueLabels[opt.value] = opt.children;
            });
          } else if (option) {
            valueLabels[option.value] = option.children;
          }
          let valNum;
          if (Array.isArray(val)) {
            valNum = val.map(Number);
          } else {
            valNum = Number(val);
          }
          onUpdateSearchKeyword(valNum, valueLabels);
        }}
        ownerClass={ownerClass ?? ""}
        fieldName={fieldName ?? ""}
        zIndex={zIndex ?? 1}
        column={column}
      />
    </div>
  );
};
 
export const getStringSearchInput = (props: SearchInputProps): ReactElement => {
  const { onUpdateSearchKeyword, commonProps, performSearchCallback } = props;
  return (
    <Input
      className="search-input"
      onChange={(e) => {
        const { value } = e.target;
        onUpdateSearchKeyword(value);
      }}
      {...commonProps}
      onPressEnter={performSearchCallback}
    />
  );
};