Code input example

Native change events and read-only entry.

The ordinary value/onChange convention receives the canonical code after native paste.

Complete React client module · MIT · Next.js and Vite

Interactive example

Native change events and read-only entry

The ordinary value/onChange convention receives the canonical code after native paste.

Keyboard

  • Paste formatted text into the real input.
  • Select a text range and use Backspace to edit it.
  • Read-only entry keeps its value when paste is attempted.

States covered

  • Canonical paste
  • Native change feedback
  • Complete code
  • Read only

Install this complete example and its component dependencies.

Terminal
npx shadcn@latest add https://ui.bld8.dev/r/example-otp-native-change.json
Complete source
React / TypeScript
/*!
MIT License

Copyright (c) 2026 Jaison

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
'use client';
import {useState} from 'react';
import {OTPInput} from '@bld8/ui';
import '@bld8/ui/styles.css';
export default function NativeCodeExample(){
  const [code,setCode]=useState(''),[lastEdit,setLastEdit]=useState(''),[complete,setComplete]=useState('');
  return <div>
    <OTPInput label="Native change example code" length={6} value={code}
      onChange={event=>{setCode(event.currentTarget.value);setLastEdit(event.currentTarget.value);}} onComplete={setComplete}/>
    <p role="status">Canonical edit: {lastEdit}</p><p>Last complete code: {complete}</p>
    <OTPInput label="Read only example code" length={4} value="1234" readOnly/>
  </div>;
}