From 605a157e2d27a33ba99c7bb5794993fbec2c576b Mon Sep 17 00:00:00 2001 From: Deniz97 Date: Fri, 19 Jan 2024 16:08:50 +0300 Subject: [PATCH 1/2] fix: non-ASCII characters at cell focus and tests --- src/components/DataSheetGrid.tsx | 3 ++- src/utils/copyPasting.test.ts | 21 +++++++++++++++++++++ src/utils/copyPasting.ts | 4 ++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/components/DataSheetGrid.tsx b/src/components/DataSheetGrid.tsx index 4a38007..cd3aa07 100644 --- a/src/components/DataSheetGrid.tsx +++ b/src/components/DataSheetGrid.tsx @@ -28,6 +28,7 @@ import deepEqual from 'fast-deep-equal' import { ContextMenu } from './ContextMenu' import { encodeHtml, + isPrintableUnicode, parseTextHtmlData, parseTextPlainData, } from '../utils/copyPasting' @@ -1456,7 +1457,7 @@ export const DataSheetGrid = React.memo( ) event.preventDefault() } else if ( - (event.key.match(/^[ -~]$/) || event.code.match(/Key[A-Z\p{L}]$/u)) && + (isPrintableUnicode(event.key) || event.code.match(/Key[A-Z]$/)) && !event.ctrlKey && !event.metaKey && !event.altKey diff --git a/src/utils/copyPasting.test.ts b/src/utils/copyPasting.test.ts index 7f35922..876bfe0 100644 --- a/src/utils/copyPasting.test.ts +++ b/src/utils/copyPasting.test.ts @@ -2,6 +2,7 @@ import { parseTextPlainData, parseTextHtmlData, encodeHtml, + isPrintableUnicode, } from './copyPasting' import { JSDOM } from 'jsdom' @@ -157,3 +158,23 @@ test('encodeHtml', () => { '<div title="foo'bar">baz</div>' ) }) + +test('isPrintableUnicode', () => { + expect(isPrintableUnicode('a')).toBe(true) + expect(isPrintableUnicode('ş')).toBe(true) + expect(isPrintableUnicode('Ğ')).toBe(true) + expect(isPrintableUnicode('中')).toBe(true) + expect(isPrintableUnicode('©')).toBe(true) + expect(isPrintableUnicode('5')).toBe(true) + expect(isPrintableUnicode('!')).toBe(true) + expect(isPrintableUnicode('.')).toBe(true) + expect(isPrintableUnicode(':')).toBe(true) + expect(isPrintableUnicode('[')).toBe(true) + expect(isPrintableUnicode('\x0B')).toBe(false) // Vertical Tab + expect(isPrintableUnicode('\x7F')).toBe(false) // Delete + expect(isPrintableUnicode('\r')).toBe(false) + expect(isPrintableUnicode(' ')).toBe(false) + expect(isPrintableUnicode('\t')).toBe(false) + expect(isPrintableUnicode('\n')).toBe(false) + expect(isPrintableUnicode('\x90')).toBe(false) // Non-printable character in the extended ASCII range +}) diff --git a/src/utils/copyPasting.ts b/src/utils/copyPasting.ts index 5fbc540..febf0bd 100644 --- a/src/utils/copyPasting.ts +++ b/src/utils/copyPasting.ts @@ -103,3 +103,7 @@ export const encodeHtml = (str: string) => { .replace(/"/g, '"') .replace(/'/g, ''') } + +export const isPrintableUnicode = (str: string): boolean => { + return str.match(/^[^\x00-\x20\x7F-\x9F]$/) !== null +} From 56c525ae8dd75505fd0e305e4c0a2dbca8446896 Mon Sep 17 00:00:00 2001 From: Deniz97 Date: Fri, 19 Jan 2024 16:15:53 +0300 Subject: [PATCH 2/2] add component test --- tests/editTextCell.test.tsx | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/editTextCell.test.tsx b/tests/editTextCell.test.tsx index 2c9faf5..fd84315 100644 --- a/tests/editTextCell.test.tsx +++ b/tests/editTextCell.test.tsx @@ -116,6 +116,26 @@ test('Enter to edit', () => { }) }) +test('Non-ascii character to edit', () => { + const ref = { current: null as unknown as DataSheetGridRef } + const data = { + current: [ + { firstName: 'Elon', lastName: 'Musk' }, + { firstName: 'Jeff', lastName: 'Bezos' }, + ], + } + + render() + + act(() => ref.current.setActiveCell({ col: 0, row: 1 })) + + userEvent.keyboard('ş') + expect(data.current).toEqual([ + { firstName: 'Elon', lastName: 'Musk' }, + { firstName: 'ş', lastName: 'Bezos' }, + ]) +}) + test('Lazy cell validate with Enter', () => { const ref = { current: null as unknown as DataSheetGridRef } const data = {