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

teableio / teable / 10057009462

23 Jul 2024 10:11AM UTC coverage: 17.811% (-0.04%) from 17.849%
10057009462

Pull #748

github

web-flow
Merge dec0c237f into 8d891a1e3
Pull Request #748: feat: copy and paste operations for pre-filled rows in the grid

1384 of 2805 branches covered (49.34%)

0 of 369 new or added lines in 8 files covered. (0.0%)

1 existing line in 1 file now uncovered.

14014 of 78680 relevant lines covered (17.81%)

1.77 hits per line

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

0.0
/packages/sdk/src/components/grid-enhancements/hooks/use-grid-prefilling-row.ts
1
import type { IUpdateOrderRo } from '@teable/openapi';
×
2
import { isEqual } from 'lodash';
×
3
import { useCallback, useMemo, useState } from 'react';
×
4
import { useFieldCellEditable, useFields } from '../../../hooks';
×
5
import { createRecordInstance } from '../../../model';
×
6
import { CellType } from '../../grid/interface';
×
7
import type { ICell, ICellItem, IGridColumn, IInnerCell } from '../../grid/interface';
×
8
import { createCellValue2GridDisplay } from './use-grid-columns';
×
9

×
10
export const useGridPrefillingRow = (columns: (IGridColumn & { id: string })[]) => {
×
11
  const fields = useFields();
×
12
  const fieldEditable = useFieldCellEditable();
×
13

×
14
  const [prefillingRowOrder, setPrefillingRowOrder] = useState<IUpdateOrderRo>();
×
15
  const [prefillingRowIndex, setPrefillingRowIndex] = useState<number>();
×
16
  const [prefillingFieldValueMap, setPrefillingFieldValueMap] = useState<
×
17
    { [fieldId: string]: unknown } | undefined
×
18
  >();
×
19

×
20
  const localRecord = useMemo(() => {
×
21
    if (prefillingFieldValueMap == null) {
×
22
      return null;
×
23
    }
×
24

×
25
    const record = createRecordInstance({
×
26
      id: '',
×
27
      fields: prefillingFieldValueMap,
×
28
    });
×
29
    record.getCellValue = (fieldId: string) => {
×
30
      return prefillingFieldValueMap[fieldId];
×
31
    };
×
32
    record.updateCell = (fieldId: string, newValue: unknown) => {
×
33
      record.fields[fieldId] = newValue;
×
34
      setPrefillingFieldValueMap({
×
35
        ...prefillingFieldValueMap,
×
36
        [fieldId]: newValue,
×
37
      });
×
38
      return Promise.resolve();
×
39
    };
×
40

×
41
    return record;
×
42
  }, [prefillingFieldValueMap]);
×
43

×
44
  const getPrefillingCellContent = useCallback<(cell: ICellItem) => ICell>(
×
45
    (cell) => {
×
46
      const [columnIndex] = cell;
×
47
      const cellValue2GridDisplay = createCellValue2GridDisplay(fields, fieldEditable);
×
48
      if (localRecord != null) {
×
49
        const fieldId = columns[columnIndex]?.id;
×
50
        if (!fieldId) return { type: CellType.Loading };
×
51
        return cellValue2GridDisplay(localRecord, columnIndex);
×
52
      }
×
53
      return { type: CellType.Loading };
×
54
    },
×
55
    [columns, fieldEditable, fields, localRecord]
×
56
  );
×
57

×
58
  const onPrefillingCellEdited = useCallback(
×
59
    (cell: ICellItem, newVal: IInnerCell) => {
×
60
      if (localRecord == null) return;
×
61

×
62
      const [col] = cell;
×
63
      const fieldId = columns[col].id;
×
64
      const { type, data } = newVal;
×
65
      let newCellValue: unknown = null;
×
66

×
67
      switch (type) {
×
68
        case CellType.Select:
×
69
          newCellValue = data?.length ? data : null;
×
70
          break;
×
71
        case CellType.Text:
×
72
        case CellType.Number:
×
73
        case CellType.Boolean:
×
74
        default:
×
75
          newCellValue = data === '' ? null : data;
×
76
      }
×
77
      const oldCellValue = localRecord.getCellValue(fieldId) ?? null;
×
78
      if (isEqual(newCellValue, oldCellValue)) return;
×
79
      localRecord.updateCell(fieldId, newCellValue);
×
80
      return localRecord;
×
81
    },
×
82
    [localRecord, columns]
×
83
  );
×
84

×
85
  return useMemo(() => {
×
86
    return {
×
NEW
87
      localRecord,
×
88
      prefillingRowIndex,
×
89
      prefillingRowOrder,
×
90
      prefillingFieldValueMap,
×
91
      setPrefillingRowIndex,
×
92
      setPrefillingRowOrder,
×
93
      onPrefillingCellEdited,
×
94
      getPrefillingCellContent,
×
95
      setPrefillingFieldValueMap,
×
96
    };
×
97
  }, [
×
NEW
98
    localRecord,
×
99
    prefillingRowIndex,
×
100
    prefillingRowOrder,
×
101
    prefillingFieldValueMap,
×
102
    setPrefillingRowIndex,
×
103
    setPrefillingRowOrder,
×
104
    onPrefillingCellEdited,
×
105
    getPrefillingCellContent,
×
106
    setPrefillingFieldValueMap,
×
107
  ]);
×
108
};
×
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