Search code examples
react-nativeformiktextinput

Pass raw value to onChangeText in React Native


In my react project i am using Formik library. for the input i am using TextInputMask which is other library.

export default function Calc() {
return (
    <Formik
        initialValues={{ monthly_rental: '', }}
    >
        {(props) => (
            <View>
                <Text>Monthly rental</Text>
                <TextInputMask
                    type={"money"}
                    options={{
                        precision: 0,
                        separator: ".",
                        delimiter: ",",
                        unit: "£",
                        suffixUnit: ""
                    }}
                    placeholder={"£500"}
                    value={props.values.monthly_rental}
                    includeRawValueInChangeText={true}
                    onChangeText={props.handleChange('monthly_rental')}
                />
            </View>
        )}
    </Formik>
  )
 }

The above code work nicely but what i want is on onChangeText={props.handleChange('monthly_rental')} set the raw value instead of formatted value.

i have seen this code but i dont now to to implement this

onChangeText={(text, rawValue) => {
        this.setState({
          value: text,
          raw: rawValue
        })

Solution

  • So finally I have find out. In the Formik Library there is a function called setFieldValue: (field: string, value: any, shouldValidate?: boolean) => void. see here

    So my final solution was this :

    <TextInputMask
      type={"money"}
      options={{
        precision: 0,
        separator: ".",
        delimiter: ",",
        unit: "£",
        suffixUnit: ""
      }}
      style={globalstyles.input}
      textAlign={"center"}
      placeholder={"£500"}
      keyboardType={"decimal-pad"}
      value={props.values.monthly_rental}
      includeRawValueInChangeText={true}
      onChangeText={(maskedText, rawText) => {
        // Set RawText
        props.setFieldValue('monthly_rental', rawText)
      }}
    />