retyui / react-native-confirmation-code-field

A react-native confirmation code field compatible with iOS, Android and Web
MIT License
1.07k stars 125 forks source link

Facing problems with using new method of calling onFulfill() #202

Closed aggnostos closed 2 years ago

aggnostos commented 2 years ago

I need to call onFulfill when entire code is entered. So I removed onChangeText={setValue} and pasted:

onChangeText={(value) => {
    if (value.length === CELL_COUNT) {
        onFulfill(value);
    }
}}

But now I can't enter any text, so where I should place setValue?

LordParsley commented 2 years ago

@Lian39 you'll still need to update the value using setValue or else the component won't know that its own value has changed. But you can add your onFulfill call after that.

Based on the minimal example, maybe try something like this:

<CodeField
      ref={ref}
      {...props}
      // Use `caretHidden={false}` when users can't paste a text value, because context menu doesn't appear
      value={value}
      onChangeText={(text) => {
        setValue(text);

        // Submit the completed PIN.
        if (text && text.length === CELL_COUNT) {
          onFulfill(text);
        }
      }}
      cellCount={CELL_COUNT}
      rootStyle={styles.codeFieldRoot}
      keyboardType="number-pad"
      textContentType="oneTimeCode"
      renderCell={({ index, symbol, isFocused }) => (
        <Text
          key={index}
          style={[styles.cell, isFocused && styles.focusCell]}
          onLayout={getCellOnLayoutHandler(index)}
        >
          {symbol || (isFocused ? <Cursor /> : null)}
        </Text>
      )}
    />