7.4.0 • Published 9 months ago

react-native-confirmation-code-field v7.4.0

Weekly downloads
28,184
License
MIT
Repository
github
Last release
9 months ago

react-native-confirmation-code-field

react-native-confirmation-code-field on npm react-native-confirmation-code-field downloads react-native-confirmation-code-field install size CI status

A simple react-native confirmation code field compatible with iOS, Android.

Links

Component features:

  • 🔮 Simple and tiny 3.8 KB. Easy to use;
  • 🚮 Clearing part of the code by clicking on the cell;
  • 🍎 Support "fast paste SMS-code" on iOS. And custom code paste for Android;
  • ⚡ TextInput ref support;
  • 🛠 Highly customizable. Can be used as masked TextInput;
  • 🤓 Readable changelog.

Screenshots

react-native-confirmation-code-field animated examplereact-native-confirmation-code-field mask examplereact-native-confirmation-code-field unmask examplereact-native-confirmation-code-field underline examplereact-native-confirmation-code-field formatting example

Install

yarn add react-native-confirmation-code-field

How it works

I use an invisible <TextInput/> component that will be stretched over <Cell/> components.

JSX structure looks like that:

// Root view (rectangle with a red border on 3d visualization below)
<View style={rootStyle}>
  // Each Cell element is result of a `renderCell` function (gray boxes)
  <Cell>1</Cell>
  <Cell>2</Cell>
  <Cell>3</Cell>
  <Cell>|</Cell>
  <Cell></Cell>
  <Cell></Cell>
  // Invisible Text Input with absolute position (gray rectangle with text '123')
  <TextInput value="123"/>
</View>

3d layout of component

It needs to solve next problems:

  • When user pastes code from SMS on iOS issue#25
  • Better UX when user types fast, or system sluggish, characters might lost when component switching focus between <TextInput/>.

Basic example

I took a minimal implementation approach. It mean that this component provides low-level functionality so you can create incredible UI without tears 😭. I recommend you start with creating your own wrapper where you apply all styles and basic configuration.

You can use a ready-made solution out of the box:

import React, {useState} from 'react';
import {SafeAreaView, Text, StyleSheet} from 'react-native';

import {
  CodeField,
  Cursor,
  useBlurOnFulfill,
  useClearByFocusCell,
} from 'react-native-confirmation-code-field';

const styles = StyleSheet.create({
  root: {flex: 1, padding: 20},
  title: {textAlign: 'center', fontSize: 30},
  codeFieldRoot: {marginTop: 20},
  cell: {
    width: 40,
    height: 40,
    lineHeight: 38,
    fontSize: 24,
    borderWidth: 2,
    borderColor: '#00000030',
    textAlign: 'center',
  },
  focusCell: {
    borderColor: '#000',
  },
});

const CELL_COUNT = 6;

const App = () => {
  const [value, setValue] = useState('');
  const ref = useBlurOnFulfill({value, cellCount: CELL_COUNT});
  const [props, getCellOnLayoutHandler] = useClearByFocusCell({
    value,
    setValue,
  });

  return (
    <SafeAreaView style={styles.root}>
      <Text style={styles.title}>Verification</Text>
      <CodeField
        ref={ref}
        {...props}
        // Use `caretHidden={false}` when users can't paste a text value, because context menu doesn't appear
        value={value}
        onChangeText={setValue}
        cellCount={CELL_COUNT}
        rootStyle={styles.codeFieldRoot}
        keyboardType="number-pad"
        textContentType="oneTimeCode"
        autoComplete={Platform.select({ android: 'sms-otp', default: 'one-time-code' })}
        testID="my-code-input"
        renderCell={({index, symbol, isFocused}) => (
          <Text
            key={index}
            style={[styles.cell, isFocused && styles.focusCell]}
            onLayout={getCellOnLayoutHandler(index)}>
            {symbol || (isFocused ? <Cursor/> : null)}
          </Text>
        )}
      />
    </SafeAreaView>
  );
};

export default App;

Compatibility

For react-native@0.63.x and below use yarn add react-native-confirmation-code-field@6, otherwise use the latest version yarn add react-native-confirmation-code-field

7.4.0

9 months ago

7.3.2

1 year ago

7.3.1

2 years ago

7.3.0

3 years ago

7.2.0

3 years ago

7.1.0

4 years ago

6.6.0

4 years ago

6.7.0

4 years ago

7.0.1

4 years ago

7.0.0

4 years ago

6.5.1

4 years ago

6.5.0

4 years ago

6.3.0

5 years ago

6.4.0

5 years ago

6.2.0

5 years ago

6.2.0-beta.0

5 years ago

6.2.0-beta.2

5 years ago

6.2.0-beta.1

5 years ago

6.1.1

5 years ago

6.1.0

5 years ago

6.0.3

5 years ago

6.0.1

5 years ago

6.0.2

5 years ago

6.0.0

5 years ago

6.0.0-rc.1

5 years ago

6.0.0-rc.0

5 years ago

5.1.0

5 years ago

5.0.3

5 years ago

5.0.2

5 years ago

5.0.1

5 years ago

5.0.0-beta.2

5 years ago

5.0.0-beta.0

5 years ago

5.0.0-beta.1

5 years ago

4.2.0

5 years ago

5.0.0

5 years ago

4.1.0

5 years ago

4.0.0

5 years ago

3.9.0

5 years ago

3.8.0

5 years ago

3.7.1

5 years ago

3.7.0

5 years ago

3.6.0

6 years ago

3.5.2

6 years ago

3.5.1

6 years ago

3.5.0

6 years ago

3.4.1

6 years ago

3.4.0

6 years ago

3.3.0

6 years ago

3.2.3

6 years ago

3.2.2

6 years ago

3.2.1

6 years ago

3.2.0

6 years ago

3.1.3

6 years ago

3.1.2

6 years ago

3.1.1

6 years ago

3.1.0

6 years ago

3.0.0-beta-5

6 years ago

3.0.0

6 years ago

3.0.0-beta-4

6 years ago

3.0.0-beta-3

6 years ago

3.0.0-beta-2

6 years ago

3.0.0-beta-1

6 years ago

2.0.5

6 years ago

2.0.4

6 years ago

2.0.3

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.2.4

6 years ago

1.2.3

6 years ago

2.0.0-beta.3

6 years ago

2.0.0-beta.2

6 years ago

1.2.2

6 years ago

2.0.0-beta.1

6 years ago

1.2.1

6 years ago

2.0.0-beta.0

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago