All files / src/kernel EditableComponents.tsx

64.58% Statements 93/144
61.29% Branches 57/93
49.12% Functions 28/57
64.53% Lines 91/141

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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562                                                              66x   66x 4x 4x 4x             66x 31x 31x 31x               66x 100x 100x                   66x 1x 1x       1x       66x   35x 35x                     66x 1x                       1x 1x 1x 1x                                     66x 17x             17x                   66x 17x           66x 61x             66x 1x             66x 1x 1x   1x                                 66x                         66x     78x 78x 78x                               66x 1x   1x                             66x 1x   1x                             66x 31x 31x                       66x         17x   17x 17x     17x     17x 17x 2x   15x     17x 17x 17x   17x                                                                                           66x 1x 1x                     66x       1x 1x                                       66x 1x 1x                       66x                         66x 8x 8x 8x                         66x                   66x                   66x                         66x   66x                               66x       66x               66x         66x                           66x                                         66x                           66x                                          
import React, { ChangeEvent, ReactElement, Suspense } from "react";
import { DatePicker, Input, InputNumber, Select, Switch } from "antd";
import { EyeTwoTone, EyeInvisibleOutlined } from '@ant-design/icons';
import { EditableComponentDefaultStyle, FileFieldType } from '@config/base';
import { TableMode } from "@props/RecordProps";
import { getReadOnlyClass } from "@utils/ComponentUtils";
import { hasDetailPanel, isObjectType } from "@utils/ColumnsUtils";
import {
  EditableComponentRenderFunction,
  EditableControllerProps
} from "./ComponentsConfig";
import { SearchNoDataFoundText, SearchPlaceholderText } from "@utils/Constants";
 
import { SingleImage, SuffixIcon, FileOperator, CommentsIcon } from '../components';
 
import { HasDetailFieldPlaceholderCell } from "../form/cells";
 
import {
  Percentage, Currency, DynamicSelect, DynamicCheckboxAndRadio,
  EnumSelect, ObjectSelect, HttpMethodSelect, StaticFieldSelect,
  RangedSeriesInput, LineChart, TreeSelect
} from '../form/fields';
import { IconSelect } from "../components/icons";
import { getRawDomainName, isObject } from "@utils/ObjectUtils";
import SubTableForm from "../form/subTable/SubTableForm";
import ValueSelect from "../form/fields/ValueSelect";
import EntityAttributesComponentCell from "../form/entityAttributes/EntityAttributesComponentCell";
import { EntityAttributeValues } from "../form/entityAttributes/EntityAttributesUtils";
import FileOperatorCell from "../components/fileOperator/FileOperatorCell";
import { TableChart } from "../form/fields/TableChart";
 
const CodeEditor = React.lazy(() => import('../form/fields/CodeEditor'));
 
export const integerInputComponentRender = (props: EditableControllerProps): ReactElement => {
  const { updatable } = props;
  const readOnlyClass = updatable ? "" : "readonly";
  return (<InputNumber
    className={`integer ${readOnlyClass}`}
    style={props.style ?? EditableComponentDefaultStyle}
    precision={0}
    readOnly={!updatable} />);
};
 
export const decimalInputComponentRender = (props: EditableControllerProps): ReactElement => {
  const { updatable } = props;
  const readOnlyClass = updatable ? "" : "readonly";
  return (
    <InputNumber
      className={`decimal ${readOnlyClass}`}
      style={props.style ?? EditableComponentDefaultStyle}
      readOnly={!props.updatable} />
  );
};
 
export const stringInputComponentRender = (props: EditableControllerProps): ReactElement => {
  const { updatable, fieldValue } = props;
  return (
    <Input
      className={`string ${getReadOnlyClass(updatable)}`}
      style={props.style ?? EditableComponentDefaultStyle}
      readOnly={!props.updatable}
      suffix={<SuffixIcon updatable={updatable} />}
      allowClear={true}
      value={fieldValue === undefined ? undefined : fieldValue} />);
};
 
export const passwordInputComponentRender = (props: EditableControllerProps): ReactElement => {
  const { updatable } = props;
  return <Input.Password
    className={`password ${getReadOnlyClass(updatable)}`}
    style={props.style ?? EditableComponentDefaultStyle}
    readOnly={!props.updatable}
    iconRender={visible => (visible ? <EyeTwoTone /> : <EyeInvisibleOutlined />)}
    allowClear={true} />;
};
 
export const datetimeInputComponentRender = (showTime: boolean):
  ((props: EditableControllerProps) => ReactElement) => {
  return (props: EditableControllerProps) => (
    <DatePicker
      className="datetime"
      style={props.style ?? EditableComponentDefaultStyle}
      dropdownClassName='datetime-dropdown'
      showTime={showTime}
      disabled={!props.updatable}
      allowClear={true}
    />
  );
};
 
export const codeInputComponentRender = (tableMode?: TableMode): EditableComponentRenderFunction => {
  return (tableMode === 'finder') ? (props: EditableControllerProps) => {
    return (
      <Input className={`string ${getReadOnlyClass(props.updatable)}`}
        style={props.style ?? EditableComponentDefaultStyle}
        readOnly={!props.updatable}
        suffix={<SuffixIcon updatable={props.updatable} />}
        allowClear={true}
      />);
  } : (props: EditableControllerProps) => {
    const {
      fieldValue, form, key, updatable, column, zIndex,
      record, onValuesChange,
    } = props;
    const { extInfo } = column;
    const { codeLanguage } = extInfo ?? {};
    return (hasDetailPanel(column)) ? (<HasDetailFieldPlaceholderCell
      {...props}
      columnKey={key}
    />) : (<Suspense fallback={<div />}><CodeEditor
        value={fieldValue}
        onChange={(val) => {
          form.setFieldsValue({ [key]: val });
          onValuesChange?.({ [key]: val }, form.getFieldsValue());
        }}
        name={key}
        updatable={updatable}
        width={updatable ? "95%" : "100%"}
        mode={codeLanguage}
        zIndex={zIndex}
        record={record}
    /></Suspense>);
  };
};
 
export const textInputComponentRender = (tableMode?: TableMode): EditableComponentRenderFunction => {
  return (tableMode === 'finder') ? (props: EditableControllerProps) => (
    <Input className={`string ${getReadOnlyClass(props.updatable)}`}
      style={props.style ?? EditableComponentDefaultStyle}
      readOnly={!props.updatable}
      suffix={<SuffixIcon updatable={props.updatable} />}
      allowClear={true}
    />) : (props: EditableControllerProps) => (
      <Input.TextArea
        className="text"
        style={props.style ?? EditableComponentDefaultStyle}
        readOnly={!props.updatable}
        defaultValue={props.fieldValue}
        autoSize={{ minRows: 3 }}
      />
    );
};
 
export const booleanInputComponentRender = (props: EditableControllerProps): ReactElement => (
  <Switch className="boolean"
    defaultChecked={Boolean(props.fieldValue)}
    disabled={!props.updatable}
  />
);
 
export const percentageInputComponentRender = (props: EditableControllerProps): ReactElement => (
  <Percentage
    className="percentage"
    style={props.style ?? EditableComponentDefaultStyle}
    readOnly={!props.updatable}
  />
);
 
export const currencyInputComponentRender = (props: EditableControllerProps): ReactElement => (
  <Currency
    className="currency"
    style={props.style ?? EditableComponentDefaultStyle}
    readOnly={!props.updatable}
  />
);
 
export const tagsInputComponentRender = (props: EditableControllerProps): ReactElement => {
  const { updatable, fieldValue } = props;
  const displayVal = (fieldValue == null) ?
    "" : (typeof fieldValue === "string") ? fieldValue : fieldValue.join(",");
  return updatable ? (
    <Select
      className={`tags ${getReadOnlyClass(updatable)}`}
      mode="tags"
      style={props.style ?? EditableComponentDefaultStyle}
      tokenSeparators={[',']}
      disabled={!props.updatable}
      showSearch={true} />
  ) : <Input
      value={displayVal}
      style={props.style ?? EditableComponentDefaultStyle}
      readOnly={!updatable}
      suffix={<SuffixIcon updatable={updatable} />}
      className={getReadOnlyClass(updatable)}
    />;
};
 
export const tagListInputComponentRender = (props: EditableControllerProps): ReactElement => {
  const { updatable, fieldValue } = props;
  console.log('fieldValue', fieldValue);
  return <Select
    className={`tags ${getReadOnlyClass(updatable)}`}
    mode="tags"
    style={props.style ?? EditableComponentDefaultStyle}
    disabled={!props.updatable}
    showSearch={true}
    defaultValue={fieldValue}
  />;
};
 
export const selectComponentRender = (
  mode?: "multiple" | "tags" | undefined
): ((props: EditableControllerProps) => ReactElement) => {
  return (props: EditableControllerProps) => {
    const { key, updatable, form, fieldValue } = props;
    return (<DynamicSelect
      mode={mode}
      dfKey={key}
      style={props.style ?? EditableComponentDefaultStyle}
      form={form}
      updatable={updatable}
      currentValue={fieldValue}
      onChange={(val: string | number): void => {
        form.setFieldsValue({
          [key]: val
        });
      }}
    />);
  };
};
 
export const radioInputComponentRender = (props: EditableControllerProps): ReactElement => {
  const { key, updatable, form, fieldValue } = props;
 
  return (<DynamicCheckboxAndRadio
    type="radio"
    dfKey={key}
    style={props.style ?? EditableComponentDefaultStyle}
    form={form}
    updatable={updatable}
    currentValue={fieldValue}
    onChange={(event: ChangeEvent<HTMLInputElement>): void => {
      form.setFieldsValue({
        [key]: event?.target?.value
      });
    }}
  />);
};
 
export const checkboxInputComponentRender = (props: EditableControllerProps): ReactElement => {
  const { key, updatable, form, fieldValue } = props;
 
  return (<DynamicCheckboxAndRadio
    type="checkbox"
    dfKey={key}
    style={props.style ?? EditableComponentDefaultStyle}
    form={form}
    updatable={updatable}
    currentValue={fieldValue}
    onChange={(val: string | number): void => {
      form.setFieldsValue({
        [key]: val
      });
    }}
  />);
};
 
export const enumInputComponentRender = (props: EditableControllerProps): ReactElement => {
  const { updatable, column, type, form, key } = props;
  return (
    <EnumSelect
      style={props.style ?? EditableComponentDefaultStyle}
      updatable={updatable}
      type={column.enumType ?? type}
      placeholder={SearchPlaceholderText}
      notFoundContent={SearchNoDataFoundText}
      onSelect={(val: string) => form.setFieldsValue({ [key]: val })}
      options={props.options} />
  );
};
 
export const objectInputComponentRender = (props: EditableControllerProps): ReactElement => {
  const {
    updatable, type, form, key, fieldValue, style,
    domainName, onValuesChange, options, zIndex,
    multiple, column,
  } = props;
 
  const convertDefaultValue = (): undefined | string | number | Array<number> => {
    Iif (multiple) {
      return fieldValue;
    }
    Iif (isObject(fieldValue)) {
      return fieldValue.id;
    }
    const intValue = parseInt(fieldValue);
    if (isNaN(intValue)) {
      return undefined;
    }
    return intValue;
  };
 
  if (column.isDynamicField || column.type === 'object' || isObjectType(column.type)) {
    const defaultValue = convertDefaultValue();
    const mode = multiple ? "multiple" : undefined;
 
    return (
      <ObjectSelect
        column={column}
        options={options}
        ownerClass={domainName}
        fieldName={key}
        updatable={updatable}
        domainName={column.elementDomain ?? type}
        placeholder={SearchPlaceholderText}
        notFoundContent={SearchNoDataFoundText}
        defaultValue={defaultValue}
        value={defaultValue}
        form={form}
        onValuesChange={onValuesChange}
        onSelect={(val: number) => form.setFieldsValue({ [key]: val })}
        zIndex={zIndex}
        mode={mode}
        style={style}
      />
    );
  } else E{
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const defaultValue = fieldValue?.map((v: any) => v.id);
    form.setFieldValue(key, defaultValue);
    return (
      <ObjectSelect
        column={column}
        ownerClass={domainName}
        fieldName={key}
        updatable={updatable}
        domainName={
          column.elementDomain ??
          // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
          column.elementType!}
        placeholder={SearchPlaceholderText}
        notFoundContent={SearchNoDataFoundText}
        defaultValue={defaultValue}
        mode="multiple"
        form={form}
        style={style}
        onValuesChange={onValuesChange}
        zIndex={zIndex}
      />);
  }
};
 
export const httpMethodInputComponentRender = (props: EditableControllerProps): ReactElement => {
  const { fieldValue, key, form, updatable, options } = props;
  return (<HttpMethodSelect
    style={props.style ?? EditableComponentDefaultStyle}
    value={fieldValue}
    updatable={updatable}
    onSelect={(val: string) => {
      form.setFieldsValue({ [key]: val });
    }}
    options={options}
  />);
};
 
export const rolesInputComponentRender = (props: EditableControllerProps): ReactElement => {
  const {
    updatable, form, key, fieldValue, style,
    domainName, zIndex, column, type
  } = props;
  return (
    <ObjectSelect
      column={column}
      className={`roles ${getReadOnlyClass(updatable)}`}
      ownerClass={domainName}
      fieldName={key}
      updatable={updatable}
      domainName={getRawDomainName(type)}
      placeholder={SearchPlaceholderText}
      notFoundContent={SearchNoDataFoundText}
      defaultValue={fieldValue}
      value={fieldValue}
      mode="multiple"
      onChange={(val: string) => form.setFieldsValue({ [key]: val })}
      labelField="authority"
      form={form}
      style={style}
      zIndex={zIndex} />);
};
 
export const staticFieldInputComponentRender = (props: EditableControllerProps): ReactElement => {
  const { updatable, fieldValue, key, form, record, options } = props;
  return (
    <StaticFieldSelect
      style={props.style ?? EditableComponentDefaultStyle}
      onChange={(val: string) => form.setFieldsValue({ [key]: val })}
      record={record}
      value={fieldValue}
      options={options}
      key={key}
      updatable={updatable} />
  );
};
 
export const seriesInputComponentRender = (props: EditableControllerProps): ReactElement => {
  const { fieldValue, key, form } = props;
  return (
    <RangedSeriesInput
      style={props.style ?? EditableComponentDefaultStyle}
      value={fieldValue}
      max={2.4}
      labelSuffix={'  mm'}
      valueSuffix={'%'}
      onChange={(val: string) => form.setFieldsValue({ [key]: val })} />
  );
};
 
export const arrayInputComponentRender = (props: EditableControllerProps): ReactElement => {
  const { key, column } = props;
  const { elementType } = column;
  return (elementType === FileFieldType) ? (
    (<FileOperator
      {...props}
      columnKey={key}
      multiple={true}
      column={column}
    />)
  ) : (<HasDetailFieldPlaceholderCell
    {...props}
    columnKey={key}
  />);
};
 
export const lineChartInputComponentRender = (props: EditableControllerProps): ReactElement => {
  const { fieldValue, column } = props;
  return (
    <LineChart
      value={fieldValue}
      column={column}
    />
  );
};
 
export const tableChartInputComponentRender = (props: EditableControllerProps): ReactElement => {
  const { fieldValue, column } = props;
  return (
    <TableChart
      data={fieldValue}
      column={column}
    />
  );
};
 
export const treeSelectInputComponentRender = (props: EditableControllerProps): ReactElement => {
  const { fieldValue, key, form, column, updatable } = props;
  const { key: columnKey } = column;
  return (
    <TreeSelect
      updatable={updatable}
      columnKey={columnKey}
      style={props.style}
      value={fieldValue}
      onChange={(val: string) => form.setFieldsValue({ [key]: val })} />
  );
};
 
export const idInputComponentRender = (): ReactElement => (<Input className="id" type="hidden" />);
 
export const fileOperatorComponentRender = (props: EditableControllerProps): ReactElement => {
  const { page } = props;
 
  const fileOperatorProps = {
    ...props,
    columnKey: props.key,
    multiple: props.column.multiple ?? false,
    column: props.column,
  };
  if (page === 'LIST') {
    return <FileOperatorCell {...fileOperatorProps}/>;
  } else {
    return <FileOperator {...fileOperatorProps}/>;
  }
};
 
export const imageOperatorComponentRender = (props: EditableControllerProps): ReactElement => {
  return (<SingleImage {...props} />);
};
 
export const hasDetailColumnEditComponentRender = (props: EditableControllerProps): ReactElement => {
  const { key } = props;
  return (<HasDetailFieldPlaceholderCell
    {...props}
    columnKey={key}
  />);
};
 
export const commentsComponentRender = (props: EditableControllerProps): ReactElement => {
  return (<CommentsIcon {...props} />);
};
 
 
export const iconInputComponentRender = (props: EditableControllerProps): ReactElement => {
  const { updatable, fieldValue, zIndex, onValuesChange, form, key } = props;
 
  return (
    <IconSelect
      value={fieldValue === undefined ? undefined : String(fieldValue)}
      onChange={(value) => onValuesChange?.({[key]: value}, form?.getFieldsValue())}
      readonly={!updatable}
      zIndex={zIndex}
    />
  );
};
 
 
export const subTableComponentRender = (props: EditableControllerProps): ReactElement => {
  const {
    column, updatable, form, domainName, record,
    zIndex, saveOptions, onValuesChange
  } = props;
 
  return (
    <SubTableForm
      column={column}
      owner={record}
      editMode={updatable}
      ownerClass={domainName}
      zIndex={zIndex}
      form={form}
      saveOptions={saveOptions}
      isCurrentActiveTab={true}
      onValuesChange={onValuesChange}
    />
  );
};
 
export const valueSelectComponentRender = (props: EditableControllerProps): ReactElement => {
  const { updatable, form, key, column } = props;
  return (
    <ValueSelect
      style={props.style ?? EditableComponentDefaultStyle}
      updatable={updatable}
      placeholder={SearchPlaceholderText}
      notFoundContent={SearchNoDataFoundText}
      onSelect={(val) => form.setFieldsValue({ [key]: val })}
      options={column.options}
    />
  );
};
 
export const entityAttributesComponentRender = (props: EditableControllerProps): ReactElement => {
  const {
    updatable, form, column, fieldValue, record,
    zIndex, domainName, onValuesChange, saveOptions,
    path,
  } = props;
  return (
    <EntityAttributesComponentCell
      value={fieldValue as EntityAttributeValues}
      column={column}
      owner={record}
      ownerClass={domainName}
      zIndex={zIndex}
      editable={updatable}
      form={form}
      onValuesChange={onValuesChange}
      saveOptions={saveOptions}
      path={path}
    />
  );
};