Back to Mantine

Pin Input

apps/mantine.dev/src/pages/core/pin-input.mdx

9.2.02.2 KB
Original Source

import { PinInputDemos } from '@docs/demos'; import { Layout } from '@/layout'; import { MDX_DATA } from '@/mdx';

export default Layout(MDX_DATA.PinInput);

Usage

<Demo data={PinInputDemos.configurator} />

Controlled

tsx
import { useState } from 'react';
import { PinInput } from '@mantine/core';

function Demo() {
  const [value, setValue] = useState('');
  return <PinInput value={value} onChange={setValue} />;
}

Uncontrolled

PinInput can be used with uncontrolled forms the same way as a native input element. Set the name attribute to include pin input value in FormData object on form submission. To control the initial value in uncontrolled forms, use the defaultValue prop.

Example usage of uncontrolled PinInput with FormData:

tsx
import { PinInput } from '@mantine/core';

function Demo() {
  return (
    <form
      onSubmit={(event) => {
        event.preventDefault();
        const formData = new FormData(event.currentTarget);
        console.log('PIN value:', formData.get('pin'));
      }}
    >
      <PinInput
        name="pin"
        length={4}
        oneTimeCode
      />
      <button type="submit">Submit</button>
    </form>
  );
}

Regex type

You can use a regular expression to validate user input. Characters that do not match the given expression will be disregarded. For example, to create a PinInput that will accept only numbers from 0 to 3, set type={/^[0-3]+/}:

<Demo data={PinInputDemos.regexp} />

One time code

Some operating systems expose the last received SMS code to be used by applications like your keyboard. If the current form input asks for this code, your keyboard adapts and proposes the code as a keyboard suggestion. The oneTimeCode prop makes your input set autocomplete="one-time-code" which allows using that feature.

tsx
import { PinInput } from '@mantine/core';

function OneTimeCodeInput() {
  return <PinInput oneTimeCode />;
}
<StylesApiSelectors component="PinInput" /> <Demo data={PinInputDemos.stylesApi} />

Accessibility

Inputs do not have associated labels. Set aria-label to make the component visible to screen readers:

tsx
import { PinInput } from '@mantine/core';

function Accessibility() {
  return <PinInput aria-label="One time code" />;
}