Mobilens 中文

Control · Inputs

OTP input

Code input style

Every state

When
Behaviour

    Usage

    import { useState } from 'react';
    import { OtpInput } from '@/components/mobilens/OtpInput';
    
    export function VerifyCode({ verify }: { verify: (code: string) => Promise<boolean> }) {
      const [code, setCode] = useState('');
      const [wrong, setWrong] = useState(false);
      const [checking, setChecking] = useState(false);
    
      return (
        <OtpInput
          variant="segmented"
          value={code}
          autoFocus
          error={wrong}
          disabled={checking}
          onChangeText={(next) => {
            setCode(next);
            setWrong(false);
          }}
          onFilled={async (full) => {
            setChecking(true);
            setWrong(!(await verify(full)));
            setChecking(false);
          }}
        />
      );
    }
    

    API

    PropTypeDefaultDescription
    value string – Digits typed so far. Keep it in state and pass it back, like a TextInput.
    onChangeText (value: string) => void – Called with the new digits on every edit. Non-digits are already removed.
    onFilled? (code: string) => void – Called once each time the full code is entered.
    length? number 6 Number of digits.
    variant? OtpInputVariant 'segmented' Show each digit in its own cell or all digits inside one outline.
    error? boolean false Red outline and a shake. Clear it when the user edits the code.
    disabled? boolean false Disable editing, for example while the code is checked.
    autoFocus? boolean false Focus the field and bring up the number pad when the screen opens.
    accessibilityLabel? string – What screen readers announce for the field.
    ref? Ref<ComponentRef<typeof TextInput>> – The hidden field, to focus() or blur() it.

    Used in