Skip to content

Commit 09a3d6a

Browse files
committed
Add prePasteValues column prop
1 parent 1acd488 commit 09a3d6a

File tree

14 files changed

+65
-32
lines changed

14 files changed

+65
-32
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Change Log
22

3+
## 3.6.0
4+
> Date: 2021-12-30
5+
### Added
6+
- New column prop `prePasteValues`
7+
38
## 3.5.0
49
> Date: 2021-12-26
510
### Added

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-datasheet-grid",
3-
"version": "3.5.0",
3+
"version": "3.6.0",
44
"description": "An Excel-like React component to create beautiful spreadsheets.",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/columns/checkboxColumn.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const CheckboxComponent = React.memo<CellProps<boolean, any>>(
5252

5353
CheckboxComponent.displayName = 'CheckboxComponent'
5454

55-
export const checkboxColumn: Partial<Column<boolean, any>> = {
55+
export const checkboxColumn: Partial<Column<boolean, any, string>> = {
5656
component: CheckboxComponent as CellComponent<boolean, any>,
5757
deleteValue: () => false,
5858
// We can customize what value is copied: when the checkbox is checked we copy YES, otherwise we copy NO

src/columns/dateColumn.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const DateComponent = React.memo<CellProps<Date | null, any>>(
4141

4242
DateComponent.displayName = 'DateComponent'
4343

44-
export const dateColumn: Partial<Column<Date | null, any>> = {
44+
export const dateColumn: Partial<Column<Date | null, any, string>> = {
4545
component: DateComponent as CellComponent<Date | null, any>,
4646
deleteValue: () => null,
4747
// We convert the date to a string for copying using toISOString

src/columns/keyColumn.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useCallback, useRef } from 'react'
22
import { CellComponent, Column } from '../types'
33

4-
type ColumnData = { key: string; original: Partial<Column<any, any>> }
4+
type ColumnData = { key: string; original: Partial<Column<any, any, any>> }
55

66
const KeyComponent: CellComponent<any, ColumnData> = ({
77
columnData: { key, original },
@@ -41,11 +41,12 @@ const KeyComponent: CellComponent<any, ColumnData> = ({
4141

4242
export const keyColumn = <
4343
T extends Record<string, any>,
44-
K extends keyof T = keyof T
44+
K extends keyof T = keyof T,
45+
PasteValue = string
4546
>(
4647
key: K,
47-
column: Partial<Column<T[K], any>>
48-
): Partial<Column<T, ColumnData>> => ({
48+
column: Partial<Column<T[K], any, PasteValue>>
49+
): Partial<Column<T, ColumnData, PasteValue>> => ({
4950
id: key as string,
5051
...column,
5152
// We pass the key and the original column as columnData to be able to retrieve them in the cell component

src/columns/textColumn.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ export function createTextColumn<T = string | null>({
158158
formatForCopy = (value) => String(value ?? ''),
159159
parsePastedValue = (value) =>
160160
(value.replace(/[\n\r]+/g, ' ').trim() || (null as unknown)) as T,
161-
}: TextColumnOptions<T> = {}): Partial<Column<T, TextColumnData<T>>> {
161+
}: TextColumnOptions<T> = {}): Partial<Column<T, TextColumnData<T>, string>> {
162162
return {
163163
component: TextComponent as unknown as CellComponent<T, TextColumnData<T>>,
164164
columnData: {

src/components/Cell.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const Cell: FC<{
66
gutter: boolean
77
stickyRight: boolean
88
disabled?: boolean
9-
column: Column<any, any>
9+
column: Column<any, any, any>
1010
className: string
1111
active?: boolean
1212
}> = ({

src/components/DataSheetGrid.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import { encode as encodeHtml } from 'html-entities'
4747
import { getAllTabbableElements } from '../utils/tab'
4848

4949
const DEFAULT_DATA: any[] = []
50-
const DEFAULT_COLUMNS: Column<any, any>[] = []
50+
const DEFAULT_COLUMNS: Column<any, any, any>[] = []
5151
const DEFAULT_CREATE_ROW: DataSheetGridProps<any>['createRow'] = () => ({})
5252
const DEFAULT_EMPTY_CALLBACK: () => void = () => null
5353
const DEFAULT_DUPLICATE_ROW: DataSheetGridProps<any>['duplicateRow'] = ({
@@ -676,6 +676,20 @@ export const DataSheetGrid = React.memo(
676676
const min: Cell = selection?.min || activeCell
677677
const max: Cell = selection?.max || activeCell
678678

679+
const results = await Promise.all(
680+
pasteData[0].map((_, columnIndex) => {
681+
const prePasteValues =
682+
columns[min.col + columnIndex + 1]?.prePasteValues
683+
684+
const values = pasteData.map((row) => row[columnIndex])
685+
return prePasteValues?.(values) ?? values
686+
})
687+
)
688+
689+
pasteData = pasteData.map((_, rowIndex) =>
690+
results.map((column) => column[rowIndex])
691+
)
692+
679693
// Paste single row
680694
if (pasteData.length === 1) {
681695
const newData = [...data]

src/hooks/useColumnWidths.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useEffect, useMemo, useState } from 'react'
33
import { useDeepEqualState } from './useDeepEqualState'
44

55
export const useColumnWidths = (
6-
columns: Column<any, any>[],
6+
columns: Column<any, any, any>[],
77
width?: number
88
) => {
99
const [columnWidths, setColumnWidths] = useDeepEqualState<

src/hooks/useColumns.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ const defaultGutterComponent = ({ rowIndex }: CellProps<any, any>) => (
99
<>{rowIndex + 1}</>
1010
)
1111
const cellAlwaysEmpty = () => true
12+
const defaultPrePasteValues = (values: string[]) => values
1213

1314
export const useColumns = <T extends any>(
14-
columns: Partial<Column<T, any>>[],
15+
columns: Partial<Column<T, any, any>>[],
1516
gutterColumn?: SimpleColumn<T, any> | false,
1617
stickyRightColumn?: SimpleColumn<T, any>
17-
): Column<T, any>[] => {
18-
return useMemo<Column<T, any>[]>(() => {
19-
const partialColumns: Partial<Column<T, any>>[] = [
18+
): Column<T, any, any>[] => {
19+
return useMemo<Column<T, any, any>[]>(() => {
20+
const partialColumns: Partial<Column<T, any, any>>[] = [
2021
gutterColumn === false
2122
? {
2223
width: 0,
@@ -49,7 +50,7 @@ export const useColumns = <T extends any>(
4950
})
5051
}
5152

52-
return partialColumns.map<Column<T, any>>((column) => ({
53+
return partialColumns.map<Column<T, any, any>>((column) => ({
5354
...column,
5455
width: column.width ?? 1,
5556
minWidth: column.minWidth ?? 100,
@@ -61,6 +62,7 @@ export const useColumns = <T extends any>(
6162
deleteValue: column.deleteValue ?? identityRow,
6263
copyValue: column.copyValue ?? defaultCopyValue,
6364
pasteValue: column.pasteValue ?? identityRow,
65+
prePasteValues: column.prePasteValues ?? defaultPrePasteValues,
6466
isCellEmpty: column.isCellEmpty ?? defaultIsCellEmpty,
6567
}))
6668
}, [gutterColumn, stickyRightColumn, columns])

0 commit comments

Comments
 (0)