Skip to content

Commit

Permalink
Merge pull request #332 from Deniz97/master
Browse files Browse the repository at this point in the history
fix: non-ASCII characters at cell focus and tests
  • Loading branch information
nick-keller authored Jan 27, 2024
2 parents ee80c25 + 56c525a commit 443442c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/components/DataSheetGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import deepEqual from 'fast-deep-equal'
import { ContextMenu } from './ContextMenu'
import {
encodeHtml,
isPrintableUnicode,
parseTextHtmlData,
parseTextPlainData,
} from '../utils/copyPasting'
Expand Down Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions src/utils/copyPasting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
parseTextPlainData,
parseTextHtmlData,
encodeHtml,
isPrintableUnicode,
} from './copyPasting'
import { JSDOM } from 'jsdom'

Expand Down Expand Up @@ -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
})
4 changes: 4 additions & 0 deletions src/utils/copyPasting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
20 changes: 20 additions & 0 deletions tests/editTextCell.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(<DataWrapper dataRef={data} dsgRef={ref} columns={columns} />)

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 = {
Expand Down

0 comments on commit 443442c

Please sign in to comment.