diff --git a/src/components/dialogs/EmailDialog/components/TokenField.test.tsx b/src/components/dialogs/EmailDialog/components/TokenField.test.tsx index 27f6eb543..1848217a0 100644 --- a/src/components/dialogs/EmailDialog/components/TokenField.test.tsx +++ b/src/components/dialogs/EmailDialog/components/TokenField.test.tsx @@ -55,10 +55,35 @@ describe('TokenField', () => { it('strips characters outside the base32 alphabet', () => { const onChangeText = jest.fn() render() - fireEvent.changeText(getInput(), 'a1b0c8d-9e') + fireEvent.changeText(getInput(), 'a!b8c-9d e') expect(onChangeText).toHaveBeenCalledWith('ABCDE') }) + it('maps 0 and 1 to the letters O and I', () => { + const onChangeText = jest.fn() + render() + fireEvent.changeText(getInput(), 'e6kdf-jrk70') + expect(onChangeText).toHaveBeenCalledWith('E6KDF-JRK7O') + fireEvent.changeText(getInput(), '1bcde') + expect(onChangeText).toHaveBeenCalledWith('IBCDE') + }) + + it('keeps a hyphen typed after the first group', () => { + const onChangeText = jest.fn() + render() + fireEvent.changeText(getInput(), 'ABCDE-') + expect(onChangeText).toHaveBeenCalledWith('ABCDE-') + fireEvent.changeText(getInput(), 'ABCDE-F') + expect(onChangeText).toHaveBeenCalledWith('ABCDE-F') + }) + + it('drops a hyphen typed inside the first group', () => { + const onChangeText = jest.fn() + render() + fireEvent.changeText(getInput(), 'AB-') + expect(onChangeText).toHaveBeenCalledWith('AB') + }) + it('defaults autocomplete to off', () => { render() expect(getInput().props.autoComplete).toBe('off') diff --git a/src/components/dialogs/EmailDialog/components/TokenField.tsx b/src/components/dialogs/EmailDialog/components/TokenField.tsx index 707787073..faf6e4f41 100644 --- a/src/components/dialogs/EmailDialog/components/TokenField.tsx +++ b/src/components/dialogs/EmailDialog/components/TokenField.tsx @@ -4,9 +4,22 @@ import {useLingui} from '@lingui/react/macro' import * as TextField from '#/components/forms/TextField' import {Shield_Stroke2_Corner0_Rounded as Shield} from '#/components/icons/Shield' +/** + * Codes are base32 (A-Z and 2-7), so the digits 0 and 1 never appear in one. + * People read them off the letters O and I, though, so map them back rather + * than silently dropping the keypress. + */ export function normalizeCode(value: string) { - const normalized = value.toUpperCase().replace(/[^A-Z2-7]/g, '') - if (normalized.length <= 5) return normalized + const normalized = value + .toUpperCase() + .replace(/0/g, 'O') + .replace(/1/g, 'I') + .replace(/[^A-Z2-7]/g, '') + if (normalized.length < 5) return normalized + if (normalized.length === 5) { + // keep the hyphen if the user typed it themselves + return value.endsWith('-') ? `${normalized}-` : normalized + } return `${normalized.slice(0, 5)}-${normalized.slice(5)}` }