• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

teableio / teable / 8536869866

03 Apr 2024 10:05AM CUT coverage: 21.234% (-0.3%) from 21.535%
8536869866

Pull #514

github

web-flow
Merge 91a25d710 into 45ee7ebb3
Pull Request #514: refactor: user and link selector

1394 of 2532 branches covered (55.06%)

27 of 1620 new or added lines in 60 files covered. (1.67%)

4 existing lines in 2 files now uncovered.

14588 of 68702 relevant lines covered (21.23%)

2.02 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/packages/sdk/src/components/filter/condition/Condition.tsx
NEW
1
import { FieldType, getValidFilterOperators } from '@teable/core';
×
NEW
2
import type { IOperator, IFilterItem } from '@teable/core';
×
3

×
4
import { Trash2 } from '@teable/icons';
×
5
import { Button } from '@teable/ui-lib';
×
6

×
7
import { isEqual } from 'lodash';
×
8
import { useContext, useMemo } from 'react';
×
9

×
NEW
10
import type { IFieldInstance } from '../../../model';
×
11
import { FilterContext } from '../context';
×
12
import type { IConditionProps } from '../types';
×
13
import { Conjunction } from './Conjunction';
×
14
import { FieldSelect } from './FieldSelect';
×
15
import { FieldValue } from './FieldValue';
×
16
import { OperatorSelect } from './OperatorSelect';
×
17

×
18
function Condition(props: IConditionProps) {
×
19
  const { index, filter, path, conjunction } = props;
×
20
  const { fieldId, value, operator } = filter;
×
21
  const context = useContext(FilterContext);
×
NEW
22
  const { setFilters, deleteCondition, fields } = context;
×
23
  const fieldMap = useMemo(() => {
×
NEW
24
    const map: Record<string, IFieldInstance> = {};
×
25
    fields.forEach((field) => {
×
26
      const key = field.id;
×
NEW
27
      const value = field;
×
28
      map[key] = value;
×
29
    });
×
30
    return map;
×
31
  }, [fields]);
×
32

×
33
  const fieldTypeHandler = (newFieldId: string | null) => {
×
NEW
34
    const newField = fieldMap[newFieldId!];
×
NEW
35
    const newFieldType = newField.type;
×
NEW
36
    const currentFieldType = fieldMap[fieldId].type || null;
×
37
    const newFieldPath = [...path, 'fieldId'];
×
38
    const newValuePath = [...path, 'value'];
×
39
    // different type should reset value to null.
×
NEW
40
    if (
×
NEW
41
      newFieldType !== currentFieldType ||
×
NEW
42
      [FieldType.Link, FieldType.SingleSelect, FieldType.MultipleSelect].includes(currentFieldType)
×
NEW
43
    ) {
×
44
      setFilters(newValuePath, null);
×
45
    }
×
NEW
46
    // repair operator if not valid
×
NEW
47
    const newOperators = newField ? getValidFilterOperators(newField) : null;
×
NEW
48
    if (!newOperators?.includes(operator as IOperator)) {
×
NEW
49
      const newPath = [...path, 'operator'];
×
NEW
50
      console.log('setFilters', newPath, newOperators?.[0] ?? null);
×
NEW
51
      setFilters(newPath, newOperators?.[0] ?? null);
×
NEW
52
    }
×
53
    setFilters(newFieldPath, newFieldId);
×
54
  };
×
55
  const operatorHandler = (value: string | null) => {
×
56
    if (operator !== value) {
×
57
      const newPath = [...path, 'operator'];
×
58
      setFilters(newPath, value);
×
59
    }
×
60
  };
×
61
  const fieldValueHandler = (newValue: IFilterItem['value']) => {
×
62
    if (isEqual(value, newValue)) {
×
63
      return;
×
64
    }
×
65

×
66
    let mergedValue = null;
×
67

×
68
    // empty array and string should be null!
×
69
    if (newValue !== '' && !(Array.isArray(newValue) && !newValue.length)) {
×
70
      mergedValue = newValue;
×
71
    }
×
72

×
73
    const newPath = [...path, 'value'];
×
74
    setFilters(newPath, mergedValue);
×
75
  };
×
76

×
NEW
77
  if (fields.length > 0 && !fields.some((field) => field.id === fieldId)) {
×
NEW
78
    deleteCondition(path, index);
×
NEW
79
  }
×
NEW
80

×
81
  return (
×
82
    <div className="my-1 flex items-center px-1">
×
83
      <Conjunction
×
84
        index={index}
×
85
        value={conjunction}
×
86
        onSelect={(value) => {
×
87
          const newPath = [...path];
×
88
          newPath.splice(-2, 2, 'conjunction');
×
89
          setFilters(newPath, value);
×
90
        }}
×
91
      ></Conjunction>
×
92

×
93
      <section className="flex items-center pl-1">
×
94
        <FieldSelect fieldId={fieldId} onSelect={fieldTypeHandler} />
×
95

×
96
        <OperatorSelect value={operator} fieldId={fieldId} onSelect={operatorHandler} />
×
97

×
98
        <FieldValue filter={filter} onSelect={fieldValueHandler}></FieldValue>
×
99

×
100
        <Button
×
101
          variant="outline"
×
102
          size="sm"
×
103
          onClick={() => deleteCondition(path, index)}
×
104
          className="ml-1"
×
105
        >
×
106
          <Trash2 className="size-4"></Trash2>
×
107
        </Button>
×
108
      </section>
×
109
    </div>
×
110
  );
×
111
}
×
112

×
UNCOV
113
export { Condition };
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc