Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: non-ASCII characters at cell focus and tests #332

Merged
merged 2 commits into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading