Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): leave an IME composition alone in the pairing field

Yudhi Armyndharisb0775fde2f94
diff --git a/dashboard/src/pages/Sessions.test.ts b/dashboard/src/pages/Sessions.test.ts
index 8f75e4286..8538ca5e8 100644
--- a/dashboard/src/pages/Sessions.test.ts
+++ b/dashboard/src/pages/Sessions.test.ts
@@ -655,6 +655,35 @@ test('a pairing phone number typed in native-script or full-width digits is kept
}
});
+// An IME composition in progress is the field's own text: a controlled input that rewrites it mid-composition garbles
+// what the IME then commits. The digits are read when the composition ends; pasted and directly typed text is read at once.
+test('the pairing phone field leaves a composition in progress alone and reads it when it ends', async () => {
+ const { screen, fireEvent, within } = rtl;
+ resetFetchCalls();
+ renderSessions();
+
+ await screen.findByText('new-device');
+ const qrCard = screen.getByText('new-device').closest('.session-card') as HTMLElement;
+ fireEvent.click(within(qrCard).getByRole('button', { name: 'Show QR' }));
+ await screen.findByAltText('QR');
+ fireEvent.click(screen.getByRole('tab', { name: 'Link with Phone Number' }));
+ const phoneInput = screen.getByLabelText('Phone Number') as HTMLInputElement;
+ const fullwidth = (ascii: string) => [...ascii].map(d => String.fromCharCode(0xff10 + Number(d))).join('');
+
+ fireEvent.compositionStart(phoneInput);
+ fireEvent.input(phoneInput, { target: { value: fullwidth('6') }, isComposing: true });
+ assert.equal(phoneInput.value, fullwidth('6'));
+ fireEvent.input(phoneInput, { target: { value: fullwidth('62') }, isComposing: true });
+ assert.equal(phoneInput.value, fullwidth('62'));
+
+ fireEvent.compositionEnd(phoneInput);
+ assert.equal(phoneInput.value, '62');
+
+ // Outside a composition the text is read at once, as before.
+ fireEvent.input(phoneInput, { target: { value: fullwidth('628123456789') } });
+ assert.equal(phoneInput.value, '628123456789');
+});
+
// Nothing server-side refuses a pairing code for a number linked elsewhere, so the panel has to say
// what it can cost before the operator types one.
test('the phone pairing tab warns that a code can unlink an existing session', async () => {
diff --git a/dashboard/src/pages/Sessions.tsx b/dashboard/src/pages/Sessions.tsx
index d39c2a557..963c07b42 100644
--- a/dashboard/src/pages/Sessions.tsx
+++ b/dashboard/src/pages/Sessions.tsx
@@ -885,7 +885,18 @@ export function Sessions() {
maxLength={15}
placeholder={t('sessions.pairing.phonePlaceholder')}
value={phoneNumber}
- onChange={e => setPhoneNumber(normalizePhoneText(e.target.value).replace(/\D/g, ''))}
+ // A composition in progress is left as typed, since rewriting it garbles what the IME commits;
+ // it is read when the composition ends. Pasted and directly typed text is read at once.
+ onChange={e =>
+ setPhoneNumber(
+ (e.nativeEvent as InputEvent).isComposing
+ ? e.target.value
+ : normalizePhoneText(e.target.value).replace(/\D/g, ''),
+ )
+ }
+ onCompositionEnd={e =>
+ setPhoneNumber(normalizePhoneText(e.currentTarget.value).replace(/\D/g, ''))
+ }
onKeyDown={e => e.key === 'Enter' && handleGeneratePairingCode()}
/>
<p className="input-hint" style={{ marginBottom: '1.5rem' }}>