feat(mobile): PinInput component — 6-digit numeric pad
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
import { fireEvent, render } from '@testing-library/react-native';
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import { PinInput } from './PinInput';
|
||||
|
||||
describe('<PinInput>', () => {
|
||||
it('appends digits to the underlying value and stops at length', () => {
|
||||
const onChange = vi.fn();
|
||||
const { getByTestId } = render(
|
||||
<PinInput value="" onChange={onChange} length={6} ariaLabel="PIN" />,
|
||||
);
|
||||
fireEvent.changeText(getByTestId('pin-input'), '1234567890');
|
||||
expect(onChange).toHaveBeenCalledWith('123456');
|
||||
});
|
||||
|
||||
it('strips non-digits', () => {
|
||||
const onChange = vi.fn();
|
||||
const { getByTestId } = render(
|
||||
<PinInput value="" onChange={onChange} length={6} ariaLabel="PIN" />,
|
||||
);
|
||||
fireEvent.changeText(getByTestId('pin-input'), '1a2b3c');
|
||||
expect(onChange).toHaveBeenCalledWith('123');
|
||||
});
|
||||
|
||||
it('renders one bullet per filled slot', () => {
|
||||
const { getAllByText } = render(
|
||||
<PinInput value="123" onChange={() => {}} length={6} ariaLabel="PIN" />,
|
||||
);
|
||||
expect(getAllByText('•').length).toBe(3);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,95 @@
|
||||
import { useEffect, useRef } from 'react';
|
||||
import {
|
||||
Pressable,
|
||||
StyleSheet,
|
||||
Text,
|
||||
TextInput,
|
||||
View,
|
||||
type TextInput as TextInputType,
|
||||
} from 'react-native';
|
||||
|
||||
import { colors } from '../theme/colors';
|
||||
|
||||
interface Props {
|
||||
value: string;
|
||||
onChange: (next: string) => void;
|
||||
length?: number;
|
||||
autoFocus?: boolean;
|
||||
disabled?: boolean;
|
||||
ariaLabel: string;
|
||||
onSubmit?: () => void;
|
||||
}
|
||||
|
||||
// Six-slot numeric PIN entry. The actual input is an invisible TextInput
|
||||
// that captures the numeric keyboard; visible slots render bullets when
|
||||
// filled. Tapping anywhere on the row re-focuses the input.
|
||||
export function PinInput({
|
||||
value,
|
||||
onChange,
|
||||
length = 6,
|
||||
autoFocus,
|
||||
disabled,
|
||||
ariaLabel,
|
||||
onSubmit,
|
||||
}: Props) {
|
||||
const ref = useRef<TextInputType | null>(null);
|
||||
useEffect(() => {
|
||||
if (autoFocus) ref.current?.focus();
|
||||
}, [autoFocus]);
|
||||
return (
|
||||
<Pressable onPress={() => ref.current?.focus()} style={styles.row}>
|
||||
<TextInput
|
||||
ref={ref}
|
||||
testID="pin-input"
|
||||
accessibilityLabel={ariaLabel}
|
||||
keyboardType="numeric"
|
||||
textContentType="oneTimeCode"
|
||||
autoComplete="one-time-code"
|
||||
maxLength={length}
|
||||
editable={!disabled}
|
||||
value={value}
|
||||
onChangeText={(t) => onChange(t.replace(/\D/g, '').slice(0, length))}
|
||||
onSubmitEditing={() => {
|
||||
if (value.length === length) onSubmit?.();
|
||||
}}
|
||||
style={styles.hidden}
|
||||
/>
|
||||
<View style={styles.slots}>
|
||||
{Array.from({ length }).map((_, i) => {
|
||||
const filled = i < value.length;
|
||||
return (
|
||||
<View key={i} style={[styles.slot, filled && styles.slotFilled]}>
|
||||
{filled && <Text style={styles.bullet}>•</Text>}
|
||||
</View>
|
||||
);
|
||||
})}
|
||||
</View>
|
||||
</Pressable>
|
||||
);
|
||||
}
|
||||
|
||||
const styles = StyleSheet.create({
|
||||
row: { alignItems: 'center' },
|
||||
hidden: {
|
||||
position: 'absolute',
|
||||
width: 1,
|
||||
height: 1,
|
||||
opacity: 0,
|
||||
},
|
||||
slots: { flexDirection: 'row', gap: 8 },
|
||||
slot: {
|
||||
width: 40,
|
||||
height: 48,
|
||||
borderRadius: 10,
|
||||
borderWidth: 1,
|
||||
borderColor: colors.border,
|
||||
alignItems: 'center',
|
||||
justifyContent: 'center',
|
||||
backgroundColor: colors.surface,
|
||||
},
|
||||
slotFilled: {
|
||||
borderColor: colors.accent,
|
||||
backgroundColor: colors.bg,
|
||||
},
|
||||
bullet: { color: colors.text, fontSize: 22 },
|
||||
});
|
||||
Reference in New Issue
Block a user