Skip to content

Commit 443442c

Browse files
authored
Merge pull request #332 from Deniz97/master
fix: non-ASCII characters at cell focus and tests
2 parents ee80c25 + 56c525a commit 443442c

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

src/components/DataSheetGrid.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import deepEqual from 'fast-deep-equal'
2828
import { ContextMenu } from './ContextMenu'
2929
import {
3030
encodeHtml,
31+
isPrintableUnicode,
3132
parseTextHtmlData,
3233
parseTextPlainData,
3334
} from '../utils/copyPasting'
@@ -1456,7 +1457,7 @@ export const DataSheetGrid = React.memo(
14561457
)
14571458
event.preventDefault()
14581459
} else if (
1459-
(event.key.match(/^[ -~]$/) || event.code.match(/Key[A-Z\p{L}]$/u)) &&
1460+
(isPrintableUnicode(event.key) || event.code.match(/Key[A-Z]$/)) &&
14601461
!event.ctrlKey &&
14611462
!event.metaKey &&
14621463
!event.altKey

src/utils/copyPasting.test.ts

+21
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
parseTextPlainData,
33
parseTextHtmlData,
44
encodeHtml,
5+
isPrintableUnicode,
56
} from './copyPasting'
67
import { JSDOM } from 'jsdom'
78

@@ -157,3 +158,23 @@ test('encodeHtml', () => {
157158
'<div title="foo'bar">baz</div>'
158159
)
159160
})
161+
162+
test('isPrintableUnicode', () => {
163+
expect(isPrintableUnicode('a')).toBe(true)
164+
expect(isPrintableUnicode('ş')).toBe(true)
165+
expect(isPrintableUnicode('Ğ')).toBe(true)
166+
expect(isPrintableUnicode('中')).toBe(true)
167+
expect(isPrintableUnicode('©')).toBe(true)
168+
expect(isPrintableUnicode('5')).toBe(true)
169+
expect(isPrintableUnicode('!')).toBe(true)
170+
expect(isPrintableUnicode('.')).toBe(true)
171+
expect(isPrintableUnicode(':')).toBe(true)
172+
expect(isPrintableUnicode('[')).toBe(true)
173+
expect(isPrintableUnicode('\x0B')).toBe(false) // Vertical Tab
174+
expect(isPrintableUnicode('\x7F')).toBe(false) // Delete
175+
expect(isPrintableUnicode('\r')).toBe(false)
176+
expect(isPrintableUnicode(' ')).toBe(false)
177+
expect(isPrintableUnicode('\t')).toBe(false)
178+
expect(isPrintableUnicode('\n')).toBe(false)
179+
expect(isPrintableUnicode('\x90')).toBe(false) // Non-printable character in the extended ASCII range
180+
})

src/utils/copyPasting.ts

+4
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,7 @@ export const encodeHtml = (str: string) => {
103103
.replace(/"/g, '"')
104104
.replace(/'/g, ''')
105105
}
106+
107+
export const isPrintableUnicode = (str: string): boolean => {
108+
return str.match(/^[^\x00-\x20\x7F-\x9F]$/) !== null
109+
}

tests/editTextCell.test.tsx

+20
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,26 @@ test('Enter to edit', () => {
116116
})
117117
})
118118

119+
test('Non-ascii character to edit', () => {
120+
const ref = { current: null as unknown as DataSheetGridRef }
121+
const data = {
122+
current: [
123+
{ firstName: 'Elon', lastName: 'Musk' },
124+
{ firstName: 'Jeff', lastName: 'Bezos' },
125+
],
126+
}
127+
128+
render(<DataWrapper dataRef={data} dsgRef={ref} columns={columns} />)
129+
130+
act(() => ref.current.setActiveCell({ col: 0, row: 1 }))
131+
132+
userEvent.keyboard('ş')
133+
expect(data.current).toEqual([
134+
{ firstName: 'Elon', lastName: 'Musk' },
135+
{ firstName: 'ş', lastName: 'Bezos' },
136+
])
137+
})
138+
119139
test('Lazy cell validate with Enter', () => {
120140
const ref = { current: null as unknown as DataSheetGridRef }
121141
const data = {

0 commit comments

Comments
 (0)