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

[FEAT]: Set nbDigits dynamic for PinCodeInput #7553

Merged
merged 1 commit into from
Aug 8, 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
5 changes: 5 additions & 0 deletions .changeset/metal-dolls-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"live-mobile": patch
---

Set nbDigits dynamic for PinCodeInput
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ const StepFlow = ({
}: Props) => {
const { memberCredentials } = useInitMemberCredentials();

const { handleStart, handleSendDigits, inputCallback, digits } = useSyncWithQrCode();
const { handleStart, handleSendDigits, inputCallback, nbDigits } = useSyncWithQrCode();

const handleQrCodeScanned = (data: string) => {
onQrCodeScanned();
if (memberCredentials) handleStart(data, memberCredentials, setCurrentStep);
};

const handlePinCodeSubmit = (input: string) => {
if (input && inputCallback && digits === input.length) handleSendDigits(inputCallback, input);
if (input && inputCallback && nbDigits === input.length) handleSendDigits(inputCallback, input);
};

const getScene = () => {
Expand Down Expand Up @@ -88,7 +88,9 @@ const StepFlow = ({
return qrProcess.pinCode ? <PinCodeDisplay pinCode={qrProcess.pinCode} /> : null;

case Steps.PinInput:
return <PinCodeInput handleSendDigits={handlePinCodeSubmit} />;
return nbDigits ? (
<PinCodeInput handleSendDigits={handlePinCodeSubmit} nbDigits={nbDigits} />
) : null;

case Steps.SyncError:
return <SyncError tryAgain={navigateToQrCodeMethod} />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ const ActivationFlow = ({
}: Props) => {
const { memberCredentials } = useInitMemberCredentials();

const { handleStart, handleSendDigits, inputCallback, digits } = useSyncWithQrCode();
const { handleStart, handleSendDigits, inputCallback, nbDigits } = useSyncWithQrCode();

const handleQrCodeScanned = (data: string) => {
onQrCodeScanned();
if (memberCredentials) handleStart(data, memberCredentials, setCurrentStep);
};

const handlePinCodeSubmit = (input: string) => {
if (input && inputCallback && digits === input.length) handleSendDigits(inputCallback, input);
if (input && inputCallback && nbDigits === input.length) handleSendDigits(inputCallback, input);
};

const getScene = () => {
Expand Down Expand Up @@ -79,7 +79,9 @@ const ActivationFlow = ({
return qrProcess.pinCode ? <PinCodeDisplay pinCode={qrProcess.pinCode} /> : null;

case Steps.PinInput:
return <PinCodeInput handleSendDigits={handlePinCodeSubmit} />;
return nbDigits ? (
<PinCodeInput handleSendDigits={handlePinCodeSubmit} nbDigits={nbDigits} />
) : null;

case Steps.SyncError:
return <SyncError tryAgain={navigateToQrCodeMethod} />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ export function ListInstances({ onClickInstance, changeScene, members, currentIn

const handleAutoRemove = () => changeScene(Scene.AutoRemove);

const handleGoDeleteInstance = (instance: TrustchainMember) => {
// eslint-disable-next-line no-console
console.log("delete instance IMPLEMENTED IN NEXT PR", instance);
onClickInstance(instance);
};
const handleGoDeleteInstance = (instance: TrustchainMember) => onClickInstance(instance);

const renderItem = ({ item }: ListRenderItemInfo<TrustchainMember>) => {
const instance = item;
Expand Down Expand Up @@ -54,7 +50,7 @@ export function ListInstances({ onClickInstance, changeScene, members, currentIn
renderItem={renderItem}
keyExtractor={s => s.id}
contentContainerStyle={{
paddingBottom: 10,
paddingBottom: 20,
}}
ItemSeparatorComponent={() => <Flex height={12} />}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { NavigatorName, ScreenName } from "~/const";
import { useInstanceName } from "./useInstanceName";

export const useSyncWithQrCode = () => {
const [digits, setDigits] = useState<number | null>(null);
const [nbDigits, setDigits] = useState<number | null>(null);
const [input, setInput] = useState<string | null>(null);
const instanceName = useInstanceName();

Expand Down Expand Up @@ -71,5 +71,5 @@ export const useSyncWithQrCode = () => {
[],
);

return { digits, input, handleStart, handleSendDigits, setInput, inputCallback };
return { nbDigits, input, handleStart, handleSendDigits, setInput, inputCallback };
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ import { NativeSyntheticEvent, TextInput, TextInputKeyPressEventData } from "rea

type Props = {
handleSendDigits: (input: string) => void;
nbDigits: number;
};

export default function PinCodeInput({ handleSendDigits }: Props) {
export default function PinCodeInput({ nbDigits, handleSendDigits }: Props) {
const { t } = useTranslation();
const inputRefs = [useRef<TextInput>(null), useRef<TextInput>(null), useRef<TextInput>(null)];
const [digits, setDigits] = useState<string[]>(["", "", ""]);

// Dynamically create refs based on the number of digits
const inputRefs = useRef<(TextInput | null)[]>(Array(nbDigits).fill(null));
const [digits, setDigits] = useState<string[]>(Array(nbDigits).fill(""));

useEffect(() => {
if (digits.every(digit => digit)) {
Expand All @@ -25,15 +28,15 @@ export default function PinCodeInput({ handleSendDigits }: Props) {
const newDigits = [...digits];
newDigits[index] = value;
setDigits(newDigits);
if (value && index < digits.length - 1) {
inputRefs[index + 1].current?.focus();
if (value && index < nbDigits - 1) {
inputRefs.current[index + 1]?.focus();
}
};

const handleKeyPress = (e: NativeSyntheticEvent<TextInputKeyPressEventData>, index: number) => {
if (e.nativeEvent.key === "Backspace") {
if (!digits[index] && index > 0) {
inputRefs[index - 1].current?.focus();
inputRefs.current[index - 1]?.focus();
} else {
const newDigits = [...digits];
newDigits[index] = "";
Expand All @@ -59,7 +62,7 @@ export default function PinCodeInput({ handleSendDigits }: Props) {
onChange={value => handleChange(value, index)}
onKeyPress={e => handleKeyPress(e, index)}
index={index}
ref={inputRefs[index]}
ref={el => (inputRefs.current[index] = el)}
/>
))}
</Flex>
Expand All @@ -74,18 +77,12 @@ interface DigitInputProps {
index: number;
}

interface TextInputRef {
focus: () => void;
}

const DigitInput = forwardRef<TextInputRef, DigitInputProps>(
const DigitInput = forwardRef<TextInput, DigitInputProps>(
({ value, onChange, onKeyPress, index }, forwardedRef) => {
const [isFocused, setIsFocused] = useState(false);
const inputRef = useRef<TextInput>(null);

useImperativeHandle(forwardedRef, () => ({
focus: () => inputRef.current?.focus(),
}));
useImperativeHandle(forwardedRef, () => inputRef.current as TextInput);

const handleChange = (text: string) => {
if (text.length <= 1 && /^\d*$/.test(text)) {
Expand Down
Loading